ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_NodeVisitor_Escaper Class Reference

Twig_NodeVisitor_Escaper implements output escaping. More...

+ Inheritance diagram for Twig_NodeVisitor_Escaper:
+ Collaboration diagram for Twig_NodeVisitor_Escaper:

Public Member Functions

 __construct ()
 
 getPriority ()
 Returns the priority for this visitor. More...
 
- Public Member Functions inherited from Twig_BaseNodeVisitor
 enterNode (Twig_NodeInterface $node, Twig_Environment $env)
 Called before child nodes are visited. More...
 
 leaveNode (Twig_NodeInterface $node, Twig_Environment $env)
 Called after child nodes are visited. More...
 

Protected Member Functions

 doEnterNode (Twig_Node $node, Twig_Environment $env)
 
 doLeaveNode (Twig_Node $node, Twig_Environment $env)
 
 escapePrintNode (Twig_Node_Print $node, Twig_Environment $env, $type)
 
 preEscapeFilterNode (Twig_Node_Expression_Filter $filter, Twig_Environment $env)
 
 isSafeFor ($type, Twig_NodeInterface $expression, $env)
 
 needEscaping (Twig_Environment $env)
 
 getEscaperFilter ($type, Twig_NodeInterface $node)
 
- Protected Member Functions inherited from Twig_BaseNodeVisitor
 doEnterNode (Twig_Node $node, Twig_Environment $env)
 Called before child nodes are visited. More...
 
 doLeaveNode (Twig_Node $node, Twig_Environment $env)
 Called after child nodes are visited. More...
 

Protected Attributes

 $statusStack = array()
 
 $blocks = array()
 
 $safeAnalysis
 
 $traverser
 
 $defaultStrategy = false
 
 $safeVars = array()
 

Detailed Description

Twig_NodeVisitor_Escaper implements output escaping.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 19 of file Escaper.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_NodeVisitor_Escaper::__construct ( )

Definition at line 28 of file Escaper.php.

29  {
30  $this->safeAnalysis = new Twig_NodeVisitor_SafeAnalysis();
31  }

Member Function Documentation

◆ doEnterNode()

Twig_NodeVisitor_Escaper::doEnterNode ( Twig_Node  $node,
Twig_Environment  $env 
)
protected

Definition at line 33 of file Escaper.php.

References $defaultStrategy, array, Twig_Node\getAttribute(), Twig_Environment\getExtension(), Twig_Node\getNode(), Twig_Node\getTemplateName(), Twig_Environment\hasExtension(), and needEscaping().

34  {
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;
38  }
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');
47  }
48 
49  return $node;
50  }
getTemplateName()
Definition: Node.php:229
Represents an autoescape node.
Definition: AutoEscape.php:23
needEscaping(Twig_Environment $env)
Definition: Escaper.php:130
Represents a block node.
Definition: Block.php:18
getAttribute($name)
Definition: Node.php:152
Represents a module node.
Definition: Module.php:22
Create styles array
The data for the language used.
hasExtension($class)
Returns true if the given extension is registered.
Represents an import node.
Definition: Import.php:17
getNode($name)
Definition: Node.php:186
getExtension($class)
Gets an extension by class name.
+ Here is the call graph for this function:

◆ doLeaveNode()

Twig_NodeVisitor_Escaper::doLeaveNode ( Twig_Node  $node,
Twig_Environment  $env 
)
protected

Definition at line 52 of file Escaper.php.

References array, escapePrintNode(), Twig_Node\getAttribute(), needEscaping(), and preEscapeFilterNode().

53  {
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));
62  }
63 
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);
68  }
69 
70  return $node;
71  }
Represents an autoescape node.
Definition: AutoEscape.php:23
needEscaping(Twig_Environment $env)
Definition: Escaper.php:130
preEscapeFilterNode(Twig_Node_Expression_Filter $filter, Twig_Environment $env)
Definition: Escaper.php:93
Represents a block node.
Definition: Block.php:18
Represents a block call node.
escapePrintNode(Twig_Node_Print $node, Twig_Environment $env, $type)
Definition: Escaper.php:73
Represents a node that outputs an expression.
Definition: Print.php:18
getAttribute($name)
Definition: Node.php:152
Represents a module node.
Definition: Module.php:22
Create styles array
The data for the language used.
+ Here is the call graph for this function:

◆ escapePrintNode()

Twig_NodeVisitor_Escaper::escapePrintNode ( Twig_Node_Print  $node,
Twig_Environment  $env,
  $type 
)
protected

Definition at line 73 of file Escaper.php.

References $type, getEscaperFilter(), Twig_Node\getNode(), Twig_Node\getTemplateLine(), and isSafeFor().

Referenced by doLeaveNode().

