Twig_NodeVisitor_Escaper

Twig_NodeVisitor_Escaper implements output escaping.

Defined (1)

The class is defined in the following location(s).

/vendor/twig/twig/lib/Twig/NodeVisitor/Escaper.php  
  1. class Twig_NodeVisitor_Escaper extends Twig_BaseNodeVisitor 
  2. protected $statusStack = array(); 
  3. protected $blocks = array(); 
  4. protected $safeAnalysis; 
  5. protected $traverser; 
  6. protected $defaultStrategy = false; 
  7. protected $safeVars = array(); 
  8.  
  9. public function __construct() 
  10. $this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis(); 
  11.  
  12. protected function doEnterNode(Twig_Node $node, Twig_Environment $env) 
  13. if ($node instanceof Twig_Node_Module) { 
  14. if ($env->hasExtension('Twig_Extension_Escaper') && $defaultStrategy = $env->getExtension('Twig_Extension_Escaper')->getDefaultStrategy($node->getTemplateName())) { 
  15. $this->defaultStrategy = $defaultStrategy; 
  16. $this->safeVars = array(); 
  17. $this->blocks = array(); 
  18. } elseif ($node instanceof Twig_Node_AutoEscape) { 
  19. $this->statusStack[] = $node->getAttribute('value'); 
  20. } elseif ($node instanceof Twig_Node_Block) { 
  21. $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env); 
  22. } elseif ($node instanceof Twig_Node_Import) { 
  23. $this->safeVars[] = $node->getNode('var')->getAttribute('name'); 
  24.  
  25. return $node; 
  26.  
  27. protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) 
  28. if ($node instanceof Twig_Node_Module) { 
  29. $this->defaultStrategy = false; 
  30. $this->safeVars = array(); 
  31. $this->blocks = array(); 
  32. } elseif ($node instanceof Twig_Node_Expression_Filter) { 
  33. return $this->preEscapeFilterNode($node, $env); 
  34. } elseif ($node instanceof Twig_Node_Print) { 
  35. return $this->escapePrintNode($node, $env, $this->needEscaping($env)); 
  36.  
  37. if ($node instanceof Twig_Node_AutoEscape || $node instanceof Twig_Node_Block) { 
  38. array_pop($this->statusStack); 
  39. } elseif ($node instanceof Twig_Node_BlockReference) { 
  40. $this->blocks[$node->getAttribute('name')] = $this->needEscaping($env); 
  41.  
  42. return $node; 
  43.  
  44. protected function escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type) 
  45. if (false === $type) { 
  46. return $node; 
  47.  
  48. $expression = $node->getNode('expr'); 
  49.  
  50. if ($this->isSafeFor($type, $expression, $env)) { 
  51. return $node; 
  52.  
  53. $class = get_class($node); 
  54.  
  55. return new $class( 
  56. $this->getEscaperFilter($type, $expression),  
  57. $node->getTemplateLine() 
  58. ); 
  59.  
  60. protected function preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env) 
  61. $name = $filter->getNode('filter')->getAttribute('value'); 
  62.  
  63. $type = $env->getFilter($name)->getPreEscape(); 
  64. if (null === $type) { 
  65. return $filter; 
  66.  
  67. $node = $filter->getNode('node'); 
  68. if ($this->isSafeFor($type, $node, $env)) { 
  69. return $filter; 
  70.  
  71. $filter->setNode('node', $this->getEscaperFilter($type, $node)); 
  72.  
  73. return $filter; 
  74.  
  75. protected function isSafeFor($type, Twig_NodeInterface $expression, $env) 
  76. $safe = $this->safeAnalysis->getSafe($expression); 
  77.  
  78. if (null === $safe) { 
  79. if (null === $this->traverser) { 
  80. $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis)); 
  81.  
  82. $this->safeAnalysis->setSafeVars($this->safeVars); 
  83.  
  84. $this->traverser->traverse($expression); 
  85. $safe = $this->safeAnalysis->getSafe($expression); 
  86.  
  87. return in_array($type, $safe) || in_array('all', $safe); 
  88.  
  89. protected function needEscaping(Twig_Environment $env) 
  90. if (count($this->statusStack)) { 
  91. return $this->statusStack[count($this->statusStack) - 1]; 
  92.  
  93. return $this->defaultStrategy ? $this->defaultStrategy : false; 
  94.  
  95. protected function getEscaperFilter($type, Twig_NodeInterface $node) 
  96. $line = $node->getTemplateLine(); 
  97. $name = new Twig_Node_Expression_Constant('escape', $line); 
  98. $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))); 
  99.  
  100. return new Twig_Node_Expression_Filter($node, $name, $args, $line); 
  101.  
  102. public function getPriority() 
  103. return 0;