← All changes
|
vendor/symfony/css-selector/XPath/Extension/NodeExtension.php
+61
-6
4.3.7
→
4.4.9
View file →
| @@ -18,9 +18,9 @@ | ||
| 18 | 18 | /** |
| 19 | 19 | * XPath expression translator node extension. |
| 20 | 20 | * |
| 21 | 21 | * This component is a port of the Python cssselect library, |
| 22 | - * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. | |
| 22 | + * which is copyright Ian Bicking, @see https://github.com/scrapy/cssselect. | |
| 23 | 23 | * |
| 24 | 24 | * @author Jean-François Simon <[email protected]> |
| 25 | 25 | * |
| 26 | 26 | * @internal |
| @@ -30,13 +30,11 @@ | ||
| 30 | 30 | public const ELEMENT_NAME_IN_LOWER_CASE = 1; |
| 31 | 31 | public const ATTRIBUTE_NAME_IN_LOWER_CASE = 2; |
| 32 | 32 | public const ATTRIBUTE_VALUE_IN_LOWER_CASE = 4; |
| 33 | 33 | |
| 34 | - private int $flags; | |
| 35 | - | |
| 36 | - public function __construct(int $flags = 0) | |
| 37 | - { | |
| 38 | - $this->flags = $flags; | |
| 34 | + public function __construct( | |
| 35 | + private int $flags = 0, | |
| 36 | + ) { | |
| 39 | 37 | } |
| 40 | 38 | |
| 41 | 39 | /** |
| 42 | 40 | * @return $this |
| @@ -64,8 +62,10 @@ | ||
| 64 | 62 | return [ |
| 65 | 63 | 'Selector' => $this->translateSelector(...), |
| 66 | 64 | 'CombinedSelector' => $this->translateCombinedSelector(...), |
| 67 | 65 | 'Negation' => $this->translateNegation(...), |
| 66 | + 'Matching' => $this->translateMatching(...), | |
| 67 | + 'SpecificityAdjustment' => $this->translateSpecificityAdjustment(...), | |
| 68 | 68 | 'Function' => $this->translateFunction(...), |
| 69 | 69 | 'Pseudo' => $this->translatePseudo(...), |
| 70 | 70 | 'Attribute' => $this->translateAttribute(...), |
| 71 | 71 | 'Class' => $this->translateClass(...), |
| @@ -70,8 +70,9 @@ | ||
| 70 | 70 | 'Attribute' => $this->translateAttribute(...), |
| 71 | 71 | 'Class' => $this->translateClass(...), |
| 72 | 72 | 'Hash' => $this->translateHash(...), |
| 73 | 73 | 'Element' => $this->translateElement(...), |
| 74 | + 'Relation' => $this->translateRelation(...), | |
| 74 | 75 | ]; |
| 75 | 76 | } |
| 76 | 77 | |
| 77 | 78 | public function translateSelector(Node\SelectorNode $node, Translator $translator): XPathExpr |
| @@ -96,8 +97,41 @@ | ||
| 96 | 97 | |
| 97 | 98 | return $xpath->addCondition('0'); |
| 98 | 99 | } |
| 99 | 100 | |
| 101 | + public function translateMatching(Node\MatchingNode $node, Translator $translator): XPathExpr | |
| 102 | + { | |
| 103 | + return $this->translateMatchingOrSpecificityAdjustment($node->selector, $node->arguments, $translator); | |
| 104 | + } | |
| 105 | + | |
| 106 | + public function translateSpecificityAdjustment(Node\SpecificityAdjustmentNode $node, Translator $translator): XPathExpr | |
| 107 | + { | |
| 108 | + return $this->translateMatchingOrSpecificityAdjustment($node->selector, $node->arguments, $translator); | |
| 109 | + } | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * @param array<Node\NodeInterface> $arguments | |
| 113 | + */ | |
| 114 | + private function translateMatchingOrSpecificityAdjustment(Node\NodeInterface $selector, array $arguments, Translator $translator): XPathExpr | |
| 115 | + { | |
| 116 | + $xpath = $translator->nodeToXPath($selector); | |
| 117 | + | |
| 118 | + $conditions = []; | |
| 119 | + foreach ($arguments as $argument) { | |
| 120 | + $expr = $translator->nodeToXPath($argument); | |
| 121 | + $expr->addNameTest(); | |
| 122 | + if ('' !== $condition = $expr->getCondition()) { | |
| 123 | + $conditions[] = $condition; | |
| 124 | + } | |
| 125 | + } | |
| 126 | + | |
| 127 | + if ($conditions) { | |
| 128 | + $xpath->addCondition(1 === \count($conditions) ? $conditions[0] : '('.implode(') or (', $conditions).')'); | |
| 129 | + } | |
| 130 | + | |
| 131 | + return $xpath; | |
| 132 | + } | |
| 133 | + | |
| 100 | 134 | public function translateFunction(Node\FunctionNode $node, Translator $translator): XPathExpr |
| 101 | 135 | { |
| 102 | 136 | $xpath = $translator->nodeToXPath($node->getSelector()); |
| 103 | 137 | |
| @@ -174,8 +208,29 @@ | ||
| 174 | 208 | |
| 175 | 209 | if (!$safe) { |
| 176 | 210 | $xpath->addNameTest(); |
| 177 | 211 | } |
| 212 | + | |
| 213 | + return $xpath; | |
| 214 | + } | |
| 215 | + | |
| 216 | + public function translateRelation(Node\RelationNode $node, Translator $translator): XPathExpr | |
| 217 | + { | |
| 218 | + $arguments = $node->getArguments(); | |
| 219 | + if (1 === \count($arguments)) { | |
| 220 | + [$combinator, $subSelector] = $arguments[0]; | |
| 221 | + | |
| 222 | + return $translator->addRelativeCombination($combinator, $node->getSelector(), $subSelector); | |
| 223 | + } | |
| 224 | + | |
| 225 | + $conditions = []; | |
| 226 | + foreach ($arguments as [$combinator, $subSelector]) { | |
| 227 | + $relativeXpath = (string) $translator->addRelativeCombination($combinator, new Node\ElementNode(), $subSelector); | |
| 228 | + $conditions[] = substr($relativeXpath, 2, -1); | |
| 229 | + } | |
| 230 | + | |
| 231 | + $xpath = $translator->nodeToXPath($node->getSelector()); | |
| 232 | + $xpath->addCondition('('.implode(') or (', $conditions).')'); | |
| 178 | 233 | |
| 179 | 234 | return $xpath; |
| 180 | 235 | } |
| 181 | 236 | |