Skip to content

Commit 8c3855b

Browse files
committed
RegularParser now finds tags inside runs of opening tags
1 parent 2f353d9 commit 8c3855b

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎src/Parser/RegularParser.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private function shortcode(array &$names)
158158

159159
$this->beginBacktrack();
160160
// @mago-expect analysis:possibly-invalid-argument
161+
/** @psalm-suppress ArgumentTypeCoercion */
161162
$contentMatchedShortcodes = $this->shortcode($names);
162163
if(\is_string($contentMatchedShortcodes)) {
163164
$closingName = $contentMatchedShortcodes;
@@ -414,6 +415,11 @@ private function prepareLexer(SyntaxInterface $syntax)
414415
return preg_quote((string)$text, '~');
415416
};
416417

418+
// a character that starts an opening tag is text when another opening tag starts before it ends,
419+
// so in a run like `[[[a]]` for `[[` the opening tag token is the last one, right before the name
420+
$length = mb_strlen($syntax->getOpeningTag(), 'utf-8');
421+
$overlap = $length > 1 ? '|(?='.$quote($syntax->getOpeningTag()).').(?=.{0,'.($length - 2).'}'.$quote($syntax->getOpeningTag()).')' : '';
422+
417423
$rules = array(
418424
'(?<string>\\\\.|(?:(?!'.implode('|', array(
419425
$quote($syntax->getOpeningTag()),
@@ -423,7 +429,7 @@ private function prepareLexer(SyntaxInterface $syntax)
423429
$quote($syntax->getParameterValueDelimiter()),
424430
'\\\\',
425431
'\s+',
426-
)).').)++)',
432+
)).').'.$overlap.')++)',
427433
'(?<ws>\s+)',
428434
$group($syntax->getClosingTagMarker(), 'marker'),
429435
$group($syntax->getParameterValueDelimiter(), 'delimiter'),

‎tests/ParserTest.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,56 @@ public function testHybridParserSkipsEscapedTags()
388388
}
389389
}
390390

391+
/**
392+
* @dataProvider provideRunsOfOpeningTags
393+
*/
394+
#[DataProvider('provideRunsOfOpeningTags')]
395+
public function testParsersFindTagsInsideRunsOfOpeningTags(ParserInterface $parser, $code, array $expected)
396+
{
397+
$this->assertShortcodes($parser->parse($code), $expected);
398+
}
399+
400+
public static function provideRunsOfOpeningTags()
401+
{
402+
$double = new Syntax('[[', ']]', '//', '==', '""');
403+
$curly = new Syntax('{{', '}}', '/', '=', '"');
404+
$cases = array(
405+
array($double, '[[[a]]', array(
406+
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 1),
407+
)),
408+
array($double, '[[a]][[[b]]', array(
409+
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 0),
410+
new ParsedShortcode(new Shortcode('b', array(), null, null), '[[b]]', 6),
411+
)),
412+
array($double, '[[a]]x[[[//a]]', array(
413+
new ParsedShortcode(new Shortcode('a', array(), 'x[', null), '[[a]]x[[[//a]]', 0),
414+
)),
415+
array($double, '[[[[a]]', array(
416+
new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 2),
417+
)),
418+
array($curly, 'a{{{x}}', array(
419+
new ParsedShortcode(new Shortcode('x', array(), null, null), '{{x}}', 2),
420+
)),
421+
array($curly, '{{x}}{{{/x}}', array(
422+
new ParsedShortcode(new Shortcode('x', array(), '{', null), '{{x}}{{{/x}}', 0),
423+
)),
424+
array($curly, '{{a k="{{"}}', array(
425+
new ParsedShortcode(new Shortcode('a', array('k' => '{{'), null, null), '{{a k="{{"}}', 0),
426+
)),
427+
array(new Syntax('««', '»»', '/', '=', '"'), 'ż«««x»»', array(
428+
new ParsedShortcode(new Shortcode('x', array(), null, null), '««x»»', 2),
429+
)),
430+
);
431+
432+
$result = array();
433+
foreach($cases as $case) {
434+
$result[] = array(new RegularParser($case[0]), $case[1], $case[2]);
435+
$result[] = array(new HybridParser($case[0]), $case[1], $case[2]);
436+
}
437+
438+
return $result;
439+
}
440+
391441
public function testIssue119()
392442
{
393443
$cases = array(

0 commit comments

Comments
 (0)