From: Fabien Potencier Date: Wed, 25 Apr 2012 04:23:17 +0000 (+0200) Subject: changed the autoescape option to also accept a supported escaping strategy (for BC... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=109db731cafe6699dfb2ceb94cd740365ae28916;p=web%2Fkonrad%2Ftwig.git changed the autoescape option to also accept a supported escaping strategy (for BC, true is equivalent to html) --- diff --git a/CHANGELOG b/CHANGELOG index fa2af1d..c76909a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.8.0 (2012-XX-XX) + * changed the autoescape option to also accept a supported escaping strategy (for BC, true is equivalent to html) * added an embed tag * 1.7.0 (2012-04-24) diff --git a/doc/api.rst b/doc/api.rst index 445b8a7..fc7b0d6 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -95,7 +95,8 @@ The following options are available: exception instead (default to ``false``). * ``autoescape``: If set to ``true``, auto-escaping will be enabled by default - for all templates (default to ``true``). + for all templates (default to ``true``). As of Twig 1.8, you can set the + escaping strategy to use (``html``, ``js``, or ``false`` to disable). * ``optimizations``: A flag that indicates which optimizations to apply (default to ``-1`` -- all optimizations are enabled; set it to ``0`` to diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 879ef61..bfd307b 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -60,7 +60,7 @@ class Twig_Environment * templates (default to Twig_Template). * * * cache: An absolute path where to store the compiled templates, or - * false to disable compilation cache (default) + * false to disable compilation cache (default). * * * auto_reload: Whether to reload the template is the original source changed. * If you don't provide the auto_reload option, it will be @@ -69,11 +69,14 @@ class Twig_Environment * * strict_variables: Whether to ignore invalid variables in templates * (default to false). * - * * autoescape: Whether to enable auto-escaping (default to true); + * * autoescape: Whether to enable auto-escaping (default to html): + * * false: disable auto-escaping + * * true: equivalent to html + * * html, js: set the autoescaping to one of the supported strategies * * * optimizations: A flag that indicates which optimizations to apply * (default to -1 which means that all optimizations are enabled; - * set it to 0 to disable) + * set it to 0 to disable). * * @param Twig_LoaderInterface $loader A Twig_LoaderInterface instance * @param array $options An array of options @@ -89,7 +92,7 @@ class Twig_Environment 'charset' => 'UTF-8', 'base_template_class' => 'Twig_Template', 'strict_variables' => false, - 'autoescape' => true, + 'autoescape' => 'html', 'cache' => false, 'auto_reload' => null, 'optimizations' => -1, @@ -101,7 +104,7 @@ class Twig_Environment $this->autoReload = null === $options['auto_reload'] ? $this->debug : (bool) $options['auto_reload']; $this->extensions = array( 'core' => new Twig_Extension_Core(), - 'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']), + 'escaper' => new Twig_Extension_Escaper($options['autoescape']), 'optimizer' => new Twig_Extension_Optimizer($options['optimizations']), ); $this->strictVariables = (bool) $options['strict_variables']; diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php index 43ae111..686cc90 100644 --- a/lib/Twig/Extension/Escaper.php +++ b/lib/Twig/Extension/Escaper.php @@ -10,11 +10,16 @@ */ class Twig_Extension_Escaper extends Twig_Extension { - protected $autoescape; + protected $defaultStrategy; - public function __construct($autoescape = true) + public function __construct($defaultStrategy = 'html') { - $this->autoescape = $autoescape; + // for BC + if (true === $defaultStrategy) { + $defaultStrategy = 'html'; + } + + $this->defaultStrategy = $defaultStrategy; } /** @@ -49,9 +54,9 @@ class Twig_Extension_Escaper extends Twig_Extension ); } - public function isGlobal() + public function getDefaultStrategy() { - return $this->autoescape; + return $this->defaultStrategy; } /** diff --git a/lib/Twig/NodeVisitor/Escaper.php b/lib/Twig/NodeVisitor/Escaper.php index bf15077..d57c307 100644 --- a/lib/Twig/NodeVisitor/Escaper.php +++ b/lib/Twig/NodeVisitor/Escaper.php @@ -19,7 +19,6 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface { protected $statusStack = array(); protected $blocks = array(); - protected $safeAnalysis; protected $traverser; @@ -136,8 +135,8 @@ class Twig_NodeVisitor_Escaper implements Twig_NodeVisitorInterface return $this->statusStack[count($this->statusStack) - 1]; } - if ($env->hasExtension('escaper') && $env->getExtension('escaper')->isGlobal()) { - return 'html'; + if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy()) { + return $defaultStrategy; } return false;