@@ -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