Translator.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 = [];
  34. /**
  35. * @var Extension\ExtensionInterface[]
  36. */
  37. private $extensions = [];
  38. private $nodeTranslators = [];
  39. private $combinationTranslators = [];
  40. private $functionTranslators = [];
  41. private $pseudoClassTranslators = [];
  42. private $attributeMatchingTranslators = [];
  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. public static function getXpathLiteral(string $element): string
  55. {
  56. if (false === strpos($element, "'")) {
  57. return "'".$element."'";
  58. }
  59. if (false === strpos($element, '"')) {
  60. return '"'.$element.'"';
  61. }
  62. $string = $element;
  63. $parts = [];
  64. while (true) {
  65. if (false !== $pos = strpos($string, "'")) {
  66. $parts[] = sprintf("'%s'", substr($string, 0, $pos));
  67. $parts[] = "\"'\"";
  68. $string = substr($string, $pos + 1);
  69. } else {
  70. $parts[] = "'$string'";
  71. break;
  72. }
  73. }
  74. return sprintf('concat(%s)', implode(', ', $parts));
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string
  80. {
  81. $selectors = $this->parseSelectors($cssExpr);
  82. /** @var SelectorNode $selector */
  83. foreach ($selectors as $index => $selector) {
  84. if (null !== $selector->getPseudoElement()) {
  85. throw new ExpressionErrorException('Pseudo-elements are not supported.');
  86. }
  87. $selectors[$index] = $this->selectorToXPath($selector, $prefix);
  88. }
  89. return implode(' | ', $selectors);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::'): string
  95. {
  96. return ($prefix ?: '').$this->nodeToXPath($selector);
  97. }
  98. public function registerExtension(Extension\ExtensionInterface $extension): self
  99. {
  100. $this->extensions[$extension->getName()] = $extension;
  101. $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
  102. $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
  103. $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
  104. $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
  105. $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
  106. return $this;
  107. }
  108. /**
  109. * @throws ExpressionErrorException
  110. */
  111. public function getExtension(string $name): Extension\ExtensionInterface
  112. {
  113. if (!isset($this->extensions[$name])) {
  114. throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
  115. }
  116. return $this->extensions[$name];
  117. }
  118. public function registerParserShortcut(ParserInterface $shortcut): self
  119. {
  120. $this->shortcutParsers[] = $shortcut;
  121. return $this;
  122. }
  123. /**
  124. * @throws ExpressionErrorException
  125. */
  126. public function nodeToXPath(NodeInterface $node): XPathExpr
  127. {
  128. if (!isset($this->nodeTranslators[$node->getNodeName()])) {
  129. throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
  130. }
  131. return $this->nodeTranslators[$node->getNodeName()]($node, $this);
  132. }
  133. /**
  134. * @throws ExpressionErrorException
  135. */
  136. public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr
  137. {
  138. if (!isset($this->combinationTranslators[$combiner])) {
  139. throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
  140. }
  141. return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
  142. }
  143. /**
  144. * @throws ExpressionErrorException
  145. */
  146. public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr
  147. {
  148. if (!isset($this->functionTranslators[$function->getName()])) {
  149. throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
  150. }
  151. return $this->functionTranslators[$function->getName()]($xpath, $function);
  152. }
  153. /**
  154. * @throws ExpressionErrorException
  155. */
  156. public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr
  157. {
  158. if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
  159. throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
  160. }
  161. return $this->pseudoClassTranslators[$pseudoClass]($xpath);
  162. }
  163. /**
  164. * @throws ExpressionErrorException
  165. */
  166. public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, $value): XPathExpr
  167. {
  168. if (!isset($this->attributeMatchingTranslators[$operator])) {
  169. throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
  170. }
  171. return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value);
  172. }
  173. /**
  174. * @return SelectorNode[]
  175. */
  176. private function parseSelectors(string $css)
  177. {
  178. foreach ($this->shortcutParsers as $shortcut) {
  179. $tokens = $shortcut->parse($css);
  180. if (!empty($tokens)) {
  181. return $tokens;
  182. }
  183. }
  184. return $this->mainParser->parse($css);
  185. }
  186. }