PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/CssSelector/Parser/Parser.php

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