Skip to content

Commit ff97904

Browse files
committed
fixed parent shortcode in context, added test for stripping content
1 parent 303f4b6 commit ff97904

4 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎src/Processor/Processor.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Thunder\Shortcode\Parser\ParserInterface;
66
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface;
77
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
8+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
89

910
/**
1011
* @author Tomasz Kowalczyk <tomasz@kowalczyk.cc>
@@ -39,7 +40,7 @@ public function process($text)
3940

4041
while ($iterations--) {
4142
$context->iterationNumber++;
42-
$newText = $this->processIteration($text, $context);
43+
$newText = $this->processIteration($text, $context, null);
4344
if ($newText === $text) {
4445
break;
4546
}
@@ -50,7 +51,7 @@ public function process($text)
5051
return $text;
5152
}
5253

53-
private function processIteration($text, ProcessorContext $context)
54+
private function processIteration($text, ProcessorContext $context, ShortcodeInterface $parent = null)
5455
{
5556
if (null !== $this->recursionDepth && $context->recursionLevel > $this->recursionDepth) {
5657
return $text;
@@ -60,6 +61,7 @@ private function processIteration($text, ProcessorContext $context)
6061
$shortcodes = $this->parser->parse($text);
6162
$replaces = array();
6263
foreach ($shortcodes as $shortcode) {
64+
$context->parent = $parent;
6365
$this->prepareHandlerContext($shortcode, $context);
6466
$handler = $this->handlers->get($shortcode->getName());
6567
$replace = $this->processHandler($shortcode, $context, $handler);
@@ -83,6 +85,7 @@ private function prepareHandlerContext(ParsedShortcodeInterface $shortcode, Proc
8385
$context->shortcodeText = $shortcode->getText();
8486
$context->offset = $shortcode->getOffset();
8587
$context->shortcode = $shortcode;
88+
$context->originalContent = $shortcode->getContent();
8689
}
8790

8891
private function processHandler(ParsedShortcodeInterface $parsed, ProcessorContext $context, $handler)
@@ -99,10 +102,8 @@ private function processRecursion(ParsedShortcodeInterface $shortcode, Processor
99102
{
100103
if ($this->autoProcessContent && null !== $shortcode->getContent()) {
101104
$context->recursionLevel++;
102-
$context->parent = $shortcode;
103105
// this is safe from using max iterations value because it's manipulated in process() method
104-
$content = $this->processIteration($shortcode->getContent(), $context);
105-
$context->parent = null;
106+
$content = $this->processIteration($shortcode->getContent(), $context, $shortcode);
106107
$context->recursionLevel--;
107108

108109
return $shortcode->withContent($content);

‎src/Processor/ProcessorContext.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class ProcessorContext
1717
/** @var ProcessorInterface */
1818
public $processor = null;
1919

20+
public $originalContent = null;
2021
public $position = 0;
2122
public $namePosition = array();
2223
public $text = '';

‎src/Shortcode/ProcessedShortcode.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ final class ProcessedShortcode extends AbstractShortcode implements ParsedShortc
1414
private $position;
1515
private $namePosition;
1616
private $text;
17+
private $originalContent;
1718
private $offset;
1819
private $shortcodeText;
1920
private $iterationNumber;
@@ -34,6 +35,7 @@ public static function createFromContext(ProcessorContext $context)
3435
$self->parameters = $context->shortcode->getParameters();
3536
$self->content = $context->shortcode->getContent();
3637
$self->bbCode = $context->shortcode->getBbCode();
38+
$self->originalContent = $context->originalContent;
3739

3840
// runtime context
3941
$self->parent = $context->parent;
@@ -66,6 +68,11 @@ public function getParent()
6668
return $this->parent;
6769
}
6870

71+
public function getOriginalContent()
72+
{
73+
return $this->originalContent;
74+
}
75+
6976
public function getPosition()
7077
{
7178
return $this->position;

‎tests/ProcessorTest.php‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Thunder\Shortcode\Parser\RegexParser;
66
use Thunder\Shortcode\Parser\RegularParser;
77
use Thunder\Shortcode\Processor\Processor;
8+
use Thunder\Shortcode\Shortcode\ParsedShortcode;
9+
use Thunder\Shortcode\Shortcode\ParsedShortcodeInterface;
810
use Thunder\Shortcode\Shortcode\ProcessedShortcode;
911
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
1012
use Thunder\Shortcode\Tests\Fake\ReverseShortcode;
@@ -208,6 +210,49 @@ public function testDefaultHandler()
208210
$this->assertSame('namerandom', $processor->process('[name][other][/name][random]'));
209211
}
210212

213+
public function testStripOuter()
214+
{
215+
$handlers = new HandlerContainer();
216+
$handlers->add('q', function(ShortcodeInterface $s) {
217+
return $s->getContent();
218+
});
219+
$handlers->add('p', function(ProcessedShortcode $s) use(&$handlers) {
220+
$parser = new RegexParser();
221+
$processor = new Processor($parser, $handlers);
222+
$shortcodes = $parser->parse($s->getOriginalContent());
223+
224+
return array_reduce($shortcodes, function($result, ParsedShortcodeInterface $s) use($processor) {
225+
return $result.$processor->process($s->getText());
226+
}, '');
227+
});
228+
$processor = new Processor(new RegexParser(), $handlers);
229+
230+
$this->assertSame('x ab y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
231+
$this->assertSame('x ab c y', $processor->process('x [p] [q]a[/q] [q]b [q]c[/q][/q] [/p] y'));
232+
}
233+
234+
public function testOriginalContent()
235+
{
236+
$handlers = new HandlerContainer();
237+
$handlers->add('p', function(ProcessedShortcode $s) { return $s->getOriginalContent(); });
238+
$handlers->addAlias('q', 'p');
239+
$processor = new Processor(new RegexParser(), $handlers);
240+
241+
$this->assertSame('x [q]a[/q] [q]b[/q] y', $processor->process('x [p] [q]a[/q] [q]b[/q] [/p] y'));
242+
}
243+
244+
public function testMultipleParent()
245+
{
246+
$parents = 0;
247+
$handlers = new HandlerContainer();
248+
$handlers->add('p', function(ProcessedShortcode $s) use(&$parents) { $parents += $s->getParent() ? 1 : 0; });
249+
$handlers->addAlias('q', 'p');
250+
$processor = new Processor(new RegexParser(), $handlers);
251+
$processor->process('x [p] [q]a[/q] [q]b[/q] [q]c[/q] [/p] y');
252+
253+
$this->assertSame(3, $parents);
254+
}
255+
211256
public function testPreventInfiniteLoop()
212257
{
213258
$handlers = new HandlerContainer();

0 commit comments

Comments
 (0)