mirror of
https://github.com/twigphp/Twig.git
synced 2026-10-05 11:27:26 +00:00
Discard what the non-selected branch of a guard tag registers on the parser
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
* Reject a deprecated `Template` instance created by another environment in `Environment::resolveTemplate()`
|
||||
* Fix a compilation error inside an `autoescape` tag leaking its escaping strategy into the next compiled template
|
||||
* Fix the non-selected branch of a `guard` tag registering its parent, blocks, and macros
|
||||
* Speed up reading object attributes backed by getters or class constants
|
||||
* Fix the sandbox not reporting the line of a rejected `guard` tag
|
||||
|
||||
|
||||
@@ -159,14 +159,19 @@ class Parser
|
||||
return $this->ignoreUnknownTwigCallables;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a discarded part of the template: what it registers (parent, blocks, macros, ...) is discarded as well.
|
||||
*/
|
||||
public function subparseIgnoreUnknownTwigCallables($test, bool $dropNeedle = false): void
|
||||
{
|
||||
$previous = $this->ignoreUnknownTwigCallables;
|
||||
$state = [$this->parent, $this->blocks, $this->documentationTargets, $this->macros, $this->importedSymbols, $this->traits, $this->embeddedTemplates];
|
||||
$this->ignoreUnknownTwigCallables = true;
|
||||
try {
|
||||
$this->subparse($test, $dropNeedle);
|
||||
} finally {
|
||||
$this->ignoreUnknownTwigCallables = $previous;
|
||||
[$this->parent, $this->blocks, $this->documentationTargets, $this->macros, $this->importedSymbols, $this->traits, $this->embeddedTemplates] = $state;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,11 @@ final class GuardTokenParser extends AbstractTokenParser
|
||||
$else = new EmptyNode();
|
||||
if ('else' === $stream->next()->getValue()) {
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
$else = $this->parser->subparse([$this, 'decideGuardEnd'], true);
|
||||
if ($exists) {
|
||||
$this->parser->subparseIgnoreUnknownTwigCallables([$this, 'decideGuardEnd'], true);
|
||||
} else {
|
||||
$else = $this->parser->subparse([$this, 'decideGuardEnd'], true);
|
||||
}
|
||||
}
|
||||
$stream->expect(Token::BLOCK_END_TYPE);
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
--TEST--
|
||||
"guard" ignores the blocks and macros defined in the discarded branch
|
||||
--TEMPLATE--
|
||||
{% guard function constant %}{% else %}{% block discarded %}{% endblock %}{% macro discarded() %}{% endmacro %}{% endguard %}
|
||||
{{ block('discarded') is defined ? 'NEVER' : 'no block' }} {{ _self.discarded is defined ? 'NEVER' : 'no macro' }}
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
no block no macro
|
||||
@@ -0,0 +1,15 @@
|
||||
--TEST--
|
||||
"guard" ignores the parent defined in the discarded branch
|
||||
--DEPRECATION--
|
||||
Since twig/twig 3.28: Using the "extends" tag outside the root of a template is deprecated in index.twig at line 2.
|
||||
--TEMPLATE--
|
||||
{% guard function foobar %}{% extends "never.twig" %}{% else %}{% extends "layout.twig" %}{% endguard %}
|
||||
{% block content %}content{% endblock %}
|
||||
--TEMPLATE(layout.twig)--
|
||||
layout {{ block('content') }}
|
||||
--TEMPLATE(never.twig)--
|
||||
NEVER
|
||||
--DATA--
|
||||
return []
|
||||
--EXPECT--
|
||||
layout content
|
||||
Reference in New Issue
Block a user