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