made the escape filter smarter when the encoding is not supported by PHP
authorFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 09:09:54 +0000 (11:09 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Sun, 16 Oct 2011 09:12:08 +0000 (11:12 +0200)
CHANGELOG
lib/Twig/Extension/Core.php

index cf0646f..84b314b 100644 (file)
--- 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
index 2d2c22e..d0f6acb 100644 (file)
@@ -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));