AbstractExtension.php
2 years ago
AttributeMatchingExtension.php
2 years ago
CombinationExtension.php
2 years ago
ExtensionInterface.php
2 years ago
FunctionExtension.php
2 years ago
HtmlExtension.php
2 years ago
NodeExtension.php
2 years ago
PseudoClassExtension.php
2 years ago
index.php
2 years ago
NodeExtension.php
123 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\CssSelector\XPath\Extension; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\CssSelector\Node; |
| 5 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\Translator; |
| 6 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\XPathExpr; |
| 7 | class NodeExtension extends AbstractExtension |
| 8 | { |
| 9 | public const ELEMENT_NAME_IN_LOWER_CASE = 1; |
| 10 | public const ATTRIBUTE_NAME_IN_LOWER_CASE = 2; |
| 11 | public const ATTRIBUTE_VALUE_IN_LOWER_CASE = 4; |
| 12 | private $flags; |
| 13 | public function __construct(int $flags = 0) |
| 14 | { |
| 15 | $this->flags = $flags; |
| 16 | } |
| 17 | public function setFlag(int $flag, bool $on) : self |
| 18 | { |
| 19 | if ($on && !$this->hasFlag($flag)) { |
| 20 | $this->flags += $flag; |
| 21 | } |
| 22 | if (!$on && $this->hasFlag($flag)) { |
| 23 | $this->flags -= $flag; |
| 24 | } |
| 25 | return $this; |
| 26 | } |
| 27 | public function hasFlag(int $flag) : bool |
| 28 | { |
| 29 | return (bool) ($this->flags & $flag); |
| 30 | } |
| 31 | public function getNodeTranslators() : array |
| 32 | { |
| 33 | return ['Selector' => [$this, 'translateSelector'], 'CombinedSelector' => [$this, 'translateCombinedSelector'], 'Negation' => [$this, 'translateNegation'], 'Function' => [$this, 'translateFunction'], 'Pseudo' => [$this, 'translatePseudo'], 'Attribute' => [$this, 'translateAttribute'], 'Class' => [$this, 'translateClass'], 'Hash' => [$this, 'translateHash'], 'Element' => [$this, 'translateElement']]; |
| 34 | } |
| 35 | public function translateSelector(Node\SelectorNode $node, Translator $translator) : XPathExpr |
| 36 | { |
| 37 | return $translator->nodeToXPath($node->getTree()); |
| 38 | } |
| 39 | public function translateCombinedSelector(Node\CombinedSelectorNode $node, Translator $translator) : XPathExpr |
| 40 | { |
| 41 | return $translator->addCombination($node->getCombinator(), $node->getSelector(), $node->getSubSelector()); |
| 42 | } |
| 43 | public function translateNegation(Node\NegationNode $node, Translator $translator) : XPathExpr |
| 44 | { |
| 45 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 46 | $subXpath = $translator->nodeToXPath($node->getSubSelector()); |
| 47 | $subXpath->addNameTest(); |
| 48 | if ($subXpath->getCondition()) { |
| 49 | return $xpath->addCondition(\sprintf('not(%s)', $subXpath->getCondition())); |
| 50 | } |
| 51 | return $xpath->addCondition('0'); |
| 52 | } |
| 53 | public function translateFunction(Node\FunctionNode $node, Translator $translator) : XPathExpr |
| 54 | { |
| 55 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 56 | return $translator->addFunction($xpath, $node); |
| 57 | } |
| 58 | public function translatePseudo(Node\PseudoNode $node, Translator $translator) : XPathExpr |
| 59 | { |
| 60 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 61 | return $translator->addPseudoClass($xpath, $node->getIdentifier()); |
| 62 | } |
| 63 | public function translateAttribute(Node\AttributeNode $node, Translator $translator) : XPathExpr |
| 64 | { |
| 65 | $name = $node->getAttribute(); |
| 66 | $safe = $this->isSafeName($name); |
| 67 | if ($this->hasFlag(self::ATTRIBUTE_NAME_IN_LOWER_CASE)) { |
| 68 | $name = \strtolower($name); |
| 69 | } |
| 70 | if ($node->getNamespace()) { |
| 71 | $name = \sprintf('%s:%s', $node->getNamespace(), $name); |
| 72 | $safe = $safe && $this->isSafeName($node->getNamespace()); |
| 73 | } |
| 74 | $attribute = $safe ? '@' . $name : \sprintf('attribute::*[name() = %s]', Translator::getXpathLiteral($name)); |
| 75 | $value = $node->getValue(); |
| 76 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 77 | if ($this->hasFlag(self::ATTRIBUTE_VALUE_IN_LOWER_CASE)) { |
| 78 | $value = \strtolower($value); |
| 79 | } |
| 80 | return $translator->addAttributeMatching($xpath, $node->getOperator(), $attribute, $value); |
| 81 | } |
| 82 | public function translateClass(Node\ClassNode $node, Translator $translator) : XPathExpr |
| 83 | { |
| 84 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 85 | return $translator->addAttributeMatching($xpath, '~=', '@class', $node->getName()); |
| 86 | } |
| 87 | public function translateHash(Node\HashNode $node, Translator $translator) : XPathExpr |
| 88 | { |
| 89 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 90 | return $translator->addAttributeMatching($xpath, '=', '@id', $node->getId()); |
| 91 | } |
| 92 | public function translateElement(Node\ElementNode $node) : XPathExpr |
| 93 | { |
| 94 | $element = $node->getElement(); |
| 95 | if ($element && $this->hasFlag(self::ELEMENT_NAME_IN_LOWER_CASE)) { |
| 96 | $element = \strtolower($element); |
| 97 | } |
| 98 | if ($element) { |
| 99 | $safe = $this->isSafeName($element); |
| 100 | } else { |
| 101 | $element = '*'; |
| 102 | $safe = \true; |
| 103 | } |
| 104 | if ($node->getNamespace()) { |
| 105 | $element = \sprintf('%s:%s', $node->getNamespace(), $element); |
| 106 | $safe = $safe && $this->isSafeName($node->getNamespace()); |
| 107 | } |
| 108 | $xpath = new XPathExpr('', $element); |
| 109 | if (!$safe) { |
| 110 | $xpath->addNameTest(); |
| 111 | } |
| 112 | return $xpath; |
| 113 | } |
| 114 | public function getName() : string |
| 115 | { |
| 116 | return 'node'; |
| 117 | } |
| 118 | private function isSafeName(string $name) : bool |
| 119 | { |
| 120 | return 0 < \preg_match('~^[a-zA-Z_][a-zA-Z0-9_.-]*$~', $name); |
| 121 | } |
| 122 | } |
| 123 |