Skip to content

Commit

Permalink
do not clean up whitespace text nodes inside if tags
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh authored and fabpot committed Dec 26, 2022
1 parent 6cd1473 commit 79d5c24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Parser.php
Expand Up @@ -17,6 +17,7 @@
use Twig\Node\BlockReferenceNode;
use Twig\Node\BodyNode;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\IfNode;
use Twig\Node\MacroNode;
use Twig\Node\ModuleNode;
use Twig\Node\Node;
Expand Down Expand Up @@ -381,6 +382,10 @@ private function filterBodyNodes(Node $node, bool $nested = false)
// we need to discard the wrapping "Twig_Node" for the "body" node
$nested = $nested || ('Twig_Node' !== \get_class($node) && Node::class !== \get_class($node));
foreach ($node as $k => $n) {
if ($n instanceof IfNode) {
continue;
}

if (null !== $n && null === $this->filterBodyNodes($n, $nested)) {
$node->removeNode($k);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Fixtures/tags/if/empty_body.test
@@ -0,0 +1,22 @@
--TEST--
empty "if" body in child template
--TEMPLATE--
{% extends 'base.twig' %}

{% set foo = '' %}

{% if a is defined %}

{% else %}
{% set foo = 'NOTHING' %}
{% endif %}

{% block content %}
{{ foo }}
{% endblock %}
--TEMPLATE(base.twig)--
{% block content %}{% endblock %}
--DATA--
return []
--EXPECT--
NOTHING

0 comments on commit 79d5c24

Please sign in to comment.