made the charset configurable for the escape filter

This commit is contained in:
Fabien Potencier
2011-03-17 08:02:42 +01:00
parent bec8839394
commit 64f8709de2
+6 -4
View File
@@ -299,7 +299,7 @@ function twig_strtr($pattern, $replacements)
* it would be right to leave the string as-is, but c-escape the apostrophe and
* the new line.
*/
function twig_escape_filter(Twig_Environment $env, $string, $type = 'html')
function twig_escape_filter(Twig_Environment $env, $string, $type = 'html', $charset = null)
{
if (is_object($string) && $string instanceof Twig_Markup) {
return $string;
@@ -309,12 +309,14 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html')
return $string;
}
if (null === $charset) {
$charset = $env->getCharset();
}
switch ($type) {
case 'js':
// escape all non-alphanumeric characters
// into their \xHH or \uHHHH representations
$charset = $env->getCharset();
if ('UTF-8' != $charset) {
$string = _twig_convert_encoding($string, 'UTF-8', $charset);
}
@@ -330,7 +332,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $type = 'html')
return $string;
case 'html':
return htmlspecialchars($string, ENT_QUOTES, $env->getCharset());
return htmlspecialchars($string, ENT_QUOTES, $charset);
default:
throw new Twig_Error_Runtime(sprintf('Invalid escape type "%s".', $type));