Fix ambiguous syntax parsing

This commit is contained in:
Fabien Potencier
2020-02-21 08:29:50 +01:00
parent 1325da4c85
commit 5724cbeddf
4 changed files with 25 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# 1.42.6 (2020-XX-XX)
* n/a
* Fix ambiguous syntax parsing
# 1.42.5 (2020-02-11)
+7 -3
View File
@@ -500,10 +500,14 @@ class Lexer implements \Twig_LexerInterface
foreach ($operators as $operator => $length) {
// an operator that ends with a character must be followed by
// a whitespace or a parenthesis
$r = preg_quote($operator, '/');
if (ctype_alpha($operator[$length - 1])) {
$r = preg_quote($operator, '/').'(?=[\s()])';
} else {
$r = preg_quote($operator, '/');
$r .= '(?=[\s()])';
}
// an operator that begins with a character must have a space before
if (ctype_alpha($operator[0])) {
$r = '(?<=\s)'.$r;
}
// an operator with a space can be any amount of whitespaces
+11 -1
View File
@@ -57,8 +57,13 @@ Twig supports the in operator
{{ safe in ['foo', 'bar'] ? 'OK' : 'KO' }}
{{ 'fo' in safe ? 'OK' : 'KO' }}
{{ foo.not in ['not'] ? 'OK' : 'KO' }}
{{ 'value'|not in ['not value'] ? 'OK' : 'KO' }}
{{ foo.not not in ['not'] ? 'KO' : 'OK' }}
{{ 'value'|not not in ['not value'] ? 'KO' : 'OK' }}
--DATA--
return ['bar' => 'bar', 'foo' => ['bar' => 'bar'], 'dir_object' => new \SplFileInfo(dirname(__FILE__)), 'object' => new \stdClass(), 'resource' => opendir(dirname(__FILE__)), 'safe' => new \Twig\Markup('foo', 'UTF-8')]
return ['bar' => 'bar', 'foo' => ['bar' => 'bar', 'not' => 'not'], 'dir_object' => new \SplFileInfo(dirname(__FILE__)), 'object' => new \stdClass(), 'resource' => opendir(dirname(__FILE__)), 'safe' => new \Twig\Markup('foo', 'UTF-8')]
--EXPECT--
OK
OK
@@ -116,3 +121,8 @@ OK
OK
OK
OK
OK
OK
OK
+6
View File
@@ -164,6 +164,7 @@ class TwigTestExtension extends AbstractExtension
new TwigFilter('magic_call_array', ['Twig\Tests\TwigTestExtension', 'magicStaticCall']),
new TwigFilter('*_path', [$this, 'dynamic_path']),
new TwigFilter('*_foo_*_bar', [$this, 'dynamic_foo']),
new TwigFilter('not', [$this, 'notFilter']),
];
}
@@ -188,6 +189,11 @@ class TwigTestExtension extends AbstractExtension
];
}
public function notFilter($value)
{
return 'not '.$value;
}
public function §Filter($value)
{
return "§{$value}§";