PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev1
Elementor Website Builder – more than just a page builder v3.28.0-dev1
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / vendor_prefixed / twig / src / NodeVisitor / EscaperNodeVisitor.php

EscaperNodeVisitor.php in Elementor Website Builder – more than just a page builder 3.28.0-dev1, at vendor_prefixed/twig/src/NodeVisitor/EscaperNodeVisitor.php

170 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 namespace ElementorDeps\Twig\NodeVisitor;
12
13 use ElementorDeps\Twig\Environment;
14 use ElementorDeps\Twig\Extension\EscaperExtension;
15 use ElementorDeps\Twig\Node\AutoEscapeNode;
16 use ElementorDeps\Twig\Node\BlockNode;
17 use ElementorDeps\Twig\Node\BlockReferenceNode;
18 use ElementorDeps\Twig\Node\DoNode;
19 use ElementorDeps\Twig\Node\Expression\ConditionalExpression;
20 use ElementorDeps\Twig\Node\Expression\ConstantExpression;
21 use ElementorDeps\Twig\Node\Expression\FilterExpression;
22 use ElementorDeps\Twig\Node\Expression\InlinePrint;
23 use ElementorDeps\Twig\Node\ImportNode;
24 use ElementorDeps\Twig\Node\ModuleNode;
25 use ElementorDeps\Twig\Node\Node;
26 use ElementorDeps\Twig\Node\PrintNode;
27 use ElementorDeps\Twig\NodeTraverser;
28 /**
29 * @author Fabien Potencier <fabien@symfony.com>
30 *
31 * @internal
32 */
33 final class EscaperNodeVisitor implements NodeVisitorInterface
34 {
35 private $statusStack = [];
36 private $blocks = [];
37 private $safeAnalysis;
38 private $traverser;
39 private $defaultStrategy = \false;
40 private $safeVars = [];
41 public function __construct()
42 {
43 $this->safeAnalysis = new SafeAnalysisNodeVisitor();
44 }
45 public function enterNode(Node $node, Environment $env) : Node
46 {
47 if ($node instanceof ModuleNode) {
48 if ($env->hasExtension(EscaperExtension::class) && ($defaultStrategy = $env->getExtension(EscaperExtension::class)->getDefaultStrategy($node->getTemplateName()))) {
49 $this->defaultStrategy = $defaultStrategy;
50 }
51 $this->safeVars = [];
52 $this->blocks = [];
53 } elseif ($node instanceof AutoEscapeNode) {
54 $this->statusStack[] = $node->getAttribute('value');
55 } elseif ($node instanceof BlockNode) {
56 $this->statusStack[] = $this->blocks[$node->getAttribute('name')] ?? $this->needEscaping();
57 } elseif ($node instanceof ImportNode) {
58 $this->safeVars[] = $node->getNode('var')->getAttribute('name');
59 }
60 return $node;
61 }
62 public function leaveNode(Node $node, Environment $env) : ?Node
63 {
64 if ($node instanceof ModuleNode) {
65 $this->defaultStrategy = \false;
66 $this->safeVars = [];
67 $this->blocks = [];
68 } elseif ($node instanceof FilterExpression) {
69 return $this->preEscapeFilterNode($node, $env);
70 } elseif ($node instanceof PrintNode && \false !== ($type = $this->needEscaping())) {
71 $expression = $node->getNode('expr');
72 if ($expression instanceof ConditionalExpression && $this->shouldUnwrapConditional($expression, $env, $type)) {
73 return new DoNode($this->unwrapConditional($expression, $env, $type), $expression->getTemplateLine());
74 }
75 return $this->escapePrintNode($node, $env, $type);
76 }
77 if ($node instanceof AutoEscapeNode || $node instanceof BlockNode) {
78 \array_pop($this->statusStack);
79 } elseif ($node instanceof BlockReferenceNode) {
80 $this->blocks[$node->getAttribute('name')] = $this->needEscaping();
81 }
82 return $node;
83 }
84 private function shouldUnwrapConditional(ConditionalExpression $expression, Environment $env, string $type) : bool
85 {
86 $expr2Safe = $this->isSafeFor($type, $expression->getNode('expr2'), $env);
87 $expr3Safe = $this->isSafeFor($type, $expression->getNode('expr3'), $env);
88 return $expr2Safe !== $expr3Safe;
89 }
90 private function unwrapConditional(ConditionalExpression $expression, Environment $env, string $type) : ConditionalExpression
91 {
92 // convert "echo a ? b : c" to "a ? echo b : echo c" recursively
93 $expr2 = $expression->getNode('expr2');
94 if ($expr2 instanceof ConditionalExpression && $this->shouldUnwrapConditional($expr2, $env, $type)) {
95 $expr2 = $this->unwrapConditional($expr2, $env, $type);
96 } else {
97 $expr2 = $this->escapeInlinePrintNode(new InlinePrint($expr2, $expr2->getTemplateLine()), $env, $type);
98 }
99 $expr3 = $expression->getNode('expr3');
100 if ($expr3 instanceof ConditionalExpression && $this->shouldUnwrapConditional($expr3, $env, $type)) {
101 $expr3 = $this->unwrapConditional($expr3, $env, $type);
102 } else {
103 $expr3 = $this->escapeInlinePrintNode(new InlinePrint($expr3, $expr3->getTemplateLine()), $env, $type);
104 }
105 return new ConditionalExpression($expression->getNode('expr1'), $expr2, $expr3, $expression->getTemplateLine());
106 }
107 private function escapeInlinePrintNode(InlinePrint $node, Environment $env, string $type) : Node
108 {
109 $expression = $node->getNode('node');
110 if ($this->isSafeFor($type, $expression, $env)) {
111 return $node;
112 }
113 return new InlinePrint($this->getEscaperFilter($type, $expression), $node->getTemplateLine());
114 }
115 private function escapePrintNode(PrintNode $node, Environment $env, string $type) : Node
116 {
117 $expression = $node->getNode('expr');
118 if ($this->isSafeFor($type, $expression, $env)) {
119 return $node;
120 }
121 $class = \get_class($node);
122 return new $class($this->getEscaperFilter($type, $expression), $node->getTemplateLine());
123 }
124 private function preEscapeFilterNode(FilterExpression $filter, Environment $env) : FilterExpression
125 {
126 $name = $filter->getNode('filter')->getAttribute('value');
127 $type = $env->getFilter($name)->getPreEscape();
128 if (null === $type) {
129 return $filter;
130 }
131 $node = $filter->getNode('node');
132 if ($this->isSafeFor($type, $node, $env)) {
133 return $filter;
134 }
135 $filter->setNode('node', $this->getEscaperFilter($type, $node));
136 return $filter;
137 }
138 private function isSafeFor(string $type, Node $expression, Environment $env) : bool
139 {
140 $safe = $this->safeAnalysis->getSafe($expression);
141 if (null === $safe) {
142 if (null === $this->traverser) {
143 $this->traverser = new NodeTraverser($env, [$this->safeAnalysis]);
144 }
145 $this->safeAnalysis->setSafeVars($this->safeVars);
146 $this->traverser->traverse($expression);
147 $safe = $this->safeAnalysis->getSafe($expression);
148 }
149 return \in_array($type, $safe) || \in_array('all', $safe);
150 }
151 private function needEscaping()
152 {
153 if (\count($this->statusStack)) {
154 return $this->statusStack[\count($this->statusStack) - 1];
155 }
156 return $this->defaultStrategy ?: \false;
157 }
158 private function getEscaperFilter(string $type, Node $node) : FilterExpression
159 {
160 $line = $node->getTemplateLine();
161 $name = new ConstantExpression('escape', $line);
162 $args = new Node([new ConstantExpression($type, $line), new ConstantExpression(null, $line), new ConstantExpression(\true, $line)]);
163 return new FilterExpression($node, $name, $args, $line);
164 }
165 public function getPriority() : int
166 {
167 return 0;
168 }
169 }
170