Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / symfony / css-selector / XPath / Translator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\XPath;
13
14 use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
15 use Symfony\Component\CssSelector\Node\FunctionNode;
16 use Symfony\Component\CssSelector\Node\NodeInterface;
17 use Symfony\Component\CssSelector\Node\SelectorNode;
18 use Symfony\Component\CssSelector\Parser\Parser;
19 use Symfony\Component\CssSelector\Parser\ParserInterface;
20
21 /**
22  * XPath expression translator interface.
23  *
24  * This component is a port of the Python cssselect library,
25  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
26  *
27  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
28  *
29  * @internal
30  */
31 class Translator implements TranslatorInterface
32 {
33     private $mainParser;
34
35     /**
36      * @var ParserInterface[]
37      */
38     private $shortcutParsers = array();
39
40     /**
41      * @var Extension\ExtensionInterface[]
42      */
43     private $extensions = array();
44
45     private $nodeTranslators = array();
46     private $combinationTranslators = array();
47     private $functionTranslators = array();
48     private $pseudoClassTranslators = array();
49     private $attributeMatchingTranslators = array();
50
51     public function __construct(ParserInterface $parser = null)
52     {
53         $this->mainParser = $parser ?: new Parser();
54
55         $this
56             ->registerExtension(new Extension\NodeExtension())
57             ->registerExtension(new Extension\CombinationExtension())
58             ->registerExtension(new Extension\FunctionExtension())
59             ->registerExtension(new Extension\PseudoClassExtension())
60             ->registerExtension(new Extension\AttributeMatchingExtension())
61         ;
62     }
63
64     /**
65      * @param string $element
66      *
67      * @return string
68      */
69     public static function getXpathLiteral($element)
70     {
71         if (false === strpos($element, "'")) {
72             return "'".$element."'";
73         }
74
75         if (false === strpos($element, '"')) {
76             return '"'.$element.'"';
77         }
78
79         $string = $element;
80         $parts = array();
81         while (true) {
82             if (false !== $pos = strpos($string, "'")) {
83                 $parts[] = sprintf("'%s'", substr($string, 0, $pos));
84                 $parts[] = "\"'\"";
85                 $string = substr($string, $pos + 1);
86             } else {
87                 $parts[] = "'$string'";
88                 break;
89             }
90         }
91
92         return sprintf('concat(%s)', implode(', ', $parts));
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
99     {
100         $selectors = $this->parseSelectors($cssExpr);
101
102         /** @var SelectorNode $selector */
103         foreach ($selectors as $index => $selector) {
104             if (null !== $selector->getPseudoElement()) {
105                 throw new ExpressionErrorException('Pseudo-elements are not supported.');
106             }
107
108             $selectors[$index] = $this->selectorToXPath($selector, $prefix);
109         }
110
111         return implode(' | ', $selectors);
112     }
113
114     /**
115      * {@inheritdoc}
116      */
117     public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
118     {
119         return ($prefix ?: '').$this->nodeToXPath($selector);
120     }
121
122     /**
123      * Registers an extension.
124      *
125      * @return $this
126      */
127     public function registerExtension(Extension\ExtensionInterface $extension)
128     {
129         $this->extensions[$extension->getName()] = $extension;
130
131         $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
132         $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
133         $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
134         $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
135         $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
136
137         return $this;
138     }
139
140     /**
141      * @param string $name
142      *
143      * @return Extension\ExtensionInterface
144      *
145      * @throws ExpressionErrorException
146      */
147     public function getExtension($name)
148     {
149         if (!isset($this->extensions[$name])) {
150             throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
151         }
152
153         return $this->extensions[$name];
154     }
155
156     /**
157      * Registers a shortcut parser.
158      *
159      * @return $this
160      */
161     public function registerParserShortcut(ParserInterface $shortcut)
162     {
163         $this->shortcutParsers[] = $shortcut;
164
165         return $this;
166     }
167
168     /**
169      * @return XPathExpr
170      *
171      * @throws ExpressionErrorException
172      */
173     public function nodeToXPath(NodeInterface $node)
174     {
175         if (!isset($this->nodeTranslators[$node->getNodeName()])) {
176             throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
177         }
178
179         return \call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
180     }
181
182     /**
183      * @param string        $combiner
184      * @param NodeInterface $xpath
185      * @param NodeInterface $combinedXpath
186      *
187      * @return XPathExpr
188      *
189      * @throws ExpressionErrorException
190      */
191     public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
192     {
193         if (!isset($this->combinationTranslators[$combiner])) {
194             throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
195         }
196
197         return \call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
198     }
199
200     /**
201      * @return XPathExpr
202      *
203      * @throws ExpressionErrorException
204      */
205     public function addFunction(XPathExpr $xpath, FunctionNode $function)
206     {
207         if (!isset($this->functionTranslators[$function->getName()])) {
208             throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
209         }
210
211         return \call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
212     }
213
214     /**
215      * @param XPathExpr $xpath
216      * @param string    $pseudoClass
217      *
218      * @return XPathExpr
219      *
220      * @throws ExpressionErrorException
221      */
222     public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
223     {
224         if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
225             throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
226         }
227
228         return \call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
229     }
230
231     /**
232      * @param XPathExpr $xpath
233      * @param string    $operator
234      * @param string    $attribute
235      * @param string    $value
236      *
237      * @return XPathExpr
238      *
239      * @throws ExpressionErrorException
240      */
241     public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
242     {
243         if (!isset($this->attributeMatchingTranslators[$operator])) {
244             throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
245         }
246
247         return \call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
248     }
249
250     /**
251      * @param string $css
252      *
253      * @return SelectorNode[]
254      */
255     private function parseSelectors($css)
256     {
257         foreach ($this->shortcutParsers as $shortcut) {
258             $tokens = $shortcut->parse($css);
259
260             if (!empty($tokens)) {
261                 return $tokens;
262             }
263         }
264
265         return $this->mainParser->parse($css);
266     }
267 }