Skip to content
Browse files

fixed hardcoded name escaping

  • Loading branch information...
1 parent b058160 commit e0cd76614f33e8df36946452620ff89cc198cd10 @fabpot committed Sep 30, 2011
View
6 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(']')
+ ;
}
}
View
6 lib/Twig/Node/Expression/Name.php
@@ -35,7 +35,11 @@ public function compile(Twig_Compiler $compiler)
} 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(')')
+ ;
}
}
}
View
2 test/Twig/Tests/Node/Expression/AssignNameTest.php
@@ -37,7 +37,7 @@ public function getTests()
$node = new Twig_Node_Expression_AssignName('foo', 0);
return array(
- array($node, '$context[\'foo\']'),
+ array($node, '$context["foo"]'),
);
}
}
View
6 test/Twig/Tests/Node/Expression/GetAttrTest.php
@@ -49,18 +49,18 @@ public function getTests()
$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(
new Twig_Node_Expression_Name('foo', 0),
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;
}
View
4 test/Twig/Tests/Node/Expression/NameTest.php
@@ -41,8 +41,8 @@ public function getTests()
$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'),
);
View
26 test/Twig/Tests/Node/ForTest.php
@@ -64,9 +64,9 @@ public function getTests()
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_ensure_traversable(\$this->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 @@ public function getTests()
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, 'values'));
+\$context['_seq'] = twig_ensure_traversable(\$this->getContext(\$context, "values"));
\$context['loop'] = array(
'parent' => \$context['_parent'],
'index0' => 0,
@@ -99,8 +99,8 @@ public function getTests()
\$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 @@ public function getTests()
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_ensure_traversable(\$this->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 @@ public function getTests()
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_ensure_traversable(\$this->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 @@ public function getTests()
\$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 @@ public function getTests()
}
}
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']);
View
10 test/Twig/Tests/Node/IfTest.php
@@ -55,7 +55,7 @@ public function getTests()
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->getContext(\$context, 'foo');
+ echo \$this->getContext(\$context, "foo");
}
EOF
);
@@ -71,9 +71,9 @@ public function getTests()
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->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 @@ public function getTests()
$tests[] = array($node, <<<EOF
if (true) {
- echo \$this->getContext(\$context, 'foo');
+ echo \$this->getContext(\$context, "foo");
} else {
- echo \$this->getContext(\$context, 'bar');
+ echo \$this->getContext(\$context, "bar");
}
EOF
);
View
2 test/Twig/Tests/Node/ImportTest.php
@@ -43,7 +43,7 @@ public function getTests()
$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;
}
View
2 test/Twig/Tests/Node/ModuleTest.php
@@ -115,7 +115,7 @@ protected function doDisplay(array \$context, array \$blocks = array())
{
\$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));
}
View
8 test/Twig/Tests/Node/SetTest.php
@@ -43,28 +43,28 @@ public function getTests()
$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);
$node = new Twig_Node_Set(true, $names, $values, 0);
$tests[] = array($node, <<<EOF
ob_start();
echo "foo";
-\$context['foo'] = new Twig_Markup(ob_get_clean());
+\$context["foo"] = new Twig_Markup(ob_get_clean());
EOF
);
$names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0)), array(), 0);
$values = new Twig_Node_Text('foo', 0);
$node = new Twig_Node_Set(true, $names, $values, 0);
- $tests[] = array($node, '$context[\'foo\'] = new Twig_Markup("foo");');
+ $tests[] = array($node, '$context["foo"] = new Twig_Markup("foo");');
$names = new Twig_Node(array(new Twig_Node_Expression_AssignName('foo', 0), new Twig_Node_Expression_AssignName('bar', 0)), array(), 0);
$values = new Twig_Node(array(new Twig_Node_Expression_Constant('foo', 0), new Twig_Node_Expression_Name('bar', 0)), array(), 0);
$node = new Twig_Node_Set(false, $names, $values, 0);
$tests[] = array($node, <<<EOF
-list(\$context['foo'], \$context['bar']) = array("foo", \$this->getContext(\$context, 'bar'));
+list(\$context["foo"], \$context["bar"]) = array("foo", \$this->getContext(\$context, "bar"));
EOF
);

0 comments on commit e0cd766

Please sign in to comment.
Something went wrong with that request. Please try again.