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 / Extension / EscaperExtension.php

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

167 lines 6.5 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\Extension;
12
13 use ElementorDeps\Twig\Environment;
14 use ElementorDeps\Twig\FileExtensionEscapingStrategy;
15 use ElementorDeps\Twig\Node\Expression\ConstantExpression;
16 use ElementorDeps\Twig\Node\Expression\Filter\RawFilter;
17 use ElementorDeps\Twig\Node\Node;
18 use ElementorDeps\Twig\NodeVisitor\EscaperNodeVisitor;
19 use ElementorDeps\Twig\Runtime\EscaperRuntime;
20 use ElementorDeps\Twig\TokenParser\AutoEscapeTokenParser;
21 use ElementorDeps\Twig\TwigFilter;
22 final class EscaperExtension extends AbstractExtension
23 {
24 private $environment;
25 private $escapers = [];
26 private $escaper;
27 private $defaultStrategy;
28 /**
29 * @param string|false|callable $defaultStrategy An escaping strategy
30 *
31 * @see setDefaultStrategy()
32 */
33 public function __construct($defaultStrategy = 'html')
34 {
35 $this->setDefaultStrategy($defaultStrategy);
36 }
37 public function getTokenParsers() : array
38 {
39 return [new AutoEscapeTokenParser()];
40 }
41 public function getNodeVisitors() : array
42 {
43 return [new EscaperNodeVisitor()];
44 }
45 public function getFilters() : array
46 {
47 return [new TwigFilter('escape', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]), new TwigFilter('e', [EscaperRuntime::class, 'escape'], ['is_safe_callback' => [self::class, 'escapeFilterIsSafe']]), new TwigFilter('raw', null, ['is_safe' => ['all'], 'node_class' => RawFilter::class])];
48 }
49 /**
50 * @deprecated since Twig 3.10
51 */
52 public function setEnvironment(Environment $environment, bool $triggerDeprecation = \true) : void
53 {
54 if ($triggerDeprecation) {
55 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated and not needed if you are using methods from "Twig\\Runtime\\EscaperRuntime".', __METHOD__);
56 }
57 $this->environment = $environment;
58 $this->escaper = $environment->getRuntime(EscaperRuntime::class);
59 }
60 /**
61 * @deprecated since Twig 3.10
62 */
63 public function setEscaperRuntime(EscaperRuntime $escaper)
64 {
65 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated and not needed if you are using methods from "Twig\\Runtime\\EscaperRuntime".', __METHOD__);
66 $this->escaper = $escaper;
67 }
68 /**
69 * Sets the default strategy to use when not defined by the user.
70 *
71 * The strategy can be a valid PHP callback that takes the template
72 * name as an argument and returns the strategy to use.
73 *
74 * @param string|false|callable(string $templateName): string $defaultStrategy An escaping strategy
75 */
76 public function setDefaultStrategy($defaultStrategy) : void
77 {
78 if ('name' === $defaultStrategy) {
79 $defaultStrategy = [FileExtensionEscapingStrategy::class, 'guess'];
80 }
81 $this->defaultStrategy = $defaultStrategy;
82 }
83 /**
84 * Gets the default strategy to use when not defined by the user.
85 *
86 * @param string $name The template name
87 *
88 * @return string|false The default strategy to use for the template
89 */
90 public function getDefaultStrategy(string $name)
91 {
92 // disable string callables to avoid calling a function named html or js,
93 // or any other upcoming escaping strategy
94 if (!\is_string($this->defaultStrategy) && \false !== $this->defaultStrategy) {
95 return \call_user_func($this->defaultStrategy, $name);
96 }
97 return $this->defaultStrategy;
98 }
99 /**
100 * Defines a new escaper to be used via the escape filter.
101 *
102 * @param string $strategy The strategy name that should be used as a strategy in the escape call
103 * @param callable(Environment, string, string): string $callable A valid PHP callable
104 *
105 * @deprecated since Twig 3.10
106 */
107 public function setEscaper($strategy, callable $callable)
108 {
109 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\\Runtime\\EscaperRuntime::setEscaper()" method instead (be warned that Environment is not passed anymore to the callable).', __METHOD__);
110 if (!isset($this->environment)) {
111 throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
112 }
113 $this->escapers[$strategy] = $callable;
114 $callable = function ($string, $charset) use($callable) {
115 return $callable($this->environment, $string, $charset);
116 };
117 $this->escaper->setEscaper($strategy, $callable);
118 }
119 /**
120 * Gets all defined escapers.
121 *
122 * @return array<string, callable(Environment, string, string): string> An array of escapers
123 *
124 * @deprecated since Twig 3.10
125 */
126 public function getEscapers()
127 {
128 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\\Runtime\\EscaperRuntime::getEscaper()" method instead.', __METHOD__);
129 return $this->escapers;
130 }
131 /**
132 * @deprecated since Twig 3.10
133 */
134 public function setSafeClasses(array $safeClasses = [])
135 {
136 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\\Runtime\\EscaperRuntime::setSafeClasses()" method instead.', __METHOD__);
137 if (!isset($this->escaper)) {
138 throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
139 }
140 $this->escaper->setSafeClasses($safeClasses);
141 }
142 /**
143 * @deprecated since Twig 3.10
144 */
145 public function addSafeClass(string $class, array $strategies)
146 {
147 trigger_deprecation('twig/twig', '3.10', 'The "%s()" method is deprecated, use the "Twig\\Runtime\\EscaperRuntime::addSafeClass()" method instead.', __METHOD__);
148 if (!isset($this->escaper)) {
149 throw new \LogicException(\sprintf('You must call "setEnvironment()" before calling "%s()".', __METHOD__));
150 }
151 $this->escaper->addSafeClass($class, $strategies);
152 }
153 /**
154 * @internal
155 */
156 public static function escapeFilterIsSafe(Node $filterArgs)
157 {
158 foreach ($filterArgs as $arg) {
159 if ($arg instanceof ConstantExpression) {
160 return [$arg->getAttribute('value')];
161 }
162 return [];
163 }
164 return ['html'];
165 }
166 }
167