fixed trans tag when used with the Escaper extension (refs #75)
authorFabien Potencier <fabien.potencier@gmail.com>
Mon, 28 Jun 2010 12:46:55 +0000 (14:46 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Mon, 28 Jun 2010 12:52:43 +0000 (14:52 +0200)
CHANGELOG
lib/Twig/Node/Trans.php
lib/Twig/TokenParser/Trans.php
test/Twig/Tests/Node/TransTest.php

index e7f2ffa..b999de2 100644 (file)
--- 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
index 856d309..19b239e 100644 (file)
@@ -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());
             }
         }
 
index 806bf3d..d819128 100644 (file)
@@ -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);
+        }
+    }
 }
index 67a235a..30e0ddc 100644 (file)
@@ -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;