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:
Nicolas Grekas
2026-08-20 20:40:40 +02:00
2 changed files with 18 additions and 1 deletions
+5 -1
View File
@@ -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], '#'). // {#
')('.
+13
View File
@@ -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]);