From e0cd76614f33e8df36946452620ff89cc198cd10 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Sep 2011 14:51:05 +0200 Subject: [PATCH] fixed hardcoded name escaping --- lib/Twig/Node/Expression/AssignName.php | 6 ++++- lib/Twig/Node/Expression/Name.php | 6 ++++- test/Twig/Tests/Node/Expression/AssignNameTest.php | 2 +- test/Twig/Tests/Node/Expression/GetAttrTest.php | 6 ++-- test/Twig/Tests/Node/Expression/NameTest.php | 4 +- test/Twig/Tests/Node/ForTest.php | 26 ++++++++++---------- test/Twig/Tests/Node/IfTest.php | 10 ++++---- test/Twig/Tests/Node/ImportTest.php | 2 +- test/Twig/Tests/Node/ModuleTest.php | 2 +- test/Twig/Tests/Node/SetTest.php | 8 +++--- 10 files changed, 40 insertions(+), 32 deletions(-) diff --git a/lib/Twig/Node/Expression/AssignName.php b/lib/Twig/Node/Expression/AssignName.php index 67f1250..2ddea78 100644 --- a/lib/Twig/Node/Expression/AssignName.php +++ b/lib/Twig/Node/Expression/AssignName.php @@ -19,6 +19,10 @@ class Twig_Node_Expression_AssignName extends Twig_Node_Expression_Name */ public function compile(Twig_Compiler $compiler) { - $compiler->raw(sprintf('$context[\'%s\']', $this->getAttribute('name'))); + $compiler + ->raw('$context[') + ->string($this->getAttribute('name')) + ->raw(']') + ; } } diff --git a/lib/Twig/Node/Expression/Name.php b/lib/Twig/Node/Expression/Name.php index fceda2b..74065bb 100644 --- a/lib/Twig/Node/Expression/Name.php +++ b/lib/Twig/Node/Expression/Name.php @@ -35,7 +35,11 @@ class Twig_Node_Expression_Name extends Twig_Node_Expression } elseif (isset($specialVars[$name])) { $compiler->raw($specialVars[$name]); } else { - $compiler->raw(sprintf('$this->getContext($context, \'%s\')', $name)); + $compiler + ->raw('$this->getContext($context, ') + ->string($name) + ->raw(')') + ; } } } diff --git a/test/Twig/Tests/Node/Expression/AssignNameTest.php b/test/Twig/Tests/Node/Expression/AssignNameTest.php index 28bc667..fc92b0d 100644 --- a/test/Twig/Tests/Node/Expression/AssignNameTest.php +++ b/test/Twig/Tests/Node/Expression/AssignNameTest.php @@ -37,7 +37,7 @@ class Twig_Tests_Node_Expression_AssignNameTest extends Twig_Tests_Node_TestCase $node = new Twig_Node_Expression_AssignName('foo', 0); return array( - array($node, '$context[\'foo\']'), + array($node, '$context["foo"]'), ); } } diff --git a/test/Twig/Tests/Node/Expression/GetAttrTest.php b/test/Twig/Tests/Node/Expression/GetAttrTest.php index 298b532..a683969 100644 --- a/test/Twig/Tests/Node/Expression/GetAttrTest.php +++ b/test/Twig/Tests/Node/Expression/GetAttrTest.php @@ -49,10 +49,10 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase $attr = new Twig_Node_Expression_Constant('bar', 0); $args = new Twig_Node(); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ANY_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "any", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array(), "any", false)'); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::ARRAY_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array(), "array", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array(), "array", false)'); $args = new Twig_Node(array( @@ -60,7 +60,7 @@ class Twig_Tests_Node_Expression_GetAttrTest extends Twig_Tests_Node_TestCase new Twig_Node_Expression_Constant('bar', 0), )); $node = new Twig_Node_Expression_GetAttr($expr, $attr, $args, Twig_TemplateInterface::METHOD_CALL, 0); - $tests[] = array($node, '$this->getAttribute($this->getContext($context, \'foo\'), "bar", array($this->getContext($context, \'foo\'), "bar", ), "method", false)'); + $tests[] = array($node, '$this->getAttribute($this->getContext($context, "foo"), "bar", array($this->getContext($context, "foo"), "bar", ), "method", false)'); return $tests; } diff --git a/test/Twig/Tests/Node/Expression/NameTest.php b/test/Twig/Tests/Node/Expression/NameTest.php index b69707f..f041310 100644 --- a/test/Twig/Tests/Node/Expression/NameTest.php +++ b/test/Twig/Tests/Node/Expression/NameTest.php @@ -41,8 +41,8 @@ class Twig_Tests_Node_Expression_NameTest extends Twig_Tests_Node_TestCase $env = new Twig_Environment(null, array('strict_variables' => true)); return array( - array($node, '$this->getContext($context, \'foo\')', $env), - array($node, '$this->getContext($context, \'foo\')'), + array($node, '$this->getContext($context, "foo")', $env), + array($node, '$this->getContext($context, "foo")'), array($self, '$this'), array($context, '$context'), ); diff --git a/test/Twig/Tests/Node/ForTest.php b/test/Twig/Tests/Node/ForTest.php index 8841ba7..d6c5470 100644 --- a/test/Twig/Tests/Node/ForTest.php +++ b/test/Twig/Tests/Node/ForTest.php @@ -64,9 +64,9 @@ class Twig_Tests_Node_ForTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<getContext(\$context, 'items')); -foreach (\$context['_seq'] as \$context['key'] => \$context['item']) { - echo \$this->getContext(\$context, 'foo'); +\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, "items")); +foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) { + echo \$this->getContext(\$context, "foo"); } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']); @@ -85,7 +85,7 @@ EOF $tests[] = array($node, <<getContext(\$context, 'values')); +\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, "values")); \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, @@ -99,8 +99,8 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } -foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { - echo \$this->getContext(\$context, 'foo'); +foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { + echo \$this->getContext(\$context, "foo"); ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; @@ -127,19 +127,19 @@ EOF $tests[] = array($node, <<getContext(\$context, 'values')); +\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, "values")); \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); -foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { +foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { if (!(true)) { continue; } - echo \$this->getContext(\$context, 'foo'); + echo \$this->getContext(\$context, "foo"); ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; @@ -161,7 +161,7 @@ EOF $tests[] = array($node, <<getContext(\$context, 'values')); +\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, "values")); \$context['_iterated'] = false; \$context['loop'] = array( 'parent' => \$context['_parent'], @@ -176,8 +176,8 @@ if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_ \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } -foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { - echo \$this->getContext(\$context, 'foo'); +foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { + echo \$this->getContext(\$context, "foo"); \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; @@ -189,7 +189,7 @@ foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { } } if (!\$context['_iterated']) { - echo \$this->getContext(\$context, 'foo'); + echo \$this->getContext(\$context, "foo"); } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); diff --git a/test/Twig/Tests/Node/IfTest.php b/test/Twig/Tests/Node/IfTest.php index 4b9a048..e6995c8 100644 --- a/test/Twig/Tests/Node/IfTest.php +++ b/test/Twig/Tests/Node/IfTest.php @@ -55,7 +55,7 @@ class Twig_Tests_Node_IfTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<getContext(\$context, 'foo'); + echo \$this->getContext(\$context, "foo"); } EOF ); @@ -71,9 +71,9 @@ EOF $tests[] = array($node, <<getContext(\$context, 'foo'); + echo \$this->getContext(\$context, "foo"); } elseif (false) { - echo \$this->getContext(\$context, 'bar'); + echo \$this->getContext(\$context, "bar"); } EOF ); @@ -87,9 +87,9 @@ EOF $tests[] = array($node, <<getContext(\$context, 'foo'); + echo \$this->getContext(\$context, "foo"); } else { - echo \$this->getContext(\$context, 'bar'); + echo \$this->getContext(\$context, "bar"); } EOF ); diff --git a/test/Twig/Tests/Node/ImportTest.php b/test/Twig/Tests/Node/ImportTest.php index 3b5bf08..6a89195 100644 --- a/test/Twig/Tests/Node/ImportTest.php +++ b/test/Twig/Tests/Node/ImportTest.php @@ -43,7 +43,7 @@ class Twig_Tests_Node_ImportTest extends Twig_Tests_Node_TestCase $var = new Twig_Node_Expression_AssignName('macro', 0); $node = new Twig_Node_Import($macro, $var, 0); - $tests[] = array($node, '$context[\'macro\'] = $this->env->loadTemplate("foo.twig");'); + $tests[] = array($node, '$context["macro"] = $this->env->loadTemplate("foo.twig");'); return $tests; } diff --git a/test/Twig/Tests/Node/ModuleTest.php b/test/Twig/Tests/Node/ModuleTest.php index b2b2b86..05ee4f8 100644 --- a/test/Twig/Tests/Node/ModuleTest.php +++ b/test/Twig/Tests/Node/ModuleTest.php @@ -115,7 +115,7 @@ class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template { \$context = array_merge(\$this->env->getGlobals(), \$context); - \$context['macro'] = \$this->env->loadTemplate("foo.twig"); + \$context["macro"] = \$this->env->loadTemplate("foo.twig"); \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } diff --git a/test/Twig/Tests/Node/SetTest.php b/test/Twig/Tests/Node/SetTest.php index 8d03a60..d7e0d5f 100644 --- a/test/Twig/Tests/Node/SetTest.php +++ b/test/Twig/Tests/Node/SetTest.php @@ -43,7 +43,7 @@ class Twig_Tests_Node_SetTest extends Twig_Tests_Node_TestCase $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0)), array(), 0); $values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0)), array(), 0); $node = new Twig_Node_Set(false, $names, $values, 0); - $tests[] = array($node, '$context[\'foo\'] = "foo";'); + $tests[] = array($node, '$context["foo"] = "foo";'); $names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0)), array(), 0); $values = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Constant('foo', 0), 0)), array(), 0); @@ -51,20 +51,20 @@ class Twig_Tests_Node_SetTest extends Twig_Tests_Node_TestCase $tests[] = array($node, <<getContext(\$context, 'bar')); +list(\$context["foo"], \$context["bar"]) = array("foo", \$this->getContext(\$context, "bar")); EOF ); -- 1.7.2.5