菜谱项目

Translator.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\XPath;
  11. use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
  12. use Symfony\Component\CssSelector\Node\FunctionNode;
  13. use Symfony\Component\CssSelector\Node\NodeInterface;
  14. use Symfony\Component\CssSelector\Node\SelectorNode;
  15. use Symfony\Component\CssSelector\Parser\Parser;
  16. use Symfony\Component\CssSelector\Parser\ParserInterface;
  17. /**
  18. * XPath expression translator interface.
  19. *
  20. * This component is a port of the Python cssselect library,
  21. * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
  22. *
  23. * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
  24. *
  25. * @internal
  26. */
  27. class Translator implements TranslatorInterface
  28. {
  29. private $mainParser;
  30. /**
  31. * @var ParserInterface[]
  32. */
  33. private $shortcutParsers = array();
  34. /**
  35. * @var Extension\ExtensionInterface[]
  36. */
  37. private $extensions = array();
  38. private $nodeTranslators = array();
  39. private $combinationTranslators = array();
  40. private $functionTranslators = array();
  41. private $pseudoClassTranslators = array();
  42. private $attributeMatchingTranslators = array();
  43. public function __construct(ParserInterface $parser = null)
  44. {
  45. $this->mainParser = $parser ?: new Parser();
  46. $this
  47. ->registerExtension(new Extension\NodeExtension())
  48. ->registerExtension(new Extension\CombinationExtension())
  49. ->registerExtension(new Extension\FunctionExtension())
  50. ->registerExtension(new Extension\PseudoClassExtension())
  51. ->registerExtension(new Extension\AttributeMatchingExtension())
  52. ;
  53. }
  54. /**
  55. * @param string $element
  56. *
  57. * @return string
  58. */
  59. public static function getXpathLiteral($element)
  60. {
  61. if (false === strpos($element, "'")) {
  62. return "'".$element."'";
  63. }
  64. if (false === strpos($element, '"')) {
  65. return '"'.$element.'"';
  66. }
  67. $string = $element;
  68. $parts = array();
  69. while (true) {
  70. if (false !== $pos = strpos($string, "'")) {
  71. $parts[] = sprintf("'%s'", substr($string, 0, $pos));
  72. $parts[] = "\"'\"";
  73. $string = substr($string, $pos + 1);
  74. } else {
  75. $parts[] = "'$string'";
  76. break;
  77. }
  78. }
  79. return sprintf('concat(%s)', implode($parts, ', '));
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
  85. {
  86. $selectors = $this->parseSelectors($cssExpr);
  87. /** @var SelectorNode $selector */
  88. foreach ($selectors as $index => $selector) {
  89. if (null !== $selector->getPseudoElement()) {
  90. throw new ExpressionErrorException('Pseudo-elements are not supported.');
  91. }
  92. $selectors[$index] = $this->selectorToXPath($selector, $prefix);
  93. }
  94. return implode(' | ', $selectors);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
  100. {
  101. return ($prefix ?: '').$this->nodeToXPath($selector);
  102. }
  103. /**
  104. * Registers an extension.
  105. *
  106. * @return $this
  107. */
  108. public function registerExtension(Extension\ExtensionInterface $extension)
  109. {
  110. $this->extensions[$extension->getName()] = $extension;
  111. $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
  112. $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
  113. $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
  114. $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
  115. $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
  116. return $this;
  117. }
  118. /**
  119. * @param string $name
  120. *
  121. * @return Extension\ExtensionInterface
  122. *
  123. * @throws ExpressionErrorException
  124. */
  125. public function getExtension($name)
  126. {
  127. if (!isset($this->extensions[$name])) {
  128. throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
  129. }
  130. return $this->extensions[$name];
  131. }
  132. /**
  133. * Registers a shortcut parser.
  134. *
  135. * @return $this
  136. */
  137. public function registerParserShortcut(ParserInterface $shortcut)
  138. {
  139. $this->shortcutParsers[] = $shortcut;
  140. return $this;
  141. }
  142. /**
  143. * @return XPathExpr
  144. *
  145. * @throws ExpressionErrorException
  146. */
  147. public function nodeToXPath(NodeInterface $node)
  148. {
  149. if (!isset($this->nodeTranslators[$node->getNodeName()])) {
  150. throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
  151. }
  152. return call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
  153. }
  154. /**
  155. * @param string $combiner
  156. * @param NodeInterface $xpath
  157. * @param NodeInterface $combinedXpath
  158. *
  159. * @return XPathExpr
  160. *
  161. * @throws ExpressionErrorException
  162. */
  163. public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
  164. {
  165. if (!isset($this->combinationTranslators[$combiner])) {
  166. throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
  167. }
  168. return call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
  169. }
  170. /**
  171. * @return XPathExpr
  172. *
  173. * @throws ExpressionErrorException
  174. */
  175. public function addFunction(XPathExpr $xpath, FunctionNode $function)
  176. {
  177. if (!isset($this->functionTranslators[$function->getName()])) {
  178. throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
  179. }
  180. return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
  181. }
  182. /**
  183. * @param XPathExpr $xpath
  184. * @param string $pseudoClass
  185. *
  186. * @return XPathExpr
  187. *
  188. * @throws ExpressionErrorException
  189. */
  190. public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
  191. {
  192. if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
  193. throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
  194. }
  195. return call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
  196. }
  197. /**
  198. * @param XPathExpr $xpath
  199. * @param string $operator
  200. * @param string $attribute
  201. * @param string $value
  202. *
  203. * @return XPathExpr
  204. *
  205. * @throws ExpressionErrorException
  206. */
  207. public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
  208. {
  209. if (!isset($this->attributeMatchingTranslators[$operator])) {
  210. throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
  211. }
  212. return call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
  213. }
  214. /**
  215. * @param string $css
  216. *
  217. * @return SelectorNode[]
  218. */
  219. private function parseSelectors($css)
  220. {
  221. foreach ($this->shortcutParsers as $shortcut) {
  222. $tokens = $shortcut->parse($css);
  223. if (!empty($tokens)) {
  224. return $tokens;
  225. }
  226. }
  227. return $this->mainParser->parse($css);
  228. }
  229. }