From: Fabien Potencier Date: Mon, 10 Jan 2011 18:06:37 +0000 (+0100) Subject: fixed the ternary operator X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=ac9e4eec15e70ef43e50bf99fa3c2dab970e96fe;p=web%2Fkonrad%2Ftwig.git fixed the ternary operator --- diff --git a/CHANGELOG b/CHANGELOG index 1409f7b..e86bf8d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ * 1.0.0-RC2 -... +Changes: + + * fixed the ternary operator precedence rule * 1.0.0-RC1 (2011-01-09) diff --git a/lib/Twig/Node/Expression/Conditional.php b/lib/Twig/Node/Expression/Conditional.php index cefb9a2..edcb1e2 100644 --- a/lib/Twig/Node/Expression/Conditional.php +++ b/lib/Twig/Node/Expression/Conditional.php @@ -19,13 +19,13 @@ class Twig_Node_Expression_Conditional extends Twig_Node_Expression public function compile(Twig_Compiler $compiler) { $compiler - ->raw('(') + ->raw('((') ->subcompile($this->getNode('expr1')) ->raw(') ? (') ->subcompile($this->getNode('expr2')) ->raw(') : (') ->subcompile($this->getNode('expr3')) - ->raw(')') + ->raw('))') ; } } diff --git a/lib/Twig/Node/Expression/Filter.php b/lib/Twig/Node/Expression/Filter.php index 091606a..1979540 100644 --- a/lib/Twig/Node/Expression/Filter.php +++ b/lib/Twig/Node/Expression/Filter.php @@ -27,7 +27,7 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression // is a name (like obj) or an attribute (like obj.attr) // In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }} if ('default' === $name && ($this->getNode('node') instanceof Twig_Node_Expression_Name || $this->getNode('node') instanceof Twig_Node_Expression_GetAttr)) { - $compiler->raw('('); + $compiler->raw('(('); if ($this->getNode('node') instanceof Twig_Node_Expression_Name) { $testMap = $compiler->getEnvironment()->getTests(); $compiler @@ -40,11 +40,11 @@ class Twig_Node_Expression_Filter extends Twig_Node_Expression $compiler->subcompile($this->getNode('node')); } - $compiler->raw(' ? '); + $compiler->raw(') ? ('); $this->compileFilter($compiler, $filter); - $compiler->raw(' : '); + $compiler->raw(') : ('); $compiler->subcompile($this->getNode('arguments')->getNode(0)); - $compiler->raw(')'); + $compiler->raw('))'); } else { $this->compileFilter($compiler, $filter); } diff --git a/test/Twig/Tests/Node/Expression/ConditionalTest.php b/test/Twig/Tests/Node/Expression/ConditionalTest.php index f7cce28..4a05e4e 100644 --- a/test/Twig/Tests/Node/Expression/ConditionalTest.php +++ b/test/Twig/Tests/Node/Expression/ConditionalTest.php @@ -45,7 +45,7 @@ class Twig_Tests_Node_Expression_ConditionalTest extends Twig_Tests_Node_TestCas $expr2 = new Twig_Node_Expression_Constant(2, 0); $expr3 = new Twig_Node_Expression_Constant(3, 0); $node = new Twig_Node_Expression_Conditional($expr1, $expr2, $expr3, 0); - $tests[] = array($node, '(1) ? (2) : (3)'); + $tests[] = array($node, '((1) ? (2) : (3))'); return $tests; } diff --git a/test/Twig/Tests/Node/IncludeTest.php b/test/Twig/Tests/Node/IncludeTest.php index aa876ee..e174bf3 100644 --- a/test/Twig/Tests/Node/IncludeTest.php +++ b/test/Twig/Tests/Node/IncludeTest.php @@ -56,7 +56,7 @@ class Twig_Tests_Node_IncludeTest extends Twig_Tests_Node_TestCase ); $node = new Twig_Node_Include($expr, null, false, 0); $tests[] = array($node, <<env->loadTemplate(\$template); } diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index b0f94de..194bed0 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -141,7 +141,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template public function getParent(array \$context) { if (null === \$this->parent) { - \$this->parent = (true) ? ("foo") : ("foo"); + \$this->parent = ((true) ? ("foo") : ("foo")); if (!\$this->parent instanceof Twig_Template) { \$this->parent = \$this->env->loadTemplate(\$this->parent); }