From cccaf5583ae7159e6384ca77519a9e8bc4b21c97 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 28 Jun 2010 14:46:55 +0200 Subject: [PATCH] fixed trans tag when used with the Escaper extension (refs #75) --- CHANGELOG | 1 + lib/Twig/Node/Trans.php | 12 +++++++----- lib/Twig/TokenParser/Trans.php | 20 +++++++++++++++++++- test/Twig/Tests/Node/TransTest.php | 30 ++++-------------------------- 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e7f2ffa..b999de2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 0.9.8 + * fixed trans tag when used with the Escaper extension * fixed default cache umask * removed Twig_Template instances from the debug tag output * fixed objects with __isset() defined diff --git a/lib/Twig/Node/Trans.php b/lib/Twig/Node/Trans.php index 856d309..19b239e 100644 --- a/lib/Twig/Node/Trans.php +++ b/lib/Twig/Node/Trans.php @@ -18,7 +18,7 @@ */ class Twig_Node_Trans extends Twig_Node { - public function __construct(Twig_Node_Expression $count = null, Twig_NodeInterface $body, Twig_NodeInterface $plural = null, $lineno, $tag = null) + public function __construct(Twig_NodeInterface $body, Twig_NodeInterface $plural = null, Twig_Node_Expression $count = null, $lineno, $tag = null) { parent::__construct(array('count' => $count, 'body' => $body, 'plural' => $plural), array(), $lineno, $tag); } @@ -95,11 +95,13 @@ class Twig_Node_Trans extends Twig_Node foreach ($body as $i => $node) { if ($node instanceof Twig_Node_Text) { $msg .= $node['data']; - } elseif ($node instanceof Twig_Node_Print && $node->expr instanceof Twig_Node_Expression_Name) { - $msg .= sprintf('%%%s%%', $node->expr['name']); - $vars[] = $node->expr; } else { - throw new Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $this->lineno); + $n = $node->expr; + while ($n instanceof Twig_Node_Expression_Filter) { + $n = $n->node; + } + $msg .= sprintf('%%%s%%', $n['name']); + $vars[] = new Twig_Node_Expression_Name($n['name'], $n->getLine()); } } diff --git a/lib/Twig/TokenParser/Trans.php b/lib/Twig/TokenParser/Trans.php index 806bf3d..d819128 100644 --- a/lib/Twig/TokenParser/Trans.php +++ b/lib/Twig/TokenParser/Trans.php @@ -37,9 +37,12 @@ class Twig_TokenParser_Trans extends Twig_TokenParser throw new Twig_SyntaxError('When a plural is used, you must pass the count as an argument to the "trans" tag', $lineno); } } + $stream->expect(Twig_Token::BLOCK_END_TYPE); - return new Twig_Node_Trans($count, $body, $plural, $lineno, $this->getTag()); + $this->checkTransString($body, $lineno); + + return new Twig_Node_Trans($body, $plural, $count, $lineno, $this->getTag()); } public function decideForFork($token) @@ -61,4 +64,19 @@ class Twig_TokenParser_Trans extends Twig_TokenParser { return 'trans'; } + + protected function checkTransString(Twig_NodeInterface $body, $lineno) + { + foreach ($body as $i => $node) { + if ( + $node instanceof Twig_Node_Text + || + ($node instanceof Twig_Node_Print && $node->expr instanceof Twig_Node_Expression_Name) + ) { + continue; + } + + throw new Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno); + } + } } diff --git a/test/Twig/Tests/Node/TransTest.php b/test/Twig/Tests/Node/TransTest.php index 67a235a..30e0ddc 100644 --- a/test/Twig/Tests/Node/TransTest.php +++ b/test/Twig/Tests/Node/TransTest.php @@ -29,35 +29,13 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0), new Twig_Node_Text(' apples', 0), ), array(), 0); - $node = new Twig_Node_Trans($count, $body, $plural, 0); + $node = new Twig_Node_Trans($body, $plural, $count, 0); $this->assertEquals($body, $node->body); $this->assertEquals($count, $node->count); $this->assertEquals($plural, $node->plural); } - /** - * @covers Twig_Node_Trans::compile - * @covers Twig_Node_Trans::compileString - * @dataProvider getTests - */ - public function testCompile($node, $source, $environment = null) - { - parent::testCompile($node, $source, $environment); - - $body = new Twig_Node(array( - new Twig_Node_Expression_Constant('Hello', 0), - ), array(), 0); - $node = new Twig_Node_Trans(null, $body, null, 0); - - try { - $node->compile($this->getCompiler()); - $this->fail(); - } catch (Exception $e) { - $this->assertEquals('Twig_SyntaxError', get_class($e)); - } - } - public function getTests() { $tests = array(); @@ -65,7 +43,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase $body = new Twig_Node(array( new Twig_Node_Text('Hello', 0), ), array(), 0); - $node = new Twig_Node_Trans(null, $body, null, 0); + $node = new Twig_Node_Trans($body, null, null, 0); $tests[] = array($node, 'echo gettext("Hello");'); $body = new Twig_Node(array( @@ -73,7 +51,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0), new Twig_Node_Text(' pommes', 0), ), array(), 0); - $node = new Twig_Node_Trans(null, $body, null, 0); + $node = new Twig_Node_Trans($body, null, null, 0); $tests[] = array($node, 'echo strtr(gettext("J\'ai %foo% pommes"), array("%foo%" => (isset($context[\'foo\']) ? $context[\'foo\'] : null), ));'); $count = new Twig_Node_Expression_Constant(12, 0); @@ -89,7 +67,7 @@ class Twig_Tests_Node_TransTest extends Twig_Tests_Node_TestCase new Twig_Node_Print(new Twig_Node_Expression_Name('count', 0), 0), new Twig_Node_Text(' apples', 0), ), array(), 0); - $node = new Twig_Node_Trans($count, $body, $plural, 0); + $node = new Twig_Node_Trans($body, $plural, $count, 0); $tests[] = array($node, 'echo strtr(ngettext("Hey %name%, I have one apple", "Hey %name%, I have %count% apples", abs(12)), array("%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%name%" => (isset($context[\'name\']) ? $context[\'name\'] : null), "%count%" => abs(12), ));'); return $tests; -- 1.7.2.5