Merge pull request #4036 from dokuwiki/issue4033
[dokuwiki.git] / _test / vendor / symfony / css-selector / Parser / Parser.php
blobd73489edfb48157e2ddad526b5f335cb1f94f832
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\Parser;
14 use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
15 use Symfony\Component\CssSelector\Node;
16 use Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
18 /**
19 * CSS selector parser.
21 * This component is a port of the Python cssselect library,
22 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
24 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
26 * @internal
28 class Parser implements ParserInterface
30 private $tokenizer;
32 public function __construct(Tokenizer $tokenizer = null)
34 $this->tokenizer = $tokenizer ?? new Tokenizer();
37 /**
38 * {@inheritdoc}
40 public function parse(string $source): array
42 $reader = new Reader($source);
43 $stream = $this->tokenizer->tokenize($reader);
45 return $this->parseSelectorList($stream);
48 /**
49 * Parses the arguments for ":nth-child()" and friends.
51 * @param Token[] $tokens
53 * @throws SyntaxErrorException
55 public static function parseSeries(array $tokens): array
57 foreach ($tokens as $token) {
58 if ($token->isString()) {
59 throw SyntaxErrorException::stringAsFunctionArgument();
63 $joined = trim(implode('', array_map(function (Token $token) {
64 return $token->getValue();
65 }, $tokens)));
67 $int = function ($string) {
68 if (!is_numeric($string)) {
69 throw SyntaxErrorException::stringAsFunctionArgument();
72 return (int) $string;
75 switch (true) {
76 case 'odd' === $joined:
77 return [2, 1];
78 case 'even' === $joined:
79 return [2, 0];
80 case 'n' === $joined:
81 return [1, 0];
82 case !str_contains($joined, 'n'):
83 return [0, $int($joined)];
86 $split = explode('n', $joined);
87 $first = $split[0] ?? null;
89 return [
90 $first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
91 isset($split[1]) && $split[1] ? $int($split[1]) : 0,
95 private function parseSelectorList(TokenStream $stream): array
97 $stream->skipWhitespace();
98 $selectors = [];
100 while (true) {
101 $selectors[] = $this->parserSelectorNode($stream);
103 if ($stream->getPeek()->isDelimiter([','])) {
104 $stream->getNext();
105 $stream->skipWhitespace();
106 } else {
107 break;
111 return $selectors;
114 private function parserSelectorNode(TokenStream $stream): Node\SelectorNode
116 [$result, $pseudoElement] = $this->parseSimpleSelector($stream);
118 while (true) {
119 $stream->skipWhitespace();
120 $peek = $stream->getPeek();
122 if ($peek->isFileEnd() || $peek->isDelimiter([','])) {
123 break;
126 if (null !== $pseudoElement) {
127 throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
130 if ($peek->isDelimiter(['+', '>', '~'])) {
131 $combinator = $stream->getNext()->getValue();
132 $stream->skipWhitespace();
133 } else {
134 $combinator = ' ';
137 [$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream);
138 $result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
141 return new Node\SelectorNode($result, $pseudoElement);
145 * Parses next simple node (hash, class, pseudo, negation).
147 * @throws SyntaxErrorException
149 private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
151 $stream->skipWhitespace();
153 $selectorStart = \count($stream->getUsed());
154 $result = $this->parseElementNode($stream);
155 $pseudoElement = null;
157 while (true) {
158 $peek = $stream->getPeek();
159 if ($peek->isWhitespace()
160 || $peek->isFileEnd()
161 || $peek->isDelimiter([',', '+', '>', '~'])
162 || ($insideNegation && $peek->isDelimiter([')']))
164 break;
167 if (null !== $pseudoElement) {
168 throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
171 if ($peek->isHash()) {
172 $result = new Node\HashNode($result, $stream->getNext()->getValue());
173 } elseif ($peek->isDelimiter(['.'])) {
174 $stream->getNext();
175 $result = new Node\ClassNode($result, $stream->getNextIdentifier());
176 } elseif ($peek->isDelimiter(['['])) {
177 $stream->getNext();
178 $result = $this->parseAttributeNode($result, $stream);
179 } elseif ($peek->isDelimiter([':'])) {
180 $stream->getNext();
182 if ($stream->getPeek()->isDelimiter([':'])) {
183 $stream->getNext();
184 $pseudoElement = $stream->getNextIdentifier();
186 continue;
189 $identifier = $stream->getNextIdentifier();
190 if (\in_array(strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) {
191 // Special case: CSS 2.1 pseudo-elements can have a single ':'.
192 // Any new pseudo-element must have two.
193 $pseudoElement = $identifier;
195 continue;
198 if (!$stream->getPeek()->isDelimiter(['('])) {
199 $result = new Node\PseudoNode($result, $identifier);
201 continue;
204 $stream->getNext();
205 $stream->skipWhitespace();
207 if ('not' === strtolower($identifier)) {
208 if ($insideNegation) {
209 throw SyntaxErrorException::nestedNot();
212 [$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true);
213 $next = $stream->getNext();
215 if (null !== $argumentPseudoElement) {
216 throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
219 if (!$next->isDelimiter([')'])) {
220 throw SyntaxErrorException::unexpectedToken('")"', $next);
223 $result = new Node\NegationNode($result, $argument);
224 } else {
225 $arguments = [];
226 $next = null;
228 while (true) {
229 $stream->skipWhitespace();
230 $next = $stream->getNext();
232 if ($next->isIdentifier()
233 || $next->isString()
234 || $next->isNumber()
235 || $next->isDelimiter(['+', '-'])
237 $arguments[] = $next;
238 } elseif ($next->isDelimiter([')'])) {
239 break;
240 } else {
241 throw SyntaxErrorException::unexpectedToken('an argument', $next);
245 if (empty($arguments)) {
246 throw SyntaxErrorException::unexpectedToken('at least one argument', $next);
249 $result = new Node\FunctionNode($result, $identifier, $arguments);
251 } else {
252 throw SyntaxErrorException::unexpectedToken('selector', $peek);
256 if (\count($stream->getUsed()) === $selectorStart) {
257 throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
260 return [$result, $pseudoElement];
263 private function parseElementNode(TokenStream $stream): Node\ElementNode
265 $peek = $stream->getPeek();
267 if ($peek->isIdentifier() || $peek->isDelimiter(['*'])) {
268 if ($peek->isIdentifier()) {
269 $namespace = $stream->getNext()->getValue();
270 } else {
271 $stream->getNext();
272 $namespace = null;
275 if ($stream->getPeek()->isDelimiter(['|'])) {
276 $stream->getNext();
277 $element = $stream->getNextIdentifierOrStar();
278 } else {
279 $element = $namespace;
280 $namespace = null;
282 } else {
283 $element = $namespace = null;
286 return new Node\ElementNode($namespace, $element);
289 private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream): Node\AttributeNode
291 $stream->skipWhitespace();
292 $attribute = $stream->getNextIdentifierOrStar();
294 if (null === $attribute && !$stream->getPeek()->isDelimiter(['|'])) {
295 throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
298 if ($stream->getPeek()->isDelimiter(['|'])) {
299 $stream->getNext();
301 if ($stream->getPeek()->isDelimiter(['='])) {
302 $namespace = null;
303 $stream->getNext();
304 $operator = '|=';
305 } else {
306 $namespace = $attribute;
307 $attribute = $stream->getNextIdentifier();
308 $operator = null;
310 } else {
311 $namespace = $operator = null;
314 if (null === $operator) {
315 $stream->skipWhitespace();
316 $next = $stream->getNext();
318 if ($next->isDelimiter([']'])) {
319 return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
320 } elseif ($next->isDelimiter(['='])) {
321 $operator = '=';
322 } elseif ($next->isDelimiter(['^', '$', '*', '~', '|', '!'])
323 && $stream->getPeek()->isDelimiter(['='])
325 $operator = $next->getValue().'=';
326 $stream->getNext();
327 } else {
328 throw SyntaxErrorException::unexpectedToken('operator', $next);
332 $stream->skipWhitespace();
333 $value = $stream->getNext();
335 if ($value->isNumber()) {
336 // if the value is a number, it's casted into a string
337 $value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());
340 if (!($value->isIdentifier() || $value->isString())) {
341 throw SyntaxErrorException::unexpectedToken('string or identifier', $value);
344 $stream->skipWhitespace();
345 $next = $stream->getNext();
347 if (!$next->isDelimiter([']'])) {
348 throw SyntaxErrorException::unexpectedToken('"]"', $next);
351 return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());