From b6c941fc3edfbdd0a481ac4368f187af1075aa47 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 11 Dec 2020 14:29:13 +0200 Subject: [PATCH] Fixed twig script/style tag `{% script 'file.js' at 'bottom' %}`, replaces broken `in` operator [#3084] --- CHANGELOG.md | 1 + .../TokenParser/TwigTokenParserScript.php | 24 +++++++++++++++---- .../Twig/TokenParser/TwigTokenParserStyle.php | 20 ++++++++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db3209e0..db5a65e26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php b/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php index aaf1f9218..42c78fb21 100644 --- a/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php +++ b/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserScript.php @@ -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(); } diff --git a/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserStyle.php b/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserStyle.php index 12447cc34..0134491dd 100644 --- a/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserStyle.php +++ b/system/src/Grav/Common/Twig/TokenParser/TwigTokenParserStyle.php @@ -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(); }