From c298b787f79aa3531502b7f7caacff17634576ac Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 27 Nov 2026 15:29:03 +0000 Subject: [PATCH] Fix blank text nested in tags outside of blocks in a child template being rendered --- CHANGELOG | 2 +- src/Parser.php | 27 +++++++++++++ .../blank_text_nested_outside_blocks.test | 34 ++++++++++++++++ tests/ParserTest.php | 39 +++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/tags/inheritance/blank_text_nested_outside_blocks.test diff --git a/CHANGELOG b/CHANGELOG index 868a802124d..67a62817b60 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.31.2 (2026-XX-XX) - * n/a + * Fix blank text nested in tags outside of blocks in a child template being rendered # 3.31.1 (2026-11-24) diff --git a/src/Parser.php b/src/Parser.php index 05d1cf22c41..c3967e6fd30 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -30,6 +30,7 @@ use Twig\Node\MacrosNode; use Twig\Node\ModuleNode; use Twig\Node\Node; +use Twig\Node\NodeCaptureInterface; use Twig\Node\NodeDocumentation; use Twig\Node\Nodes; use Twig\Node\PrintNode; @@ -622,6 +623,12 @@ private function cleanupBodyForChildTemplates(Node $body): Node if ($body instanceof TextNode && $body->isBlank()) { return new EmptyNode(); } + if (!$body instanceof Nodes && Node::class !== $body::class) { + // a single root node (see CorrectnessNodeVisitor::getRootNodes()) + $this->replaceNestedBlankTextNodes($body); + + return $body; + } foreach ($body as $k => $node) { if ($node instanceof BlockReferenceNode) { @@ -630,12 +637,32 @@ private function cleanupBodyForChildTemplates(Node $body): Node } elseif ($node instanceof TextNode && $node->isBlank()) { // remove nodes considered as "empty" $body->removeNode($k); + } else { + $this->replaceNestedBlankTextNodes($node); } } return $body; } + /** + * Replaces (instead of removing) blank text to keep the structure of nodes like "if". + */ + private function replaceNestedBlankTextNodes(Node $node): void + { + if ($node instanceof NodeCaptureInterface || $node instanceof BlockReferenceNode) { + return; + } + + foreach ($node as $k => $n) { + if ($n instanceof TextNode && $n->isBlank()) { + $node->setNode((string) $k, new EmptyNode($n->getTemplateLine())); + } else { + $this->replaceNestedBlankTextNodes($n); + } + } + } + private function checkPrecedenceDeprecations(ExpressionParserInterface $expressionParser, AbstractExpression $expr): void { $this->expressionRefs[$expr] = $expressionParser; diff --git a/tests/Fixtures/tags/inheritance/blank_text_nested_outside_blocks.test b/tests/Fixtures/tags/inheritance/blank_text_nested_outside_blocks.test new file mode 100644 index 00000000000..211ab880548 --- /dev/null +++ b/tests/Fixtures/tags/inheritance/blank_text_nested_outside_blocks.test @@ -0,0 +1,34 @@ +--TEST-- +"extends" tag does not output blank text nested in tags outside of blocks +--TEMPLATE-- +[{{ include('child.twig') }}] +[{% embed 'layout.twig' %} +{% if true %} + {% set foo = 'embed' %} +{% endif %} +{% block content %}{{ foo }}{% endblock %} +{% endembed %}] +--TEMPLATE(child.twig)-- +{% extends 'layout.twig' %} +{% for i in [1, 2] %} + {% if i %} + {% set unused = i %} + {% endif %} +{% endfor %} +{% with {a: 1} %} + {% set unused = 1 %} +{% endwith %} +{% if true %} {% endif %} +{% if true %} + {% set foo %} {{ 'bar' }} {% endset %} +{% endif %} +{% block content %}{{ foo }}{% endblock %} +--TEMPLATE(layout.twig)-- +{% block content %}{% endblock %} +--DATA-- +return [] +--EXPECT-- +[ + bar ] +[ +embed] diff --git a/tests/ParserTest.php b/tests/ParserTest.php index c38dc98b8d9..756cc5a312f 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -418,6 +418,29 @@ public function testCleanupBodyForChildTemplatesWithASingleNodeBody(): void $this->assertInstanceOf(EmptyNode::class, $body); } + public function testCleanupBodyForChildTemplatesReplacesNestedBlankText(): void + { + $twig = new Environment(new ArrayLoader()); + $node = $twig->parse($twig->tokenize(new Source('{% extends "base" %}{% if true %} {% set foo %} {{ bar }}{% endset %}{% endif %}', 'index'))); + + $if = $node->getNode('body')->getNode('0')->getNode('1'); + $this->assertInstanceOf(IfNode::class, $if); + $this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1')->getNode('0')); + $this->assertInstanceOf(TextNode::class, $if->getNode('tests')->getNode('1')->getNode('1')->getNode('values')->getNode('body')->getNode('0')); + } + + public function testCleanupBodyForChildTemplatesReplacesNestedBlankTextInASingleNodeBody(): void + { + $twig = new Environment(new ArrayLoader()); + $twig->addTokenParser(new ParentSettingInConditionTokenParser()); + + $node = $twig->parse($twig->tokenize(new Source('{% set_parent_in_condition %}', 'index'))); + + $if = $node->getNode('body')->getNode('0'); + $this->assertInstanceOf(IfNode::class, $if); + $this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1')); + } + public function testCleanupBodyAcceptsAnUnregisteredBlockReference(): void { $twig = new Environment(new ArrayLoader()); @@ -771,3 +794,19 @@ public function getTag(): string return 'set_parent'; } } + +class ParentSettingInConditionTokenParser extends AbstractTokenParser +{ + public function parse(Token $token): Node + { + $this->parser->setParent(new ConstantExpression('base', $token->getLine()), false); + $this->parser->getStream()->expect(Token::BLOCK_END_TYPE); + + return new IfNode(new Nodes([new ConstantExpression(true, $token->getLine()), new TextNode(' ', $token->getLine())]), null, $token->getLine()); + } + + public function getTag(): string + { + return 'set_parent_in_condition'; + } +}