菜谱项目

Parser.php 12KB

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