diff --git a/src/Parser/RegularParser.php b/src/Parser/RegularParser.php index b32b3d5..1e72257 100644 --- a/src/Parser/RegularParser.php +++ b/src/Parser/RegularParser.php @@ -158,6 +158,7 @@ private function shortcode(array &$names) $this->beginBacktrack(); // @mago-expect analysis:possibly-invalid-argument + /** @psalm-suppress ArgumentTypeCoercion */ $contentMatchedShortcodes = $this->shortcode($names); if(\is_string($contentMatchedShortcodes)) { $closingName = $contentMatchedShortcodes; @@ -414,6 +415,11 @@ private function prepareLexer(SyntaxInterface $syntax) return preg_quote((string)$text, '~'); }; + // a character that starts an opening tag is text when another opening tag starts before it ends, + // so in a run like `[[[a]]` for `[[` the opening tag token is the last one, right before the name + $length = mb_strlen($syntax->getOpeningTag(), 'utf-8'); + $overlap = $length > 1 ? '|(?='.$quote($syntax->getOpeningTag()).').(?=.{0,'.($length - 2).'}'.$quote($syntax->getOpeningTag()).')' : ''; + $rules = array( '(?\\\\.|(?:(?!'.implode('|', array( $quote($syntax->getOpeningTag()), @@ -423,7 +429,7 @@ private function prepareLexer(SyntaxInterface $syntax) $quote($syntax->getParameterValueDelimiter()), '\\\\', '\s+', - )).').)++)', + )).').'.$overlap.')++)', '(?\s+)', $group($syntax->getClosingTagMarker(), 'marker'), $group($syntax->getParameterValueDelimiter(), 'delimiter'), diff --git a/tests/ParserTest.php b/tests/ParserTest.php index 30c1da2..7cadcaa 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -388,6 +388,56 @@ public function testHybridParserSkipsEscapedTags() } } + /** + * @dataProvider provideRunsOfOpeningTags + */ + #[DataProvider('provideRunsOfOpeningTags')] + public function testParsersFindTagsInsideRunsOfOpeningTags(ParserInterface $parser, $code, array $expected) + { + $this->assertShortcodes($parser->parse($code), $expected); + } + + public static function provideRunsOfOpeningTags() + { + $double = new Syntax('[[', ']]', '//', '==', '""'); + $curly = new Syntax('{{', '}}', '/', '=', '"'); + $cases = array( + array($double, '[[[a]]', array( + new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 1), + )), + array($double, '[[a]][[[b]]', array( + new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 0), + new ParsedShortcode(new Shortcode('b', array(), null, null), '[[b]]', 6), + )), + array($double, '[[a]]x[[[//a]]', array( + new ParsedShortcode(new Shortcode('a', array(), 'x[', null), '[[a]]x[[[//a]]', 0), + )), + array($double, '[[[[a]]', array( + new ParsedShortcode(new Shortcode('a', array(), null, null), '[[a]]', 2), + )), + array($curly, 'a{{{x}}', array( + new ParsedShortcode(new Shortcode('x', array(), null, null), '{{x}}', 2), + )), + array($curly, '{{x}}{{{/x}}', array( + new ParsedShortcode(new Shortcode('x', array(), '{', null), '{{x}}{{{/x}}', 0), + )), + array($curly, '{{a k="{{"}}', array( + new ParsedShortcode(new Shortcode('a', array('k' => '{{'), null, null), '{{a k="{{"}}', 0), + )), + array(new Syntax('««', '»»', '/', '=', '"'), 'ż«««x»»', array( + new ParsedShortcode(new Shortcode('x', array(), null, null), '««x»»', 2), + )), + ); + + $result = array(); + foreach($cases as $case) { + $result[] = array(new RegularParser($case[0]), $case[1], $case[2]); + $result[] = array(new HybridParser($case[0]), $case[1], $case[2]); + } + + return $result; + } + public function testIssue119() { $cases = array(