74  {
75  if (false === $type) {
76  return $node;
77  }
78 
79  $expression = $node->getNode('expr');
80 
81  if ($this->isSafeFor($type, $expression, $env)) {
82  return $node;
83  }
84 
85  $class = get_class($node);
86 
87  return new $class(
88  $this->getEscaperFilter($type, $expression),
89  $node->getTemplateLine()
90  );
91  }
$type
isSafeFor($type, Twig_NodeInterface $expression, $env)
Definition: Escaper.php:112
getTemplateLine()
Definition: Node.php:121
getNode($name)
Definition: Node.php:186
getEscaperFilter($type, Twig_NodeInterface $node)
Definition: Escaper.php:139
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getEscaperFilter()

Twig_NodeVisitor_Escaper::getEscaperFilter (   $type,
Twig_NodeInterface  $node 
)
protected

Definition at line 139 of file Escaper.php.

References $name, $type, and array.

Referenced by escapePrintNode(), and preEscapeFilterNode().

140  {
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)));
144 
145  return new Twig_Node_Expression_Filter($node, $name, $args, $line);
146  }
Represents a node in the AST.
Definition: Node.php:18
$type
if($format !==null) $name
Definition: metadata.php:146
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ getPriority()

Twig_NodeVisitor_Escaper::getPriority ( )

Returns the priority for this visitor.

Priority should be between -10 and 10 (0 is the default).

Returns
int The priority level

Implements Twig_NodeVisitorInterface.

Definition at line 148 of file Escaper.php.

149  {
150  return 0;
151  }

◆ isSafeFor()

Twig_NodeVisitor_Escaper::isSafeFor (   $type,
Twig_NodeInterface  $expression,
  $env 
)
protected

Definition at line 112 of file Escaper.php.

References $env, $type, and array.

Referenced by escapePrintNode(), and preEscapeFilterNode().

113  {
114  $safe = $this->safeAnalysis->getSafe($expression);
115 
116  if (null === $safe) {
117  if (null === $this->traverser) {
118  $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
119  }
120 
121  $this->safeAnalysis->setSafeVars($this->safeVars);
122 
123  $this->traverser->traverse($expression);
124  $safe = $this->safeAnalysis->getSafe($expression);
125  }
126 
127  return in_array($type, $safe) || in_array('all', $safe);
128  }
Twig_NodeTraverser is a node traverser.
$type
$env
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ needEscaping()

Twig_NodeVisitor_Escaper::needEscaping ( Twig_Environment  $env)
protected

Definition at line 130 of file Escaper.php.

Referenced by doEnterNode(), and doLeaveNode().

131  {
132  if (count($this->statusStack)) {
133  return $this->statusStack[count($this->statusStack) - 1];
134  }
135 
136  return $this->defaultStrategy ? $this->defaultStrategy : false;
137  }
+ Here is the caller graph for this function:

◆ preEscapeFilterNode()

Twig_NodeVisitor_Escaper::preEscapeFilterNode ( Twig_Node_Expression_Filter  $filter,
Twig_Environment  $env 
)
protected

Definition at line 93 of file Escaper.php.

References $name, $type, getEscaperFilter(), Twig_Environment\getFilter(), Twig_Node\getNode(), isSafeFor(), and Twig_Node\setNode().

Referenced by doLeaveNode().

94  {
95  $name = $filter->getNode('filter')->getAttribute('value');
96 
97  $type = $env->getFilter($name)->getPreEscape();
98  if (null === $type) {
99  return $filter;
100  }
101 
102  $node = $filter->getNode('node');
103  if ($this->isSafeFor($type, $node, $env)) {
104  return $filter;
105  }
106 
107  $filter->setNode('node', $this->getEscaperFilter($type, $node));
108 
109  return $filter;
110  }
$type
getFilter($name)
Get a filter by name.
isSafeFor($type, Twig_NodeInterface $expression, $env)
Definition: Escaper.php:112
setNode($name, $node=null)
Definition: Node.php:195
if($format !==null) $name
Definition: metadata.php:146
getNode($name)
Definition: Node.php:186
getEscaperFilter($type, Twig_NodeInterface $node)
Definition: Escaper.php:139
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $blocks

Twig_NodeVisitor_Escaper::$blocks = array()
protected

Definition at line 22 of file Escaper.php.

◆ $defaultStrategy

Twig_NodeVisitor_Escaper::$defaultStrategy = false
protected

Definition at line 25 of file Escaper.php.

Referenced by doEnterNode().

◆ $safeAnalysis

Twig_NodeVisitor_Escaper::$safeAnalysis
protected

Definition at line 23 of file Escaper.php.

◆ $safeVars

Twig_NodeVisitor_Escaper::$safeVars = array()
protected

Definition at line 26 of file Escaper.php.

◆ $statusStack

Twig_NodeVisitor_Escaper::$statusStack = array()
protected

Definition at line 21 of file Escaper.php.

◆ $traverser

Twig_NodeVisitor_Escaper::$traverser
protected

Definition at line 24 of file Escaper.php.


The documentation for this class was generated from the following file: