added a js escaping strategy

git-svn-id: http://svn.twig-project.org/trunk@81 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
fabien
2009-10-20 04:25:01 +00:00
parent e850d97980
commit 4a5173151d
2 changed files with 24 additions and 5 deletions
-3
View File
@@ -1,8 +1,5 @@
<?php
require_once dirname(__FILE__).'/../lib/Twig/Autoloader.php';
Twig_Autoloader::register();
if (!isset($argv[1]))
{
die('You must provide the version (1.0.0)');
+24 -2
View File
@@ -99,14 +99,36 @@ function twig_sort_filter($array)
return $array;
}
function twig_escape_filter(Twig_Environment $env, $string)
/*
* Each type specifies a way for applying a transformation to a string
* The purpose is for the string to be "escaped" so it is suitable for
* the format it is being displayed in.
*
* For example, the string: "It's required that you enter a username & password.\n"
* If this were to be displayed as HTML it would be sensible to turn the
* ampersand into '&amp;' and the apostrophe into '&aps;'. However if it were
* going to be used as a string in JavaScript to be displayed in an alert box
* 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')
{
if (!is_string($string))
{
return $string;
}
return htmlspecialchars($string, ENT_QUOTES, $env->getCharset());
switch ($type)
{
case 'js':
// a function the c-escapes a string, making it suitable to be placed in a JavaScript string
return str_replace(array("\\" , "\n" , "\r" , "\"" , "'"),
array("\\\\", "\\n" , "\\r", "\\\"", "\\'"),
$string);
case 'html':
default:
return htmlspecialchars($string, ENT_QUOTES, $env->getCharset());
}
}
// add multibyte extensions if possible