No Description

Translator.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /**
  30. * @var ParserInterface
  31. */
  32. private $mainParser;
  33. /**
  34. * @var ParserInterface[]
  35. */
  36. private $shortcutParsers = array();
  37. /**
  38. * @var Extension\ExtensionInterface
  39. */
  40. private $extensions = array();
  41. /**
  42. * @var array
  43. */
  44. private $nodeTranslators = array();
  45. /**
  46. * @var array
  47. */
  48. private $combinationTranslators = array();
  49. /**
  50. * @var array
  51. */
  52. private $functionTranslators = array();
  53. /**
  54. * @var array
  55. */
  56. private $pseudoClassTranslators = array();
  57. /**
  58. * @var array
  59. */
  60. private $attributeMatchingTranslators = array();
  61. public function __construct(ParserInterface $parser = null)
  62. {
  63. $this->mainParser = $parser ?: new Parser();
  64. $this
  65. ->registerExtension(new Extension\NodeExtension())
  66. ->registerExtension(new Extension\CombinationExtension())
  67. ->registerExtension(new Extension\FunctionExtension())
  68. ->registerExtension(new Extension\PseudoClassExtension())
  69. ->registerExtension(new Extension\AttributeMatchingExtension())
  70. ;
  71. }
  72. /**
  73. * @param string $element
  74. *
  75. * @return string
  76. */
  77. public static function getXpathLiteral($element)
  78. {
  79. if (false === strpos($element, "'")) {
  80. return "'".$element."'";
  81. }
  82. if (false === strpos($element, '"')) {
  83. return '"'.$element.'"';
  84. }
  85. $string = $element;
  86. $parts = array();
  87. while (true) {
  88. if (false !== $pos = strpos($string, "'")) {
  89. $parts[] = sprintf("'%s'", substr($string, 0, $pos));
  90. $parts[] = "\"'\"";
  91. $string = substr($string, $pos + 1);
  92. } else {
  93. $parts[] = "'$string'";
  94. break;
  95. }
  96. }
  97. return sprintf('concat(%s)', implode($parts, ', '));
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
  103. {
  104. $selectors = $this->parseSelectors($cssExpr);
  105. /** @var SelectorNode $selector */
  106. foreach ($selectors as $index => $selector) {
  107. if (null !== $selector->getPseudoElement()) {
  108. throw new ExpressionErrorException('Pseudo-elements are not supported.');
  109. }
  110. $selectors[$index] = $this->selectorToXPath($selector, $prefix);
  111. }
  112. return implode(' | ', $selectors);
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
  118. {
  119. return ($prefix ?: '').$this->nodeToXPath($selector);
  120. }
  121. /**
  122. * Registers an extension.
  123. *
  124. * @param Extension\ExtensionInterface $extension
  125. *
  126. * @return $this
  127. */
  128. public function registerExtension(Extension\ExtensionInterface $extension)
  129. {
  130. $this->extensions[$extension->getName()] = $extension;
  131. $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
  132. $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
  133. $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
  134. $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
  135. $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
  136. return $this;
  137. }
  138. /**
  139. * @param string $name
  140. *
  141. * @return Extension\ExtensionInterface
  142. *
  143. * @throws ExpressionErrorException
  144. */
  145. public function getExtension($name)
  146. {
  147. if (!isset($this->extensions[$name])) {
  148. throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
  149. }
  150. return $this->extensions[$name];
  151. }
  152. /**
  153. * Registers a shortcut parser.
  154. *
  155. * @param ParserInterface $shortcut
  156. *
  157. * @return $this
  158. */
  159. public function registerParserShortcut(ParserInterface $shortcut)
  160. {
  161. $this->shortcutParsers[] = $shortcut;
  162. return $this;
  163. }
  164. /**
  165. * @param NodeInterface $node
  166. *
  167. * @return XPathExpr
  168. *
  169. * @throws ExpressionErrorException
  170. */
  171. public function nodeToXPath(NodeInterface $node)
  172. {
  173. if (!isset($this->nodeTranslators[$node->getNodeName()])) {
  174. throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
  175. }
  176. return call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
  177. }
  178. /**
  179. * @param string $combiner
  180. * @param NodeInterface $xpath
  181. * @param NodeInterface $combinedXpath
  182. *
  183. * @return XPathExpr
  184. *
  185. * @throws ExpressionErrorException
  186. */
  187. public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
  188. {
  189. if (!isset($this->combinationTranslators[$combiner])) {
  190. throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
  191. }
  192. return call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
  193. }
  194. /**
  195. * @param XPathExpr $xpath
  196. * @param FunctionNode $function
  197. *
  198. * @return XPathExpr
  199. *
  200. * @throws ExpressionErrorException
  201. */
  202. public function addFunction(XPathExpr $xpath, FunctionNode $function)
  203. {
  204. if (!isset($this->functionTranslators[$function->getName()])) {
  205. throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
  206. }
  207. return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
  208. }
  209. /**
  210. * @param XPathExpr $xpath
  211. * @param string $pseudoClass
  212. *
  213. * @return XPathExpr
  214. *
  215. * @throws ExpressionErrorException
  216. */
  217. public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
  218. {
  219. if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
  220. throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
  221. }
  222. return call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
  223. }
  224. /**
  225. * @param XPathExpr $xpath
  226. * @param string $operator
  227. * @param string $attribute
  228. * @param string $value
  229. *
  230. * @return XPathExpr
  231. *
  232. * @throws ExpressionErrorException
  233. */
  234. public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
  235. {
  236. if (!isset($this->attributeMatchingTranslators[$operator])) {
  237. throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
  238. }
  239. return call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
  240. }
  241. /**
  242. * @param string $css
  243. *
  244. * @return SelectorNode[]
  245. */
  246. private function parseSelectors($css)
  247. {
  248. foreach ($this->shortcutParsers as $shortcut) {
  249. $tokens = $shortcut->parse($css);
  250. if (!empty($tokens)) {
  251. return $tokens;
  252. }
  253. }
  254. return $this->mainParser->parse($css);
  255. }
  256. }