composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / NodeVisitor / Escaper.php
blob1a1ae66f7584d6e916fe0d8d1fe247c9af68afd5
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 /**
13 * Twig_NodeVisitor_Escaper implements output escaping.
15 * @final
17 * @author Fabien Potencier <fabien@symfony.com>
19 class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor
21 protected $statusStack = array();
22 protected $blocks = array();
23 protected $safeAnalysis;
24 protected $traverser;
25 protected $defaultStrategy = false;
26 protected $safeVars = array();
28 public function __construct()
30 $this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
33 protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
35 if ($node instanceof Twig_Node_Module) {
36 if ($env->hasExtension('Twig_Extension_Escaper') && $defaultStrategy = $env->getExtension('Twig_Extension_Escaper')->getDefaultStrategy($node->getTemplateName())) {
37 $this->defaultStrategy = $defaultStrategy;
39 $this->safeVars = array();
40 $this->blocks = array();
41 } elseif ($node instanceof Twig_Node_AutoEscape) {
42 $this->statusStack[] = $node->getAttribute('value');
43 } elseif ($node instanceof Twig_Node_Block) {
44 $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
45 } elseif ($node instanceof Twig_Node_Import) {
46 $this->safeVars[] = $node->getNode('var')->getAttribute('name');
49 return $node;
52 protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
54 if ($node instanceof Twig_Node_Module) {
55 $this->defaultStrategy = false;
56 $this->safeVars = array();
57 $this->blocks = array();
58 } elseif ($node instanceof Twig_Node_Expression_Filter) {
59 return $this->preEscapeFilterNode($node, $env);
60 } elseif ($node instanceof Twig_Node_Print) {
61 return $this->escapePrintNode($node, $env, $this->needEscaping($env));
64 if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) {
65 array_pop($this->statusStack);
66 } elseif ($node instanceof Twig_Node_BlockReference) {
67 $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env);
70 return $node;
73 protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type)
75 if (false === $type) {
76 return $node;
79 $expression = $node->getNode('expr');
81 if ($this->isSafeFor($type, $expression, $env)) {
82 return $node;
85 $class = get_class($node);
87 return new $class(
88 $this->getEscaperFilter($type, $expression),
89 $node->getTemplateLine()
93 protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
95 $name = $filter->getNode('filter')->getAttribute('value');
97 $type = $env->getFilter($name)->getPreEscape();
98 if (null === $type) {
99 return $filter;
102 $node = $filter->getNode('node');
103 if ($this->isSafeFor($type, $node, $env)) {
104 return $filter;
107 $filter->setNode('node', $this->getEscaperFilter($type, $node));
109 return $filter;
112 protected function isSafeFor($type, Twig_NodeInterface $expression, $env)
114 $safe = $this->safeAnalysis->getSafe($expression);
116 if (null === $safe) {
117 if (null === $this->traverser) {
118 $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
121 $this->safeAnalysis->setSafeVars($this->safeVars);
123 $this->traverser->traverse($expression);
124 $safe = $this->safeAnalysis->getSafe($expression);
127 return in_array($type, $safe) || in_array('all', $safe);
130 protected function needEscaping(Twig_Environment $env)
132 if (count($this->statusStack)) {
133 return $this->statusStack[count($this->statusStack) - 1];
136 return $this->defaultStrategy ? $this->defaultStrategy : false;
139 protected function getEscaperFilter($type, Twig_NodeInterface $node)
141 $line = $node->getTemplateLine();
142 $name = new Twig_Node_Expression_Constant('escape', $line);
143 $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)));
145 return new Twig_Node_Expression_Filter($node, $name, $args, $line);
148 public function getPriority()
150 return 0;
154 class_alias('Twig_NodeVisitor_Escaper', 'Twig\NodeVisitor\EscaperNodeVisitor', false);