Fixed twig script/style tag {% script 'file.js' at 'bottom' %}, replaces broken in operator [#3084]

This commit is contained in:
Matias Griese
2020-12-11 14:29:13 +02:00
parent cedcc845d5
commit b6c941fc3e
3 changed files with 39 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
* Fixed `Argument 1 passed to Grav\Common\User\DataUser\User::filterUsername() must be of the type string` [#3101](https://github.com/getgrav/grav/issues/3101)
* Fixed broken check if php exif module is enabled in `ImageFile::fixOrientation()`
* Fixed `StaticResizeTrait::resize()` bad image height/width attributes if `null` values are passed to the method
* Fixed twig script/style tag `{% script 'file.js' at 'bottom' %}`, replaces broken `in` operator [#3084](https://github.com/getgrav/grav/issues/3084)
# v1.7.0-rc.19
## 12/02/2020

View File

@@ -17,9 +17,9 @@ use Twig\TokenParser\AbstractTokenParser;
/**
* Adds a script to head/bottom/custom location in the document.
*
* {% script 'theme://js/something.js' in 'bottom' priority: 20 with { defer: true, async: true } %}
* {% script 'theme://js/something.js' at 'bottom' priority: 20 with { defer: true, async: true } %}
*
* {% script in 'bottom' priority: 20 %}
* {% script at 'bottom' priority: 20 %}
* alert('Warning!');
* {% endscript %}
@@ -57,13 +57,29 @@ class TwigTokenParserScript extends AbstractTokenParser
{
$stream = $this->parser->getStream();
// Look for deprecated {% script ... in ... %}
if (!$stream->test(Token::BLOCK_END_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in')) {
$i = 0;
do {
$token = $stream->look(++$i);
if ($token->test(Token::BLOCK_END_TYPE)) {
break;
}
if ($token->test(Token::OPERATOR_TYPE, 'in') && $stream->look($i+1)->test(Token::STRING_TYPE)) {
user_error("Twig: Using {% script ... in ... %} is deprecated, use {% script ... at ... %} instead", E_USER_DEPRECATED);
break;
}
} while (true);
}
$file = null;
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE) && !$stream->test(Token::BLOCK_END_TYPE)) {
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in') && !$stream->test(Token::BLOCK_END_TYPE)) {
$file = $this->parser->getExpressionParser()->parseExpression();
}
$group = null;
if ($stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
if ($stream->nextIf(Token::NAME_TYPE, 'at') || $stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
$group = $this->parser->getExpressionParser()->parseExpression();
}

View File

@@ -56,13 +56,29 @@ class TwigTokenParserStyle extends AbstractTokenParser
{
$stream = $this->parser->getStream();
// Look for deprecated {% style ... in ... %}
if (!$stream->test(Token::BLOCK_END_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in')) {
$i = 0;
do {
$token = $stream->look(++$i);
if ($token->test(Token::BLOCK_END_TYPE)) {
break;
}
if ($token->test(Token::OPERATOR_TYPE, 'in') && $stream->look($i+1)->test(Token::STRING_TYPE)) {
user_error("Twig: Using {% style ... in ... %} is deprecated, use {% style ... at ... %} instead", E_USER_DEPRECATED);
break;
}
} while (true);
}
$file = null;
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE) && !$stream->test(Token::BLOCK_END_TYPE)) {
if (!$stream->test(Token::NAME_TYPE) && !$stream->test(Token::OPERATOR_TYPE, 'in') && !$stream->test(Token::BLOCK_END_TYPE)) {
$file = $this->parser->getExpressionParser()->parseExpression();
}
$group = null;
if ($stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
if ($stream->nextIf(Token::NAME_TYPE, 'at') || $stream->nextIf(Token::OPERATOR_TYPE, 'in')) {
$group = $this->parser->getExpressionParser()->parseExpression();
}