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;
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>
28 class Parser implements ParserInterface
38 * @param null|Tokenizer $tokenizer
40 public function __construct(Tokenizer $tokenizer = null)
42 $this->tokenizer = $tokenizer ?: new Tokenizer();
48 public function parse($source)
50 $reader = new Reader($source);
51 $stream = $this->tokenizer->tokenize($reader);
53 return $this->parseSelectorList($stream);
57 * Parses the arguments for ":nth-child()" and friends.
59 * @param Token[] $tokens
63 * @throws SyntaxErrorException
65 public static function parseSeries(array $tokens)
67 foreach ($tokens as $token) {
68 if ($token->isString()) {
69 throw SyntaxErrorException::stringAsFunctionArgument();
73 $joined = trim(implode('', array_map(function (Token $token) {
74 return $token->getValue();
77 $int = function ($string) {
78 if (!is_numeric($string)) {
79 throw SyntaxErrorException::stringAsFunctionArgument();
86 case 'odd' === $joined:
88 case 'even' === $joined:
92 case false === strpos($joined, 'n'):
93 return array(0, $int($joined));
96 $split = explode('n', $joined);
97 $first = isset($split[0]) ? $split[0] : null;
100 $first ? ('-' === $first || '+' === $first ? $int($first.'1') : $int($first)) : 1,
101 isset($split[1]) && $split[1] ? $int($split[1]) : 0,
106 * Parses selector nodes.
108 * @param TokenStream $stream
112 private function parseSelectorList(TokenStream $stream)
114 $stream->skipWhitespace();
115 $selectors = array();
118 $selectors[] = $this->parserSelectorNode($stream);
120 if ($stream->getPeek()->isDelimiter(array(','))) {
122 $stream->skipWhitespace();
132 * Parses next selector or combined node.
134 * @param TokenStream $stream
136 * @return Node\SelectorNode
138 * @throws SyntaxErrorException
140 private function parserSelectorNode(TokenStream $stream)
142 list($result, $pseudoElement) = $this->parseSimpleSelector($stream);
145 $stream->skipWhitespace();
146 $peek = $stream->getPeek();
148 if ($peek->isFileEnd() || $peek->isDelimiter(array(','))) {
152 if (null !== $pseudoElement) {
153 throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
156 if ($peek->isDelimiter(array('+', '>', '~'))) {
157 $combinator = $stream->getNext()->getValue();
158 $stream->skipWhitespace();
163 list($nextSelector, $pseudoElement) = $this->parseSimpleSelector($stream);
164 $result = new Node\CombinedSelectorNode($result, $combinator, $nextSelector);
167 return new Node\SelectorNode($result, $pseudoElement);
171 * Parses next simple node (hash, class, pseudo, negation).
173 * @param TokenStream $stream
174 * @param bool $insideNegation
178 * @throws SyntaxErrorException
180 private function parseSimpleSelector(TokenStream $stream, $insideNegation = false)
182 $stream->skipWhitespace();
184 $selectorStart = count($stream->getUsed());
185 $result = $this->parseElementNode($stream);
186 $pseudoElement = null;
189 $peek = $stream->getPeek();
190 if ($peek->isWhitespace()
191 || $peek->isFileEnd()
192 || $peek->isDelimiter(array(',', '+', '>', '~'))
193 || ($insideNegation && $peek->isDelimiter(array(')')))
198 if (null !== $pseudoElement) {
199 throw SyntaxErrorException::pseudoElementFound($pseudoElement, 'not at the end of a selector');
202 if ($peek->isHash()) {
203 $result = new Node\HashNode($result, $stream->getNext()->getValue());
204 } elseif ($peek->isDelimiter(array('.'))) {
206 $result = new Node\ClassNode($result, $stream->getNextIdentifier());
207 } elseif ($peek->isDelimiter(array('['))) {
209 $result = $this->parseAttributeNode($result, $stream);
210 } elseif ($peek->isDelimiter(array(':'))) {
213 if ($stream->getPeek()->isDelimiter(array(':'))) {
215 $pseudoElement = $stream->getNextIdentifier();
220 $identifier = $stream->getNextIdentifier();
221 if (in_array(strtolower($identifier), array('first-line', 'first-letter', 'before', 'after'))) {
222 // Special case: CSS 2.1 pseudo-elements can have a single ':'.
223 // Any new pseudo-element must have two.
224 $pseudoElement = $identifier;
229 if (!$stream->getPeek()->isDelimiter(array('('))) {
230 $result = new Node\PseudoNode($result, $identifier);
236 $stream->skipWhitespace();
238 if ('not' === strtolower($identifier)) {
239 if ($insideNegation) {
240 throw SyntaxErrorException::nestedNot();
243 list($argument, $argumentPseudoElement) = $this->parseSimpleSelector($stream, true);
244 $next = $stream->getNext();
246 if (null !== $argumentPseudoElement) {
247 throw SyntaxErrorException::pseudoElementFound($argumentPseudoElement, 'inside ::not()');
250 if (!$next->isDelimiter(array(')'))) {
251 throw SyntaxErrorException::unexpectedToken('")"', $next);
254 $result = new Node\NegationNode($result, $argument);
256 $arguments = array();
260 $stream->skipWhitespace();
261 $next = $stream->getNext();
263 if ($next->isIdentifier()
266 || $next->isDelimiter(array('+', '-'))
268 $arguments[] = $next;
269 } elseif ($next->isDelimiter(array(')'))) {
272 throw SyntaxErrorException::unexpectedToken('an argument', $next);
276 if (empty($arguments)) {
277 throw SyntaxErrorException::unexpectedToken('at least one argument', $next);
280 $result = new Node\FunctionNode($result, $identifier, $arguments);
283 throw SyntaxErrorException::unexpectedToken('selector', $peek);
287 if (count($stream->getUsed()) === $selectorStart) {
288 throw SyntaxErrorException::unexpectedToken('selector', $stream->getPeek());
291 return array($result, $pseudoElement);
295 * Parses next element node.
297 * @param TokenStream $stream
299 * @return Node\ElementNode
301 private function parseElementNode(TokenStream $stream)
303 $peek = $stream->getPeek();
305 if ($peek->isIdentifier() || $peek->isDelimiter(array('*'))) {
306 if ($peek->isIdentifier()) {
307 $namespace = $stream->getNext()->getValue();
313 if ($stream->getPeek()->isDelimiter(array('|'))) {
315 $element = $stream->getNextIdentifierOrStar();
317 $element = $namespace;
321 $element = $namespace = null;
324 return new Node\ElementNode($namespace, $element);
328 * Parses next attribute node.
330 * @param Node\NodeInterface $selector
331 * @param TokenStream $stream
333 * @return Node\AttributeNode
335 * @throws SyntaxErrorException
337 private function parseAttributeNode(Node\NodeInterface $selector, TokenStream $stream)
339 $stream->skipWhitespace();
340 $attribute = $stream->getNextIdentifierOrStar();
342 if (null === $attribute && !$stream->getPeek()->isDelimiter(array('|'))) {
343 throw SyntaxErrorException::unexpectedToken('"|"', $stream->getPeek());
346 if ($stream->getPeek()->isDelimiter(array('|'))) {
349 if ($stream->getPeek()->isDelimiter(array('='))) {
354 $namespace = $attribute;
355 $attribute = $stream->getNextIdentifier();
359 $namespace = $operator = null;
362 if (null === $operator) {
363 $stream->skipWhitespace();
364 $next = $stream->getNext();
366 if ($next->isDelimiter(array(']'))) {
367 return new Node\AttributeNode($selector, $namespace, $attribute, 'exists', null);
368 } elseif ($next->isDelimiter(array('='))) {
370 } elseif ($next->isDelimiter(array('^', '$', '*', '~', '|', '!'))
371 && $stream->getPeek()->isDelimiter(array('='))
373 $operator = $next->getValue().'=';
376 throw SyntaxErrorException::unexpectedToken('operator', $next);
380 $stream->skipWhitespace();
381 $value = $stream->getNext();
383 if ($value->isNumber()) {
384 // if the value is a number, it's casted into a string
385 $value = new Token(Token::TYPE_STRING, (string) $value->getValue(), $value->getPosition());
388 if (!($value->isIdentifier() || $value->isString())) {
389 throw SyntaxErrorException::unexpectedToken('string or identifier', $value);
392 $stream->skipWhitespace();
393 $next = $stream->getNext();
395 if (!$next->isDelimiter(array(']'))) {
396 throw SyntaxErrorException::unexpectedToken('"]"', $next);
399 return new Node\AttributeNode($selector, $namespace, $attribute, $operator, $value->getValue());