mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-17 08:50:50 +00:00
Handle single-node child template bodies in cleanup
This commit is contained in:
+8
-2
@@ -121,7 +121,7 @@ class Parser
|
||||
}
|
||||
|
||||
if ($this->parent) {
|
||||
$this->cleanupBodyForChildTemplates($body);
|
||||
$body = $this->cleanupBodyForChildTemplates($body);
|
||||
}
|
||||
|
||||
$node = new ModuleNode(
|
||||
@@ -552,8 +552,12 @@ class Parser
|
||||
return $test;
|
||||
}
|
||||
|
||||
private function cleanupBodyForChildTemplates(Node $body): void
|
||||
private function cleanupBodyForChildTemplates(Node $body): Node
|
||||
{
|
||||
if ($body instanceof BlockReferenceNode || ($body instanceof TextNode && $body->isBlank())) {
|
||||
return new EmptyNode();
|
||||
}
|
||||
|
||||
foreach ($body as $k => $node) {
|
||||
if ($node instanceof BlockReferenceNode) {
|
||||
// as it has a parent, the block reference won't be used
|
||||
@@ -563,6 +567,8 @@ class Parser
|
||||
$body->removeNode($k);
|
||||
}
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
private function checkPrecedenceDeprecations(ExpressionParserInterface $expressionParser, AbstractExpression $expr)
|
||||
|
||||
@@ -171,6 +171,17 @@ EOF, 'index')));
|
||||
$this->assertSame('set', $body->getNode('4')->getNodeTag());
|
||||
}
|
||||
|
||||
public function testCleanupBodyForChildTemplatesWithASingleNodeBody()
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader());
|
||||
$twig->addTokenParser(new ParentSettingTokenParser());
|
||||
|
||||
$node = $twig->parse($twig->tokenize(new Source('{% set_parent %}', 'index')));
|
||||
|
||||
$body = $node->getNode('body')->getNode('0');
|
||||
$this->assertInstanceOf(EmptyNode::class, $body);
|
||||
}
|
||||
|
||||
public function testBodyForParentTemplates()
|
||||
{
|
||||
$twig = new Environment(new ArrayLoader());
|
||||
@@ -231,3 +242,20 @@ class TestTokenParser extends AbstractTokenParser
|
||||
return 'test';
|
||||
}
|
||||
}
|
||||
|
||||
class ParentSettingTokenParser 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);
|
||||
|
||||
// returns a blank text node so the whole child body is a single removable node
|
||||
return new TextNode(' ', $token->getLine());
|
||||
}
|
||||
|
||||
public function getTag(): string
|
||||
{
|
||||
return 'set_parent';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user