Skip to content

Commit

Permalink
bug #3738 do not clean up whitespace text nodes inside if tags (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

do not clean up whitespace text nodes inside if tags

Commits
-------

c8ec092 do not clean up whitespace text nodes inside if tags
  • Loading branch information
fabpot committed Dec 26, 2022
2 parents 6cd1473 + c8ec092 commit 7cbabe4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Node/IfNode.php
Expand Up @@ -50,8 +50,11 @@ public function compile(Compiler $compiler)
->subcompile($this->getNode('tests')->getNode($i))
->raw(") {\n")
->indent()
->subcompile($this->getNode('tests')->getNode($i + 1))
;
// The node might not exists if the content is empty
if ($this->getNode('tests')->hasNode($i + 1)) {
$compiler->subcompile($this->getNode('tests')->getNode($i + 1));
}
}

if ($this->hasNode('else')) {
Expand Down
32 changes: 32 additions & 0 deletions tests/Fixtures/tags/if/empty_body.test
@@ -0,0 +1,32 @@
--TEST--
empty "if" body in child template
--TEMPLATE--
{% extends 'base.twig' %}

{% set foo = '' %}

{% if a is defined %}

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

{% if a is defined %}

{% endif %}

{% if a is defined %}
{% set foo = 'NOTHING' %}
{% else %}

{% endif %}

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

0 comments on commit 7cbabe4

Please sign in to comment.