Skip to content

Commit

Permalink
Change leaf algorythm
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier HAUSHERR committed Sep 5, 2012
1 parent 12080d4 commit 3fa461d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
76 changes: 31 additions & 45 deletions Leaf.php
Expand Up @@ -146,78 +146,64 @@ protected function _getAnnotatedText()
// creation order for annotation
foreach ($this->_annotations as $i => $a)
{
// begin
// end - Must be set first because we close before open tags

if (! isset($list[$a->range->start]))
if (! isset($list[$a->range->end]))
{
$list[$a->range->start] = array();
$list[$a->range->end] = array();
}

$list[$a->range->start][] =
$list[$a->range->end][] =
array(
'type' => $a->type,
'begin' => true,
'data' => (isset($a->data) ? $a->data : null)
'begin' => false,
'data' => null
);

// end
// begin

if (! isset($list[$a->range->end]))
if (! isset($list[$a->range->start]))
{
$list[$a->range->end] = array();
$list[$a->range->start] = array();
}

$list[$a->range->end][] =
$list[$a->range->start][] =
array(
'type' => $a->type,
'begin' => false,
'data' => null
'begin' => true,
'data' => (isset($a->data) ? $a->data : null)
);

}

ksort($list);

// génération du vrai texte
if(count($list) > 0)
{
$text = '';
$length = 0;

$textTransformed = $this->_encodeText($this->_text);
$text = $this->_encodeText($this->_text);

$mb_text = preg_split(
'/(?<!^)(?!$)/u', $textTransformed
);
$strlen = count($mb_text);

for ($i = 0 ; $i < $strlen ; $i++)
foreach ($list as $char => $annotations)
{
if (isset($list[$i]))
$replace = null;
$pos = $char + $length;

foreach($annotations as $ann)
{
foreach ($list[$i] as $ann)
{
$text .=
Annotation::getTag(
$ann['type'],
$ann['begin'],
$ann['data']
);
}
$replace.= Annotation::getTag(
$ann['type'],
$ann['begin'],
$ann['data']
);
}
$text .= $mb_text[$i];
}

// fin de chaine
$len = mb_strlen($replace);

if (isset($list[$strlen]))
{
foreach ($list[$strlen] as $ann)
{
$text .=
Annotation::getTag(
$ann['type'],
$ann['begin'],
$ann['data']
);
}
$text = mb_substr($text, 0, $pos) . $replace .
mb_substr($text, $pos);

$length+= $len;
}

$text = $this->_entitiesText($text);
Expand Down
19 changes: 18 additions & 1 deletion Tests/MediaWiki/LeafParagraphTest.php
Expand Up @@ -114,7 +114,7 @@ public function testRenderWithLink()
*@group glou
*/

public function testRenderWithUTF8()
public function testRenderWithUTF8()
{
$leafJson =
json_decode(
Expand All @@ -131,6 +131,23 @@ public function testRenderWithUTF8()
);
}

public function testRenderWithUTF8WithHtml()
{
$leafJson =
json_decode(
'{"type":"paragraph","content":{"text":"Voilà <b>voilà</b>",' .
'"annotations"' .
':[{"type":"textStyle/bold","range":{"start":3,"end":6}}]}}'
);

$leaf = new LeafParagraph($leafJson);

$this->assertEquals(
'<p>Voi<b>là </b>&lt;b&gt;voilà&lt;/b&gt;</p>',
$leaf->render()
);
}

public function testRenderWithDel()
{
$leafJson =
Expand Down

0 comments on commit 3fa461d

Please sign in to comment.