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