From c79bb17c9372999f167e7a28d843357cad9908b7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 2 Dec 2010 08:42:09 +0100 Subject: [PATCH] fixed escaper when wrapping a Twig_Node_Print (the original class must be preserved) --- lib/Twig/Node/SandboxedPrint.php | 8 ++++---- lib/Twig/NodeVisitor/Escaper.php | 4 +++- lib/Twig/NodeVisitor/Sandbox.php | 2 +- test/Twig/Tests/Node/SandboxedPrintTest.php | 12 ++++-------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/Twig/Node/SandboxedPrint.php b/lib/Twig/Node/SandboxedPrint.php index 34db421..877838e 100644 --- a/lib/Twig/Node/SandboxedPrint.php +++ b/lib/Twig/Node/SandboxedPrint.php @@ -22,9 +22,9 @@ */ class Twig_Node_SandboxedPrint extends Twig_Node_Print { - public function __construct(Twig_Node_Print $node) + public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) { - parent::__construct($node->getNode('expr'), $node->getLine(), $node->getNodeTag()); + parent::__construct($expr, $lineno, $tag); } /** @@ -46,8 +46,8 @@ class Twig_Node_SandboxedPrint extends Twig_Node_Print ->raw('$_tmp, \'__toString\');'."\n") ->outdent() ->write('}'."\n") - ->write('echo ') - ->raw("\$_tmp;\n") ; + + parent::compile($compiler); } } diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index ad0e420..8f0cc14 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -84,7 +84,9 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface return $node; } - return new Twig_Node_Print( + $class = get_class($node); + + return new $class( $this->getEscaperFilter($type, $expression), $node->getLine() ); diff --git a/lib/Twig/NodeVisitor/Sandbox.php b/lib/Twig/NodeVisitor/Sandbox.php index 7155f58..3f9d168 100644 --- a/lib/Twig/NodeVisitor/Sandbox.php +++ b/lib/Twig/NodeVisitor/Sandbox.php @@ -50,7 +50,7 @@ class Twig_NodeVisitor_Sandbox implements Twig_NodeVisitorInterface // wrap print to check __toString() calls if ($node instanceof Twig_Node_Print) { - return new Twig_Node_SandboxedPrint($node); + return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag()); } } diff --git a/test/Twig/Tests/Node/SandboxedPrintTest.php b/test/Twig/Tests/Node/SandboxedPrintTest.php index 3e53b6e..6e30241 100644 --- a/test/Twig/Tests/Node/SandboxedPrintTest.php +++ b/test/Twig/Tests/Node/SandboxedPrintTest.php @@ -18,9 +18,7 @@ class Twig_Tests_Node_SandboxedPrintTest extends Twig_Tests_Node_TestCase */ public function testConstructor() { - $expr = new Twig_Node_Expression_Constant('foo', 0); - $node = new Twig_Node_Print($expr, 0); - $node = new Twig_Node_SandboxedPrint($node); + $node = new Twig_Node_SandboxedPrint($expr = new Twig_Node_Expression_Constant('foo', 0), 0); $this->assertEquals($expr, $node->getNode('expr')); } @@ -38,13 +36,11 @@ class Twig_Tests_Node_SandboxedPrintTest extends Twig_Tests_Node_TestCase { $tests = array(); - $node = new Twig_Node_Print(new Twig_Node_Expression_Constant('foo', 0), 0); - $tests[] = array(new Twig_Node_SandboxedPrint($node), <<env->getExtension('sandbox')->checkMethodAllowed(\$_tmp, '__toString'); } -echo \$_tmp; +echo "foo"; EOF ); -- 1.7.2.5