You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
7.2 KiB

3 years ago
  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. /**
  99. * @return $this
  100. */
  101. public function registerExtension(Extension\ExtensionInterface $extension): self
  102. {
  103. $this->extensions[$extension->getName()] = $extension;
  104. $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
  105. $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
  106. $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
  107. $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
  108. $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
  109. return $this;
  110. }
  111. /**
  112. * @throws ExpressionErrorException
  113. */
  114. public function getExtension(string $name): Extension\ExtensionInterface
  115. {
  116. if (!isset($this->extensions[$name])) {
  117. throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
  118. }
  119. return $this->extensions[$name];
  120. }
  121. /**
  122. * @return $this
  123. */
  124. public function registerParserShortcut(ParserInterface $shortcut): self
  125. {
  126. $this->shortcutParsers[] = $shortcut;
  127. return $this;
  128. }
  129. /**
  130. * @throws ExpressionErrorException
  131. */
  132. public function nodeToXPath(NodeInterface $node): XPathExpr
  133. {
  134. if (!isset($this->nodeTranslators[$node->getNodeName()])) {
  135. throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
  136. }
  137. return $this->nodeTranslators[$node->getNodeName()]($node, $this);
  138. }
  139. /**
  140. * @throws ExpressionErrorException
  141. */
  142. public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr
  143. {
  144. if (!isset($this->combinationTranslators[$combiner])) {
  145. throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
  146. }
  147. return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
  148. }
  149. /**
  150. * @throws ExpressionErrorException
  151. */
  152. public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr
  153. {
  154. if (!isset($this->functionTranslators[$function->getName()])) {
  155. throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
  156. }
  157. return $this->functionTranslators[$function->getName()]($xpath, $function);
  158. }
  159. /**
  160. * @throws ExpressionErrorException
  161. */
  162. public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr
  163. {
  164. if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
  165. throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
  166. }
  167. return $this->pseudoClassTranslators[$pseudoClass]($xpath);
  168. }
  169. /**
  170. * @throws ExpressionErrorException
  171. */
  172. public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, $value): XPathExpr
  173. {
  174. if (!isset($this->attributeMatchingTranslators[$operator])) {
  175. throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
  176. }
  177. return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value);
  178. }
  179. /**
  180. * @return SelectorNode[]
  181. */
  182. private function parseSelectors(string $css): array
  183. {
  184. foreach ($this->shortcutParsers as $shortcut) {
  185. $tokens = $shortcut->parse($css);
  186. if (!empty($tokens)) {
  187. return $tokens;
  188. }
  189. }
  190. return $this->mainParser->parse($css);
  191. }
  192. }