Skip to content

Commit c298b78

Browse files
committed
Fix blank text nested in tags outside of blocks in a child template being rendered
1 parent 6a8d315 commit c298b78

4 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎CHANGELOG‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 3.31.2 (2026-XX-XX)
22

3-
* n/a
3+
* Fix blank text nested in tags outside of blocks in a child template being rendered
44

55
# 3.31.1 (2026-11-24)
66

‎src/Parser.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Twig\Node\MacrosNode;
3131
use Twig\Node\ModuleNode;
3232
use Twig\Node\Node;
33+
use Twig\Node\NodeCaptureInterface;
3334
use Twig\Node\NodeDocumentation;
3435
use Twig\Node\Nodes;
3536
use Twig\Node\PrintNode;
@@ -622,6 +623,12 @@ private function cleanupBodyForChildTemplates(Node $body): Node
622623
if ($body instanceof TextNode && $body->isBlank()) {
623624
return new EmptyNode();
624625
}
626+
if (!$body instanceof Nodes && Node::class !== $body::class) {
627+
// a single root node (see CorrectnessNodeVisitor::getRootNodes())
628+
$this->replaceNestedBlankTextNodes($body);
629+
630+
return $body;
631+
}
625632

626633
foreach ($body as $k => $node) {
627634
if ($node instanceof BlockReferenceNode) {
@@ -630,12 +637,32 @@ private function cleanupBodyForChildTemplates(Node $body): Node
630637
} elseif ($node instanceof TextNode && $node->isBlank()) {
631638
// remove nodes considered as "empty"
632639
$body->removeNode($k);
640+
} else {
641+
$this->replaceNestedBlankTextNodes($node);
633642
}
634643
}
635644

636645
return $body;
637646
}
638647

648+
/**
649+
* Replaces (instead of removing) blank text to keep the structure of nodes like "if".
650+
*/
651+
private function replaceNestedBlankTextNodes(Node $node): void
652+
{
653+
if ($node instanceof NodeCaptureInterface || $node instanceof BlockReferenceNode) {
654+
return;
655+
}
656+
657+
foreach ($node as $k => $n) {
658+
if ($n instanceof TextNode && $n->isBlank()) {
659+
$node->setNode((string) $k, new EmptyNode($n->getTemplateLine()));
660+
} else {
661+
$this->replaceNestedBlankTextNodes($n);
662+
}
663+
}
664+
}
665+
639666
private function checkPrecedenceDeprecations(ExpressionParserInterface $expressionParser, AbstractExpression $expr): void
640667
{
641668
$this->expressionRefs[$expr] = $expressionParser;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
"extends" tag does not output blank text nested in tags outside of blocks
3+
--TEMPLATE--
4+
[{{ include('child.twig') }}]
5+
[{% embed 'layout.twig' %}
6+
{% if true %}
7+
{% set foo = 'embed' %}
8+
{% endif %}
9+
{% block content %}{{ foo }}{% endblock %}
10+
{% endembed %}]
11+
--TEMPLATE(child.twig)--
12+
{% extends 'layout.twig' %}
13+
{% for i in [1, 2] %}
14+
{% if i %}
15+
{% set unused = i %}
16+
{% endif %}
17+
{% endfor %}
18+
{% with {a: 1} %}
19+
{% set unused = 1 %}
20+
{% endwith %}
21+
{% if true %} {% endif %}
22+
{% if true %}
23+
{% set foo %} {{ 'bar' }} {% endset %}
24+
{% endif %}
25+
{% block content %}{{ foo }}{% endblock %}
26+
--TEMPLATE(layout.twig)--
27+
<!DOCTYPE html>{% block content %}{% endblock %}
28+
--DATA--
29+
return []
30+
--EXPECT--
31+
[
32+
<!DOCTYPE html> bar ]
33+
[
34+
<!DOCTYPE html>embed]

‎tests/ParserTest.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,29 @@ public function testCleanupBodyForChildTemplatesWithASingleNodeBody(): void
418418
$this->assertInstanceOf(EmptyNode::class, $body);
419419
}
420420

421+
public function testCleanupBodyForChildTemplatesReplacesNestedBlankText(): void
422+
{
423+
$twig = new Environment(new ArrayLoader());
424+
$node = $twig->parse($twig->tokenize(new Source('{% extends "base" %}{% if true %} {% set foo %} {{ bar }}{% endset %}{% endif %}', 'index')));
425+
426+
$if = $node->getNode('body')->getNode('0')->getNode('1');
427+
$this->assertInstanceOf(IfNode::class, $if);
428+
$this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1')->getNode('0'));
429+
$this->assertInstanceOf(TextNode::class, $if->getNode('tests')->getNode('1')->getNode('1')->getNode('values')->getNode('body')->getNode('0'));
430+
}
431+
432+
public function testCleanupBodyForChildTemplatesReplacesNestedBlankTextInASingleNodeBody(): void
433+
{
434+
$twig = new Environment(new ArrayLoader());
435+
$twig->addTokenParser(new ParentSettingInConditionTokenParser());
436+
437+
$node = $twig->parse($twig->tokenize(new Source('{% set_parent_in_condition %}', 'index')));
438+
439+
$if = $node->getNode('body')->getNode('0');
440+
$this->assertInstanceOf(IfNode::class, $if);
441+
$this->assertInstanceOf(EmptyNode::class, $if->getNode('tests')->getNode('1'));
442+
}
443+
421444
public function testCleanupBodyAcceptsAnUnregisteredBlockReference(): void
422445
{
423446
$twig = new Environment(new ArrayLoader());
@@ -771,3 +794,19 @@ public function getTag(): string
771794
return 'set_parent';
772795
}
773796
}
797+
798+
class ParentSettingInConditionTokenParser extends AbstractTokenParser
799+
{
800+
public function parse(Token $token): Node
801+
{
802+
$this->parser->setParent(new ConstantExpression('base', $token->getLine()), false);
803+
$this->parser->getStream()->expect(Token::BLOCK_END_TYPE);
804+
805+
return new IfNode(new Nodes([new ConstantExpression(true, $token->getLine()), new TextNode(' ', $token->getLine())]), null, $token->getLine());
806+
}
807+
808+
public function getTag(): string
809+
{
810+
return 'set_parent_in_condition';
811+
}
812+
}

0 commit comments

Comments
 (0)