4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Twig_NodeVisitor_Escaper implements output escaping.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_NodeVisitor_Escaper
implements Twig_NodeVisitorInterface
19 protected $statusStack = array();
20 protected $blocks = array();
21 protected $safeAnalysis;
23 protected $defaultStrategy = false;
24 protected $safeVars = array();
26 public function __construct()
28 $this->safeAnalysis
= new Twig_NodeVisitor_SafeAnalysis();
32 * Called before child nodes are visited.
34 * @param Twig_NodeInterface $node The node to visit
35 * @param Twig_Environment $env The Twig environment instance
37 * @return Twig_NodeInterface The modified node
39 public function enterNode(Twig_NodeInterface
$node, Twig_Environment
$env)
41 if ($node instanceof Twig_Node_Module
) {
42 if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) {
43 $this->defaultStrategy
= $defaultStrategy;
45 $this->safeVars
= array();
46 } elseif ($node instanceof Twig_Node_AutoEscape
) {
47 $this->statusStack
[] = $node->getAttribute('value');
48 } elseif ($node instanceof Twig_Node_Block
) {
49 $this->statusStack
[] = isset($this->blocks
[$node->getAttribute('name')]) ? $this->blocks
[$node->getAttribute('name')] : $this->needEscaping($env);
50 } elseif ($node instanceof Twig_Node_Import
) {
51 $this->safeVars
[] = $node->getNode('var')->getAttribute('name');
58 * Called after child nodes are visited.
60 * @param Twig_NodeInterface $node The node to visit
61 * @param Twig_Environment $env The Twig environment instance
63 * @return Twig_NodeInterface The modified node
65 public function leaveNode(Twig_NodeInterface
$node, Twig_Environment
$env)
67 if ($node instanceof Twig_Node_Module
) {
68 $this->defaultStrategy
= false;
69 $this->safeVars
= array();
70 } elseif ($node instanceof Twig_Node_Expression_Filter
) {
71 return $this->preEscapeFilterNode($node, $env);
72 } elseif ($node instanceof Twig_Node_Print
) {
73 return $this->escapePrintNode($node, $env, $this->needEscaping($env));
76 if ($node instanceof Twig_Node_AutoEscape
|| $node instanceof Twig_Node_Block
) {
77 array_pop($this->statusStack
);
78 } elseif ($node instanceof Twig_Node_BlockReference
) {
79 $this->blocks
[$node->getAttribute('name')] = $this->needEscaping($env);
85 protected function escapePrintNode(Twig_Node_Print
$node, Twig_Environment
$env, $type)
87 if (false === $type) {
91 $expression = $node->getNode('expr');
93 if ($this->isSafeFor($type, $expression, $env)) {
97 $class = get_class($node);
100 $this->getEscaperFilter($type, $expression),
105 protected function preEscapeFilterNode(Twig_Node_Expression_Filter
$filter, Twig_Environment
$env)
107 $name = $filter->getNode('filter')->getAttribute('value');
109 $type = $env->getFilter($name)->getPreEscape();
110 if (null === $type) {
114 $node = $filter->getNode('node');
115 if ($this->isSafeFor($type, $node, $env)) {
119 $filter->setNode('node', $this->getEscaperFilter($type, $node));
124 protected function isSafeFor($type, Twig_NodeInterface
$expression, $env)
126 $safe = $this->safeAnalysis
->getSafe($expression);
128 if (null === $safe) {
129 if (null === $this->traverser
) {
130 $this->traverser
= new Twig_NodeTraverser($env, array($this->safeAnalysis
));
133 $this->safeAnalysis
->setSafeVars($this->safeVars
);
135 $this->traverser
->traverse($expression);
136 $safe = $this->safeAnalysis
->getSafe($expression);
139 return in_array($type, $safe) || in_array('all', $safe);
142 protected function needEscaping(Twig_Environment
$env)
144 if (count($this->statusStack
)) {
145 return $this->statusStack
[count($this->statusStack
) - 1];
148 return $this->defaultStrategy
? $this->defaultStrategy
: false;
151 protected function getEscaperFilter($type, Twig_NodeInterface
$node)
153 $line = $node->getLine();
154 $name = new Twig_Node_Expression_Constant('escape', $line);
155 $args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));
157 return new Twig_Node_Expression_Filter($node, $name, $args, $line);
163 public function getPriority()