Extension
2 years ago
Translator.php
2 years ago
TranslatorInterface.php
2 years ago
XPathExpr.php
2 years ago
index.php
2 years ago
Translator.php
130 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\CssSelector\XPath; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\CssSelector\Exception\ExpressionErrorException; |
| 5 | use MailPoetVendor\Symfony\Component\CssSelector\Node\FunctionNode; |
| 6 | use MailPoetVendor\Symfony\Component\CssSelector\Node\NodeInterface; |
| 7 | use MailPoetVendor\Symfony\Component\CssSelector\Node\SelectorNode; |
| 8 | use MailPoetVendor\Symfony\Component\CssSelector\Parser\Parser; |
| 9 | use MailPoetVendor\Symfony\Component\CssSelector\Parser\ParserInterface; |
| 10 | class Translator implements TranslatorInterface |
| 11 | { |
| 12 | private $mainParser; |
| 13 | private $shortcutParsers = []; |
| 14 | private $extensions = []; |
| 15 | private $nodeTranslators = []; |
| 16 | private $combinationTranslators = []; |
| 17 | private $functionTranslators = []; |
| 18 | private $pseudoClassTranslators = []; |
| 19 | private $attributeMatchingTranslators = []; |
| 20 | public function __construct(?ParserInterface $parser = null) |
| 21 | { |
| 22 | $this->mainParser = $parser ?? new Parser(); |
| 23 | $this->registerExtension(new Extension\NodeExtension())->registerExtension(new Extension\CombinationExtension())->registerExtension(new Extension\FunctionExtension())->registerExtension(new Extension\PseudoClassExtension())->registerExtension(new Extension\AttributeMatchingExtension()); |
| 24 | } |
| 25 | public static function getXpathLiteral(string $element) : string |
| 26 | { |
| 27 | if (!\str_contains($element, "'")) { |
| 28 | return "'" . $element . "'"; |
| 29 | } |
| 30 | if (!\str_contains($element, '"')) { |
| 31 | return '"' . $element . '"'; |
| 32 | } |
| 33 | $string = $element; |
| 34 | $parts = []; |
| 35 | while (\true) { |
| 36 | if (\false !== ($pos = \strpos($string, "'"))) { |
| 37 | $parts[] = \sprintf("'%s'", \substr($string, 0, $pos)); |
| 38 | $parts[] = "\"'\""; |
| 39 | $string = \substr($string, $pos + 1); |
| 40 | } else { |
| 41 | $parts[] = "'{$string}'"; |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | return \sprintf('concat(%s)', \implode(', ', $parts)); |
| 46 | } |
| 47 | public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::') : string |
| 48 | { |
| 49 | $selectors = $this->parseSelectors($cssExpr); |
| 50 | foreach ($selectors as $index => $selector) { |
| 51 | if (null !== $selector->getPseudoElement()) { |
| 52 | throw new ExpressionErrorException('Pseudo-elements are not supported.'); |
| 53 | } |
| 54 | $selectors[$index] = $this->selectorToXPath($selector, $prefix); |
| 55 | } |
| 56 | return \implode(' | ', $selectors); |
| 57 | } |
| 58 | public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::') : string |
| 59 | { |
| 60 | return ($prefix ?: '') . $this->nodeToXPath($selector); |
| 61 | } |
| 62 | public function registerExtension(Extension\ExtensionInterface $extension) : self |
| 63 | { |
| 64 | $this->extensions[$extension->getName()] = $extension; |
| 65 | $this->nodeTranslators = \array_merge($this->nodeTranslators, $extension->getNodeTranslators()); |
| 66 | $this->combinationTranslators = \array_merge($this->combinationTranslators, $extension->getCombinationTranslators()); |
| 67 | $this->functionTranslators = \array_merge($this->functionTranslators, $extension->getFunctionTranslators()); |
| 68 | $this->pseudoClassTranslators = \array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators()); |
| 69 | $this->attributeMatchingTranslators = \array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators()); |
| 70 | return $this; |
| 71 | } |
| 72 | public function getExtension(string $name) : Extension\ExtensionInterface |
| 73 | { |
| 74 | if (!isset($this->extensions[$name])) { |
| 75 | throw new ExpressionErrorException(\sprintf('Extension "%s" not registered.', $name)); |
| 76 | } |
| 77 | return $this->extensions[$name]; |
| 78 | } |
| 79 | public function registerParserShortcut(ParserInterface $shortcut) : self |
| 80 | { |
| 81 | $this->shortcutParsers[] = $shortcut; |
| 82 | return $this; |
| 83 | } |
| 84 | public function nodeToXPath(NodeInterface $node) : XPathExpr |
| 85 | { |
| 86 | if (!isset($this->nodeTranslators[$node->getNodeName()])) { |
| 87 | throw new ExpressionErrorException(\sprintf('Node "%s" not supported.', $node->getNodeName())); |
| 88 | } |
| 89 | return $this->nodeTranslators[$node->getNodeName()]($node, $this); |
| 90 | } |
| 91 | public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath) : XPathExpr |
| 92 | { |
| 93 | if (!isset($this->combinationTranslators[$combiner])) { |
| 94 | throw new ExpressionErrorException(\sprintf('Combiner "%s" not supported.', $combiner)); |
| 95 | } |
| 96 | return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath)); |
| 97 | } |
| 98 | public function addFunction(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 99 | { |
| 100 | if (!isset($this->functionTranslators[$function->getName()])) { |
| 101 | throw new ExpressionErrorException(\sprintf('Function "%s" not supported.', $function->getName())); |
| 102 | } |
| 103 | return $this->functionTranslators[$function->getName()]($xpath, $function); |
| 104 | } |
| 105 | public function addPseudoClass(XPathExpr $xpath, string $pseudoClass) : XPathExpr |
| 106 | { |
| 107 | if (!isset($this->pseudoClassTranslators[$pseudoClass])) { |
| 108 | throw new ExpressionErrorException(\sprintf('Pseudo-class "%s" not supported.', $pseudoClass)); |
| 109 | } |
| 110 | return $this->pseudoClassTranslators[$pseudoClass]($xpath); |
| 111 | } |
| 112 | public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, ?string $value) : XPathExpr |
| 113 | { |
| 114 | if (!isset($this->attributeMatchingTranslators[$operator])) { |
| 115 | throw new ExpressionErrorException(\sprintf('Attribute matcher operator "%s" not supported.', $operator)); |
| 116 | } |
| 117 | return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value); |
| 118 | } |
| 119 | private function parseSelectors(string $css) : array |
| 120 | { |
| 121 | foreach ($this->shortcutParsers as $shortcut) { |
| 122 | $tokens = $shortcut->parse($css); |
| 123 | if (!empty($tokens)) { |
| 124 | return $tokens; |
| 125 | } |
| 126 | } |
| 127 | return $this->mainParser->parse($css); |
| 128 | } |
| 129 | } |
| 130 |