mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-05 06:56:51 +00:00
Merge branch '3.x' into 4.x
* 3.x:
Fix the empty comment "{##}" being lexed as a documentation comment opening
This commit is contained in:
+5
-1
@@ -111,6 +111,10 @@ class Lexer
|
||||
|
||||
$this->isInitialized = true;
|
||||
|
||||
// when the comment closing tag starts with "#" (as "#}" does), the empty comment ("{##}")
|
||||
// starts like a documentation comment ("{##"); make sure it keeps lexing as a comment
|
||||
$emptyCommentLookahead = str_starts_with($this->options['tag_comment'][1], '#') ? '(?!'.preg_quote(substr($this->options['tag_comment'][1], 1), '#').')' : '';
|
||||
|
||||
$this->regexes = [
|
||||
// }}
|
||||
'lex_var' => '{
|
||||
@@ -202,7 +206,7 @@ class Lexer
|
||||
'|'.
|
||||
preg_quote($this->options['tag_block'][0]). // {%
|
||||
'|'.
|
||||
preg_quote($this->options['tag_comment'][0].'#', '#'). // {##
|
||||
preg_quote($this->options['tag_comment'][0].'#', '#').$emptyCommentLookahead. // {## (but not the empty comment {##})
|
||||
'|'.
|
||||
preg_quote($this->options['tag_comment'][0], '#'). // {#
|
||||
')('.
|
||||
|
||||
@@ -318,6 +318,19 @@ TWIG, 'index')));
|
||||
$this->assertNull($body->getNode('4')->getDocumentation());
|
||||
}
|
||||
|
||||
public function testEmptyCommentIsNotLexedAsDocumentation(): void
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(), ['autoescape' => false]);
|
||||
$module = $twig->parse($twig->tokenize(new Source(<<<'TWIG'
|
||||
{##}{{ answer }}
|
||||
{% block a %}x{##}{% endblock %}{% block b %}y{% endblock %}
|
||||
TWIG, 'index')));
|
||||
$body = $module->getNode('body')->getNode('0');
|
||||
|
||||
$this->assertInstanceOf(PrintNode::class, $body->getNode('0'));
|
||||
$this->assertNull($body->getNode('0')->getDocumentation());
|
||||
}
|
||||
|
||||
public function testInlineDocumentationBeforeATagNameIsIgnored(): void
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader(), ['autoescape' => false]);
|
||||
|
||||
Reference in New Issue
Block a user