From 2ff6cedbf88f78c8050bc51edbed7e6d52465b6c Mon Sep 17 00:00:00 2001 From: Mark Story Date: Mon, 23 Aug 2010 23:02:57 -0400 Subject: [PATCH] fixed objects with __toString() not being autoescaped (fixes #111) --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 2 +- .../Tests/Fixtures/tags/autoescape/objects.test | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 3018b7e..ae1e046 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,6 +5,7 @@ Backward incompatibilities: * the odd and even filters are now tests: {{ foo|odd }} must now be written {{ foo is odd }} + * fixed objects with __toString() not being autoescaped * fixed subscript expressions when calling __call() (methods now keep the case) * added a "trans" filter * added "test" feature (accessible via the "is" operator) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 60b51dc..6934fb3 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -211,7 +211,7 @@ function twig_cycle_filter($values, $i) */ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html') { - if (!is_string($string)) { + if (!is_string($string) && !(is_object($string) && method_exists($string, '__toString'))) { return $string; } diff --git a/test/Twig/Tests/Fixtures/tags/autoescape/objects.test b/test/Twig/Tests/Fixtures/tags/autoescape/objects.test index 44f41e1..12964a6 100644 --- a/test/Twig/Tests/Fixtures/tags/autoescape/objects.test +++ b/test/Twig/Tests/Fixtures/tags/autoescape/objects.test @@ -4,6 +4,7 @@ {% autoescape on %} {{ user.name }} {{ user.name|lower }} +{{ user }} {% endautoescape %} --DATA-- class UserForAutoEscapeTest @@ -12,8 +13,14 @@ class UserForAutoEscapeTest { return 'Fabien
'; } + + public function __toString() + { + return 'Fabien
'; + } } return array('user' => new UserForAutoEscapeTest()) --EXPECT-- Fabien<br /> fabien<br /> +Fabien<br /> -- 1.7.2.5