From 344043e891e00899e4455e2d48a1c38c05455fcc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 16 Oct 2011 11:09:54 +0200 Subject: [PATCH] made the escape filter smarter when the encoding is not supported by PHP --- CHANGELOG | 1 + lib/Twig/Extension/Core.php | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index cf0646f..84b314b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0 + * made the escape filter smarter when the encoding is not supported by PHP * added a convert_encoding filter * moved all node manipulations outside the compile() Node method * made several speed optimizations diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 2d2c22e..d0f6acb 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -510,7 +510,28 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $cha return $string; case 'html': - return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); + // see http://php.net/htmlspecialchars + if (in_array(strtolower($charset), array( + 'iso-8859-1', 'iso8859-1', + 'iso-8859-15', 'iso8859-15', + 'utf-8', + 'cp866', 'ibm866', '866', + 'cp1251', 'win-1251', '1251', + 'cp1252', '1252', + 'koi8-r', 'koi8-ru', 'koi8r', + 'big5', '950', + 'gb2312', '936', + 'big5-hkscs', 'big5', + 'shift_jis', 'sjis', '932', + 'euc-jp', 'eucjp', + ))) { + return htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, $charset); + } + + $string = twig_convert_encoding($string, 'UTF-8', $charset); + $string = htmlspecialchars($string, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); + + return twig_convert_encoding($string, $charset, 'UTF-8'); default: throw new Twig_Error_Runtime(sprintf('Invalid escape type "%s".', $type)); -- 1.7.2.5