Skip to content

Commit

Permalink
text length must be recalculated after applying each shortcode replac…
Browse files Browse the repository at this point in the history
…ement because of the convoluted replacement logic due to the issues with multibyte strings
  • Loading branch information
thunderer committed Feb 25, 2016
1 parent 9f74c82 commit d1bfba4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ private function processIteration($text, ProcessorContext $context, ProcessedSho

private function applyReplaces($text, array $replaces)
{
$textLength = mb_strlen($text, 'utf-8');

return array_reduce(array_reverse($replaces), function($state, ReplacedShortcode $s) use($textLength) {
return array_reduce(array_reverse($replaces), function($state, ReplacedShortcode $s) {
$offset = $s->getOffset();
$length = mb_strlen($s->getText(), 'utf-8');
$textLength = mb_strlen($state, 'utf-8');

return mb_substr($state, 0, $offset, 'utf-8').$s->getReplacement().mb_substr($state, $offset + $length, $textLength, 'utf-8');
}, $text);
Expand Down
9 changes: 9 additions & 0 deletions tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public function testProcessorParentContext()
$this->assertSame($result.$result, $processor->process($text.$text));
}

public function testReplacesLongerThanInputText()
{
$handlers = new HandlerContainer();
$handlers->add('x', function() { return '<length>'; });
$processor = new Processor(new RegularParser(), $handlers);

$this->assertSame('<length><length><length>', $processor->process('[x][x][x]'));
}

public function testProcessorWithoutRecursion()
{
$processor = new Processor(new RegexParser(), $this->getHandlers());
Expand Down

0 comments on commit d1bfba4

Please sign in to comment.