| 148 |
lars |
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\Parser;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\CssSelector\Exception\SyntaxErrorException;
|
|
|
15 |
use Symfony\Component\CssSelector\Node;
|
|
|
16 |
use Symfony\Component\CssSelector\Parser\Tokenizer\Tokenizer;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* CSS selector parser.
|
|
|
20 |
*
|
|
|
21 |
* This component is a port of the Python cssselect library,
|
|
|
22 |
* which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
|
|
|
23 |
*
|
|
|
24 |
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
|
|
|
25 |
*
|
|
|
26 |
* @internal
|
|
|
27 |
*/
|
|
|
28 |
class Parser implements ParserInterface
|
|
|
29 |
{
|
|
|
30 |
private Tokenizer $tokenizer;
|
|
|
31 |
|
|
|
32 |
public function __construct(Tokenizer $tokenizer = null)
|
|
|
33 |
{
|
|
|
34 |
$this->tokenizer = $tokenizer ?? new Tokenizer();
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function parse(string $source): array
|
|
|
38 |
{
|
|
|
39 |
$reader = new Reader($source);
|
|
|
40 |
$stream = $this->tokenizer->tokenize($reader);
|
|
|
41 |
|
|
|
42 |
return $this->parseSelectorList($stream);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Parses the arguments for ":nth-child()" and friends.
|
|
|
47 |
*
|
|
|
48 |
* @param Token[] $tokens
|
|
|
49 |
*
|
|
|
50 |
* @throws SyntaxErrorException
|
|
|
51 |
*/
|
|
|
52 |
public static function parseSeries(array $tokens): array
|
|
|
53 |
{
|
|
|
54 |
foreach ($tokens as $token) {
|
|
|
55 |
if ($token->isString()) {
|
|
|
56 |
throw SyntaxErrorException::stringAsFunctionArgument();
|
|
|
57 |
}
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
$joined = trim(implode('', array_map(function (Token $token) {
|
|
|
61 |
return $token->getValue();
|
|
|
62 |
}, $tokens)));
|
|
|
63 |
|
|
|
64 |
$int = function ($string) {
|
|
|
65 |
if (!is_numeric($string)) {
|
|
|
66 |
throw SyntaxErrorException::stringAsFunctionArgument();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
return (int) $string;
|
|
|
70 |
};
|
|
|
71 |
|
|
|
72 |
switch (true) {
|
|
|
73 |
case 'odd' === $joined:
|
|
|
74 |
return [2, 1];
|
|
|
75 |
case 'even' === $joined:
|
|
|
76 |
return [2, 0];
|
|
|
77 |
case 'n' === $joined:
|
|
|
78 |
return [1, 0];
|
|
|
79 |
case !str_contains($joined, 'n'):
|
|
|
80 |
return [0, $int($joined)];
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$split = explode('n', $joined);
|
|
|
84 |
$first = $split[0] ?? null;
|
|
|
85 |
|
|
|
86 |
return [
|
|
|
87 |
$first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
|
|
|
88 |
isset($split[1]) && $split[1] ? $int($split[1]) : 0,
|
|
|
89 |
];
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function parseSelectorList(TokenStream $stream): array
|
|
|
93 |
{
|
|
|
94 |
$stream->skipWhitespace();
|
|
|
95 |
$selectors = [];
|
|
|
96 |
|
|
|
97 |
while (true) {
|
|
|
98 |
$selectors[] = $this->parserSelectorNode($stream);
|
|
|
99 |
|
|
|
100 |
if ($stream->getPeek()->isDelimiter([','])) {
|
|
|
101 |
$stream->getNext();
|
|
|
102 |
$stream->skipWhitespace();
|
|
|
103 |
} else {
|
|
|
104 |
break;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
return $selectors;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
private function parserSelectorNode(TokenStream $stream): Node\SelectorNode
|
|
|
112 |
{
|
|
|
113 |
[$result, $pseudoElement] = $this->parseSimpleSelector($stream);
|
|
|
114 |
|
|
|
115 |
while (true) {
|
|
|
116 |
$stream->skipWhitespace();
|
|
|
117 |
$peek = $stream->getPeek();
|
|
|
118 |
|
|
|
119 |
if ($peek->isFileEnd() || $peek->isDelimiter([','])) {
|
|
|
120 |
break;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
if (null !== $pseudoElement) {
|
|
|
124 |
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
if ($peek->isDelimiter(['+', '>', '~'])) {
|
|
|
128 |
$combinator = $stream->getNext()->getValue();
|
|
|
129 |
$stream->skipWhitespace();
|
|
|
130 |
} else {
|
|
|
131 |
$combinator = ' ';
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
[$nextSelector, $pseudoElement] = $this->parseSimpleSelector($stream);
|
|
|
135 |
$result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return new Node\SelectorNode($result, $pseudoElement);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Parses next simple node (hash, class, pseudo, negation).
|
|
|
143 |
*
|
|
|
144 |
* @throws SyntaxErrorException
|
|
|
145 |
*/
|
|
|
146 |
private function parseSimpleSelector(TokenStream $stream, bool $insideNegation = false): array
|
|
|
147 |
{
|
|
|
148 |
$stream->skipWhitespace();
|
|
|
149 |
|
|
|
150 |
$selectorStart = \count($stream->getUsed());
|
|
|
151 |
$result = $this->parseElementNode($stream);
|
|
|
152 |
$pseudoElement = null;
|
|
|
153 |
|
|
|
154 |
while (true) {
|
|
|
155 |
$peek = $stream->getPeek();
|
|
|
156 |
if ($peek->isWhitespace()
|
|
|
157 |
|| $peek->isFileEnd()
|
|
|
158 |
|| $peek->isDelimiter([',', '+', '>', '~'])
|
|
|
159 |
|| ($insideNegation && $peek->isDelimiter([')']))
|
|
|
160 |
) {
|
|
|
161 |
break;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
if (null !== $pseudoElement) {
|
|
|
165 |
throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if ($peek->isHash()) {
|
|
|
169 |
$result = new Node\HashNode($result, $stream->getNext()->getValue());
|
|
|
170 |
} elseif ($peek->isDelimiter(['.'])) {
|
|
|
171 |
$stream->getNext();
|
|
|
172 |
$result = new Node\ClassNode($result, $stream->getNextIdentifier());
|
|
|
173 |
} elseif ($peek->isDelimiter(['['])) {
|
|
|
174 |
$stream->getNext();
|
|
|
175 |
$result = $this->parseAttributeNode($result, $stream);
|
|
|
176 |
} elseif ($peek->isDelimiter([':'])) {
|
|
|
177 |
$stream->getNext();
|
|
|
178 |
|
|
|
179 |
if ($stream->getPeek()->isDelimiter([':'])) {
|
|
|
180 |
$stream->getNext();
|
|
|
181 |
$pseudoElement = $stream->getNextIdentifier();
|
|
|
182 |
|
|
|
183 |
continue;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
$identifier = $stream->getNextIdentifier();
|
|
|
187 |
if (\in_array(strtolower($identifier), ['first-line', 'first-letter', 'before', 'after'])) {
|
|
|
188 |
// Special case: CSS 2.1 pseudo-elements can have a single ':'.
|
|
|
189 |
// Any new pseudo-element must have two.
|
|
|
190 |
$pseudoElement = $identifier;
|
|
|
191 |
|
|
|
192 |
continue;
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if (!$stream->getPeek()->isDelimiter(['('])) {
|
|
|
196 |
$result = new Node\PseudoNode($result, $identifier);
|
|
|
197 |
|
|
|
198 |
continue;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
$stream->getNext();
|
|
|
202 |
$stream->skipWhitespace();
|
|
|
203 |
|
|
|
204 |
if ('not' === strtolower($identifier)) {
|
|
|
205 |
if ($insideNegation) {
|
|
|
206 |
throw SyntaxErrorException::nestedNot();
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
[$argument, $argumentPseudoElement] = $this->parseSimpleSelector($stream, true);
|
|
|
210 |
$next = $stream->getNext();
|
|
|
211 |
|
|
|
212 |
if (null !== $argumentPseudoElement) {
|
|
|
213 |
throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
if (!$next->isDelimiter([')'])) {
|
|
|
217 |
throw SyntaxErrorException::unexpectedToken('")"', $next);
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
$result = new Node\NegationNode($result, $argument);
|
|
|
221 |
} else {
|
|
|
222 |
$arguments = [];
|
|
|
223 |
$next = null;
|
|
|
224 |
|
|
|
225 |
while (true) {
|
|
|
226 |
$stream->skipWhitespace();
|
|
|
227 |
$next = $stream->getNext();
|
|
|
228 |
|
|
|
229 |
if ($next->isIdentifier()
|
|
|
230 |
|| $next->isString()
|
|
|
231 |
|| $next->isNumber()
|
|
|
232 |
|| $next->isDelimiter(['+', '-'])
|
|
|
233 |
) {
|
|
|
234 |
$arguments[] = $next;
|
|
|
235 |
} elseif ($next->isDelimiter([')'])) {
|
|
|
236 |
break;
|
|
|
237 |
} else {
|
|
|
238 |
throw SyntaxErrorException::unexpectedToken('an argument', $next);
|
|
|
239 |
}
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
if (!$arguments) {
|
|
|
243 |
throw SyntaxErrorException::unexpectedToken('at least one argument', $next);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
$result = new Node\FunctionNode($result, $identifier, $arguments);
|
|
|
247 |
}
|
|
|
248 |
} else {
|
|
|
249 |
throw SyntaxErrorException::unexpectedToken('selector', $peek);
|
|
|
250 |
}
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
if (\count($stream->getUsed()) === $selectorStart) {
|
|
|
254 |
throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
return [$result, $pseudoElement];
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
private function parseElementNode(TokenStream $stream): Node\ElementNode
|
|
|
261 |
{
|
|
|
262 |
$peek = $stream->getPeek();
|
|
|
263 |
|
|
|
264 |
if ($peek->isIdentifier() || $peek->isDelimiter(['*'])) {
|
|
|
265 |
if ($peek->isIdentifier()) {
|
|
|
266 |
$namespace = $stream->getNext()->getValue();
|
|
|
267 |
} else {
|
|
|
268 |
$stream->getNext();
|
|
|
269 |
$namespace = null;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
if ($stream->getPeek()->isDelimiter(['|'])) {
|
|
|
273 |
$stream->getNext();
|
|
|
274 |
$element = $stream->getNextIdentifierOrStar();
|
|
|
275 |
} else {
|
|
|
276 |
$element = $namespace;
|
|
|
277 |
$namespace = null;
|
|
|
278 |
}
|
|
|
279 |
} else {
|
|
|
280 |
$element = $namespace = null;
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
return new Node\ElementNode($namespace, $element);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream): Node\AttributeNode
|
|
|
287 |
{
|
|
|
288 |
$stream->skipWhitespace();
|
|
|
289 |
$attribute = $stream->getNextIdentifierOrStar();
|
|
|
290 |
|
|
|
291 |
if (null === $attribute && !$stream->getPeek()->isDelimiter(['|'])) {
|
|
|
292 |
throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
if ($stream->getPeek()->isDelimiter(['|'])) {
|
|
|
296 |
$stream->getNext();
|
|
|
297 |
|
|
|
298 |
if ($stream->getPeek()->isDelimiter(['='])) {
|
|
|
299 |
$namespace = null;
|
|
|
300 |
$stream->getNext();
|
|
|
301 |
$operator = '|=';
|
|
|
302 |
} else {
|
|
|
303 |
$namespace = $attribute;
|
|
|
304 |
$attribute = $stream->getNextIdentifier();
|
|
|
305 |
$operator = null;
|
|
|
306 |
}
|
|
|
307 |
} else {
|
|
|
308 |
$namespace = $operator = null;
|
|
|
309 |
}
|
|
|
310 |
|
|
|
311 |
if (null === $operator) {
|
|
|
312 |
$stream->skipWhitespace();
|
|
|
313 |
$next = $stream->getNext();
|
|
|
314 |
|
|
|
315 |
if ($next->isDelimiter([']'])) {
|
|
|
316 |
return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
|
|
|
317 |
} elseif ($next->isDelimiter(['='])) {
|
|
|
318 |
$operator = '=';
|
|
|
319 |
} elseif ($next->isDelimiter(['^', '$', '*', '~', '|', '!'])
|
|
|
320 |
&& $stream->getPeek()->isDelimiter(['='])
|
|
|
321 |
) {
|
|
|
322 |
$operator = $next->getValue().'=';
|
|
|
323 |
$stream->getNext();
|
|
|
324 |
} else {
|
|
|
325 |
throw SyntaxErrorException::unexpectedToken('operator', $next);
|
|
|
326 |
}
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
$stream->skipWhitespace();
|
|
|
330 |
$value = $stream->getNext();
|
|
|
331 |
|
|
|
332 |
if ($value->isNumber()) {
|
|
|
333 |
// if the value is a number, it's casted into a string
|
|
|
334 |
$value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());
|
|
|
335 |
}
|
|
|
336 |
|
|
|
337 |
if (!($value->isIdentifier() || $value->isString())) {
|
|
|
338 |
throw SyntaxErrorException::unexpectedToken('string or identifier', $value);
|
|
|
339 |
}
|
|
|
340 |
|
|
|
341 |
$stream->skipWhitespace();
|
|
|
342 |
$next = $stream->getNext();
|
|
|
343 |
|
|
|
344 |
if (!$next->isDelimiter([']'])) {
|
|
|
345 |
throw SyntaxErrorException::unexpectedToken('"]"', $next);
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());
|
|
|
349 |
}
|
|
|
350 |
}
|