Revision 1852
Added by darkviper over 11 years ago
Escaper.php | ||
---|---|---|
19 | 19 |
{ |
20 | 20 |
protected $statusStack = array(); |
21 | 21 |
protected $blocks = array(); |
22 |
|
|
23 | 22 |
protected $safeAnalysis; |
24 | 23 |
protected $traverser; |
24 |
protected $defaultStrategy = false; |
|
25 |
protected $safeVars = array(); |
|
25 | 26 |
|
26 |
function __construct() |
|
27 |
public function __construct()
|
|
27 | 28 |
{ |
28 | 29 |
$this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis(); |
29 | 30 |
} |
... | ... | |
38 | 39 |
*/ |
39 | 40 |
public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) |
40 | 41 |
{ |
41 |
if ($node instanceof Twig_Node_AutoEscape) { |
|
42 |
if ($node instanceof Twig_Node_Module) { |
|
43 |
if ($env->hasExtension('escaper') && $defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename'))) { |
|
44 |
$this->defaultStrategy = $defaultStrategy; |
|
45 |
} |
|
46 |
$this->safeVars = array(); |
|
47 |
} elseif ($node instanceof Twig_Node_AutoEscape) { |
|
42 | 48 |
$this->statusStack[] = $node->getAttribute('value'); |
43 | 49 |
} elseif ($node instanceof Twig_Node_Block) { |
44 | 50 |
$this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); |
51 |
} elseif ($node instanceof Twig_Node_Import) { |
|
52 |
$this->safeVars[] = $node->getNode('var')->getAttribute('name'); |
|
45 | 53 |
} |
46 | 54 |
|
47 | 55 |
return $node; |
... | ... | |
57 | 65 |
*/ |
58 | 66 |
public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) |
59 | 67 |
{ |
60 |
if ($node instanceof Twig_Node_Expression_Filter) { |
|
68 |
if ($node instanceof Twig_Node_Module) { |
|
69 |
$this->defaultStrategy = false; |
|
70 |
$this->safeVars = array(); |
|
71 |
} elseif ($node instanceof Twig_Node_Expression_Filter) { |
|
61 | 72 |
return $this->preEscapeFilterNode($node, $env); |
62 | 73 |
} elseif ($node instanceof Twig_Node_Print) { |
63 | 74 |
return $this->escapePrintNode($node, $env, $this->needEscaping($env)); |
... | ... | |
123 | 134 |
if (null === $this->traverser) { |
124 | 135 |
$this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis)); |
125 | 136 |
} |
137 |
|
|
138 |
$this->safeAnalysis->setSafeVars($this->safeVars); |
|
139 |
|
|
126 | 140 |
$this->traverser->traverse($expression); |
127 | 141 |
$safe = $this->safeAnalysis->getSafe($expression); |
128 | 142 |
} |
... | ... | |
136 | 150 |
return $this->statusStack[count($this->statusStack) - 1]; |
137 | 151 |
} |
138 | 152 |
|
139 |
if ($env->hasExtension('escaper') && $env->getExtension('escaper')->isGlobal()) { |
|
140 |
return 'html'; |
|
141 |
} |
|
142 |
|
|
143 |
return false; |
|
153 |
return $this->defaultStrategy ? $this->defaultStrategy : false; |
|
144 | 154 |
} |
145 | 155 |
|
146 | 156 |
protected function getEscaperFilter($type, Twig_NodeInterface $node) |
Also available in: Unified diff
updated Twig template engine to stable version 1.11.1 step2