diff --git a/CHANGELOG b/CHANGELOG index 481866a888..d6bf5cbd45 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.17.1 (2024-XX-XX) - * n/a + * Fix the Elvis operator when used as '? :' instead of '?:' # 3.17.0 (2024-12-10) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 567c7e6771..b60944c4a4 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -321,6 +321,7 @@ public function getOperators(): array '+' => ['precedence' => 500, 'class' => PosUnary::class], ], [ + '? :' => ['precedence' => 5, 'class' => ElvisBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT], '?:' => ['precedence' => 5, 'class' => ElvisBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT], '??' => ['precedence' => 300, 'precedence_change' => new OperatorPrecedenceChange('twig/twig', '3.15', 5), 'class' => NullCoalesceBinary::class, 'associativity' => ExpressionParser::OPERATOR_RIGHT], 'or' => ['precedence' => 10, 'class' => OrBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT], diff --git a/tests/Fixtures/expressions/ternary_operator_nothen.test b/tests/Fixtures/expressions/ternary_operator_nothen.test index ecd6b75465..53f8c0b3ca 100644 --- a/tests/Fixtures/expressions/ternary_operator_nothen.test +++ b/tests/Fixtures/expressions/ternary_operator_nothen.test @@ -3,8 +3,16 @@ Twig supports the ternary operator --TEMPLATE-- {{ 'YES' ?: 'NO' }} {{ 0 ?: 'NO' }} +{{ 'YES' ? : 'NO' }} +{{ 0 ? : 'NO' }} +{{ 'YES' ? : 'NO' }} +{{ 0 ? : 'NO' }} --DATA-- return [] --EXPECT-- YES NO +YES +NO +YES +NO