@@ -23,89 +23,89 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class XPathExpr |
| 25 | 25 | { |
| 26 | - private $path; |
|
| 27 | - private $element; |
|
| 28 | - private $condition; |
|
| 29 | - |
|
| 30 | - public function __construct(string $path = '', string $element = '*', string $condition = '', bool $starPrefix = false) |
|
| 31 | - { |
|
| 32 | - $this->path = $path; |
|
| 33 | - $this->element = $element; |
|
| 34 | - $this->condition = $condition; |
|
| 35 | - |
|
| 36 | - if ($starPrefix) { |
|
| 37 | - $this->addStarPrefix(); |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function getElement(): string |
|
| 42 | - { |
|
| 43 | - return $this->element; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return $this |
|
| 48 | - */ |
|
| 49 | - public function addCondition(string $condition): self |
|
| 50 | - { |
|
| 51 | - $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition; |
|
| 52 | - |
|
| 53 | - return $this; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - public function getCondition(): string |
|
| 57 | - { |
|
| 58 | - return $this->condition; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @return $this |
|
| 63 | - */ |
|
| 64 | - public function addNameTest(): self |
|
| 65 | - { |
|
| 66 | - if ('*' !== $this->element) { |
|
| 67 | - $this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); |
|
| 68 | - $this->element = '*'; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - return $this; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return $this |
|
| 76 | - */ |
|
| 77 | - public function addStarPrefix(): self |
|
| 78 | - { |
|
| 79 | - $this->path .= '*/'; |
|
| 80 | - |
|
| 81 | - return $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Joins another XPathExpr with a combiner. |
|
| 86 | - * |
|
| 87 | - * @return $this |
|
| 88 | - */ |
|
| 89 | - public function join(string $combiner, self $expr): self |
|
| 90 | - { |
|
| 91 | - $path = $this->__toString().$combiner; |
|
| 92 | - |
|
| 93 | - if ('*/' !== $expr->path) { |
|
| 94 | - $path .= $expr->path; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $this->path = $path; |
|
| 98 | - $this->element = $expr->element; |
|
| 99 | - $this->condition = $expr->condition; |
|
| 100 | - |
|
| 101 | - return $this; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public function __toString(): string |
|
| 105 | - { |
|
| 106 | - $path = $this->path.$this->element; |
|
| 107 | - $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']'; |
|
| 108 | - |
|
| 109 | - return $path.$condition; |
|
| 110 | - } |
|
| 26 | + private $path; |
|
| 27 | + private $element; |
|
| 28 | + private $condition; |
|
| 29 | + |
|
| 30 | + public function __construct(string $path = '', string $element = '*', string $condition = '', bool $starPrefix = false) |
|
| 31 | + { |
|
| 32 | + $this->path = $path; |
|
| 33 | + $this->element = $element; |
|
| 34 | + $this->condition = $condition; |
|
| 35 | + |
|
| 36 | + if ($starPrefix) { |
|
| 37 | + $this->addStarPrefix(); |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function getElement(): string |
|
| 42 | + { |
|
| 43 | + return $this->element; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return $this |
|
| 48 | + */ |
|
| 49 | + public function addCondition(string $condition): self |
|
| 50 | + { |
|
| 51 | + $this->condition = $this->condition ? sprintf('(%s) and (%s)', $this->condition, $condition) : $condition; |
|
| 52 | + |
|
| 53 | + return $this; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + public function getCondition(): string |
|
| 57 | + { |
|
| 58 | + return $this->condition; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @return $this |
|
| 63 | + */ |
|
| 64 | + public function addNameTest(): self |
|
| 65 | + { |
|
| 66 | + if ('*' !== $this->element) { |
|
| 67 | + $this->addCondition('name() = '.Translator::getXpathLiteral($this->element)); |
|
| 68 | + $this->element = '*'; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + return $this; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return $this |
|
| 76 | + */ |
|
| 77 | + public function addStarPrefix(): self |
|
| 78 | + { |
|
| 79 | + $this->path .= '*/'; |
|
| 80 | + |
|
| 81 | + return $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Joins another XPathExpr with a combiner. |
|
| 86 | + * |
|
| 87 | + * @return $this |
|
| 88 | + */ |
|
| 89 | + public function join(string $combiner, self $expr): self |
|
| 90 | + { |
|
| 91 | + $path = $this->__toString().$combiner; |
|
| 92 | + |
|
| 93 | + if ('*/' !== $expr->path) { |
|
| 94 | + $path .= $expr->path; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $this->path = $path; |
|
| 98 | + $this->element = $expr->element; |
|
| 99 | + $this->condition = $expr->condition; |
|
| 100 | + |
|
| 101 | + return $this; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function __toString(): string |
|
| 105 | + { |
|
| 106 | + $path = $this->path.$this->element; |
|
| 107 | + $condition = null === $this->condition || '' === $this->condition ? '' : '['.$this->condition.']'; |
|
| 108 | + |
|
| 109 | + return $path.$condition; |
|
| 110 | + } |
|
| 111 | 111 | } |
@@ -30,201 +30,201 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class Translator implements TranslatorInterface |
| 32 | 32 | { |
| 33 | - private $mainParser; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var ParserInterface[] |
|
| 37 | - */ |
|
| 38 | - private $shortcutParsers = []; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var Extension\ExtensionInterface[] |
|
| 42 | - */ |
|
| 43 | - private $extensions = []; |
|
| 44 | - |
|
| 45 | - private $nodeTranslators = []; |
|
| 46 | - private $combinationTranslators = []; |
|
| 47 | - private $functionTranslators = []; |
|
| 48 | - private $pseudoClassTranslators = []; |
|
| 49 | - private $attributeMatchingTranslators = []; |
|
| 50 | - |
|
| 51 | - public function __construct(ParserInterface $parser = null) |
|
| 52 | - { |
|
| 53 | - $this->mainParser = $parser ?? new Parser(); |
|
| 54 | - |
|
| 55 | - $this |
|
| 56 | - ->registerExtension(new Extension\NodeExtension()) |
|
| 57 | - ->registerExtension(new Extension\CombinationExtension()) |
|
| 58 | - ->registerExtension(new Extension\FunctionExtension()) |
|
| 59 | - ->registerExtension(new Extension\PseudoClassExtension()) |
|
| 60 | - ->registerExtension(new Extension\AttributeMatchingExtension()) |
|
| 61 | - ; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public static function getXpathLiteral(string $element): string |
|
| 65 | - { |
|
| 66 | - if (!str_contains($element, "'")) { |
|
| 67 | - return "'".$element."'"; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if (!str_contains($element, '"')) { |
|
| 71 | - return '"'.$element.'"'; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $string = $element; |
|
| 75 | - $parts = []; |
|
| 76 | - while (true) { |
|
| 77 | - if (false !== $pos = strpos($string, "'")) { |
|
| 78 | - $parts[] = sprintf("'%s'", substr($string, 0, $pos)); |
|
| 79 | - $parts[] = "\"'\""; |
|
| 80 | - $string = substr($string, $pos + 1); |
|
| 81 | - } else { |
|
| 82 | - $parts[] = "'$string'"; |
|
| 83 | - break; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - return sprintf('concat(%s)', implode(', ', $parts)); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * {@inheritdoc} |
|
| 92 | - */ |
|
| 93 | - public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string |
|
| 94 | - { |
|
| 95 | - $selectors = $this->parseSelectors($cssExpr); |
|
| 96 | - |
|
| 97 | - /** @var SelectorNode $selector */ |
|
| 98 | - foreach ($selectors as $index => $selector) { |
|
| 99 | - if (null !== $selector->getPseudoElement()) { |
|
| 100 | - throw new ExpressionErrorException('Pseudo-elements are not supported.'); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $selectors[$index] = $this->selectorToXPath($selector, $prefix); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return implode(' | ', $selectors); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * {@inheritdoc} |
|
| 111 | - */ |
|
| 112 | - public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::'): string |
|
| 113 | - { |
|
| 114 | - return ($prefix ?: '').$this->nodeToXPath($selector); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @return $this |
|
| 119 | - */ |
|
| 120 | - public function registerExtension(Extension\ExtensionInterface $extension): self |
|
| 121 | - { |
|
| 122 | - $this->extensions[$extension->getName()] = $extension; |
|
| 123 | - |
|
| 124 | - $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators()); |
|
| 125 | - $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators()); |
|
| 126 | - $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators()); |
|
| 127 | - $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators()); |
|
| 128 | - $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators()); |
|
| 129 | - |
|
| 130 | - return $this; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @throws ExpressionErrorException |
|
| 135 | - */ |
|
| 136 | - public function getExtension(string $name): Extension\ExtensionInterface |
|
| 137 | - { |
|
| 138 | - if (!isset($this->extensions[$name])) { |
|
| 139 | - throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name)); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $this->extensions[$name]; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @return $this |
|
| 147 | - */ |
|
| 148 | - public function registerParserShortcut(ParserInterface $shortcut): self |
|
| 149 | - { |
|
| 150 | - $this->shortcutParsers[] = $shortcut; |
|
| 151 | - |
|
| 152 | - return $this; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @throws ExpressionErrorException |
|
| 157 | - */ |
|
| 158 | - public function nodeToXPath(NodeInterface $node): XPathExpr |
|
| 159 | - { |
|
| 160 | - if (!isset($this->nodeTranslators[$node->getNodeName()])) { |
|
| 161 | - throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName())); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return $this->nodeTranslators[$node->getNodeName()]($node, $this); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @throws ExpressionErrorException |
|
| 169 | - */ |
|
| 170 | - public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr |
|
| 171 | - { |
|
| 172 | - if (!isset($this->combinationTranslators[$combiner])) { |
|
| 173 | - throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner)); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath)); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @throws ExpressionErrorException |
|
| 181 | - */ |
|
| 182 | - public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr |
|
| 183 | - { |
|
| 184 | - if (!isset($this->functionTranslators[$function->getName()])) { |
|
| 185 | - throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName())); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $this->functionTranslators[$function->getName()]($xpath, $function); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @throws ExpressionErrorException |
|
| 193 | - */ |
|
| 194 | - public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr |
|
| 195 | - { |
|
| 196 | - if (!isset($this->pseudoClassTranslators[$pseudoClass])) { |
|
| 197 | - throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass)); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - return $this->pseudoClassTranslators[$pseudoClass]($xpath); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @throws ExpressionErrorException |
|
| 205 | - */ |
|
| 206 | - public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, ?string $value): XPathExpr |
|
| 207 | - { |
|
| 208 | - if (!isset($this->attributeMatchingTranslators[$operator])) { |
|
| 209 | - throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator)); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @return SelectorNode[] |
|
| 217 | - */ |
|
| 218 | - private function parseSelectors(string $css): array |
|
| 219 | - { |
|
| 220 | - foreach ($this->shortcutParsers as $shortcut) { |
|
| 221 | - $tokens = $shortcut->parse($css); |
|
| 222 | - |
|
| 223 | - if (!empty($tokens)) { |
|
| 224 | - return $tokens; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - return $this->mainParser->parse($css); |
|
| 229 | - } |
|
| 33 | + private $mainParser; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var ParserInterface[] |
|
| 37 | + */ |
|
| 38 | + private $shortcutParsers = []; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var Extension\ExtensionInterface[] |
|
| 42 | + */ |
|
| 43 | + private $extensions = []; |
|
| 44 | + |
|
| 45 | + private $nodeTranslators = []; |
|
| 46 | + private $combinationTranslators = []; |
|
| 47 | + private $functionTranslators = []; |
|
| 48 | + private $pseudoClassTranslators = []; |
|
| 49 | + private $attributeMatchingTranslators = []; |
|
| 50 | + |
|
| 51 | + public function __construct(ParserInterface $parser = null) |
|
| 52 | + { |
|
| 53 | + $this->mainParser = $parser ?? new Parser(); |
|
| 54 | + |
|
| 55 | + $this |
|
| 56 | + ->registerExtension(new Extension\NodeExtension()) |
|
| 57 | + ->registerExtension(new Extension\CombinationExtension()) |
|
| 58 | + ->registerExtension(new Extension\FunctionExtension()) |
|
| 59 | + ->registerExtension(new Extension\PseudoClassExtension()) |
|
| 60 | + ->registerExtension(new Extension\AttributeMatchingExtension()) |
|
| 61 | + ; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public static function getXpathLiteral(string $element): string |
|
| 65 | + { |
|
| 66 | + if (!str_contains($element, "'")) { |
|
| 67 | + return "'".$element."'"; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if (!str_contains($element, '"')) { |
|
| 71 | + return '"'.$element.'"'; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $string = $element; |
|
| 75 | + $parts = []; |
|
| 76 | + while (true) { |
|
| 77 | + if (false !== $pos = strpos($string, "'")) { |
|
| 78 | + $parts[] = sprintf("'%s'", substr($string, 0, $pos)); |
|
| 79 | + $parts[] = "\"'\""; |
|
| 80 | + $string = substr($string, $pos + 1); |
|
| 81 | + } else { |
|
| 82 | + $parts[] = "'$string'"; |
|
| 83 | + break; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + return sprintf('concat(%s)', implode(', ', $parts)); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * {@inheritdoc} |
|
| 92 | + */ |
|
| 93 | + public function cssToXPath(string $cssExpr, string $prefix = 'descendant-or-self::'): string |
|
| 94 | + { |
|
| 95 | + $selectors = $this->parseSelectors($cssExpr); |
|
| 96 | + |
|
| 97 | + /** @var SelectorNode $selector */ |
|
| 98 | + foreach ($selectors as $index => $selector) { |
|
| 99 | + if (null !== $selector->getPseudoElement()) { |
|
| 100 | + throw new ExpressionErrorException('Pseudo-elements are not supported.'); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $selectors[$index] = $this->selectorToXPath($selector, $prefix); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return implode(' | ', $selectors); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * {@inheritdoc} |
|
| 111 | + */ |
|
| 112 | + public function selectorToXPath(SelectorNode $selector, string $prefix = 'descendant-or-self::'): string |
|
| 113 | + { |
|
| 114 | + return ($prefix ?: '').$this->nodeToXPath($selector); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @return $this |
|
| 119 | + */ |
|
| 120 | + public function registerExtension(Extension\ExtensionInterface $extension): self |
|
| 121 | + { |
|
| 122 | + $this->extensions[$extension->getName()] = $extension; |
|
| 123 | + |
|
| 124 | + $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators()); |
|
| 125 | + $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators()); |
|
| 126 | + $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators()); |
|
| 127 | + $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators()); |
|
| 128 | + $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators()); |
|
| 129 | + |
|
| 130 | + return $this; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @throws ExpressionErrorException |
|
| 135 | + */ |
|
| 136 | + public function getExtension(string $name): Extension\ExtensionInterface |
|
| 137 | + { |
|
| 138 | + if (!isset($this->extensions[$name])) { |
|
| 139 | + throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name)); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $this->extensions[$name]; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @return $this |
|
| 147 | + */ |
|
| 148 | + public function registerParserShortcut(ParserInterface $shortcut): self |
|
| 149 | + { |
|
| 150 | + $this->shortcutParsers[] = $shortcut; |
|
| 151 | + |
|
| 152 | + return $this; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @throws ExpressionErrorException |
|
| 157 | + */ |
|
| 158 | + public function nodeToXPath(NodeInterface $node): XPathExpr |
|
| 159 | + { |
|
| 160 | + if (!isset($this->nodeTranslators[$node->getNodeName()])) { |
|
| 161 | + throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName())); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return $this->nodeTranslators[$node->getNodeName()]($node, $this); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @throws ExpressionErrorException |
|
| 169 | + */ |
|
| 170 | + public function addCombination(string $combiner, NodeInterface $xpath, NodeInterface $combinedXpath): XPathExpr |
|
| 171 | + { |
|
| 172 | + if (!isset($this->combinationTranslators[$combiner])) { |
|
| 173 | + throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner)); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $this->combinationTranslators[$combiner]($this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath)); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @throws ExpressionErrorException |
|
| 181 | + */ |
|
| 182 | + public function addFunction(XPathExpr $xpath, FunctionNode $function): XPathExpr |
|
| 183 | + { |
|
| 184 | + if (!isset($this->functionTranslators[$function->getName()])) { |
|
| 185 | + throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName())); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $this->functionTranslators[$function->getName()]($xpath, $function); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @throws ExpressionErrorException |
|
| 193 | + */ |
|
| 194 | + public function addPseudoClass(XPathExpr $xpath, string $pseudoClass): XPathExpr |
|
| 195 | + { |
|
| 196 | + if (!isset($this->pseudoClassTranslators[$pseudoClass])) { |
|
| 197 | + throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass)); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + return $this->pseudoClassTranslators[$pseudoClass]($xpath); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @throws ExpressionErrorException |
|
| 205 | + */ |
|
| 206 | + public function addAttributeMatching(XPathExpr $xpath, string $operator, string $attribute, ?string $value): XPathExpr |
|
| 207 | + { |
|
| 208 | + if (!isset($this->attributeMatchingTranslators[$operator])) { |
|
| 209 | + throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator)); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return $this->attributeMatchingTranslators[$operator]($xpath, $attribute, $value); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @return SelectorNode[] |
|
| 217 | + */ |
|
| 218 | + private function parseSelectors(string $css): array |
|
| 219 | + { |
|
| 220 | + foreach ($this->shortcutParsers as $shortcut) { |
|
| 221 | + $tokens = $shortcut->parse($css); |
|
| 222 | + |
|
| 223 | + if (!empty($tokens)) { |
|
| 224 | + return $tokens; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + return $this->mainParser->parse($css); |
|
| 229 | + } |
|
| 230 | 230 | } |
@@ -1,11 +1,11 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (\PHP_VERSION_ID < 80000) { |
| 4 | - interface Stringable |
|
| 5 | - { |
|
| 6 | - /** |
|
| 7 | - * @return string |
|
| 8 | - */ |
|
| 9 | - public function __toString(); |
|
| 10 | - } |
|
| 4 | + interface Stringable |
|
| 5 | + { |
|
| 6 | + /** |
|
| 7 | + * @return string |
|
| 8 | + */ |
|
| 9 | + public function __toString(); |
|
| 10 | + } |
|
| 11 | 11 | } |
@@ -3,20 +3,20 @@ |
||
| 3 | 3 | #[Attribute(Attribute::TARGET_CLASS)] |
| 4 | 4 | final class Attribute |
| 5 | 5 | { |
| 6 | - public const TARGET_CLASS = 1; |
|
| 7 | - public const TARGET_FUNCTION = 2; |
|
| 8 | - public const TARGET_METHOD = 4; |
|
| 9 | - public const TARGET_PROPERTY = 8; |
|
| 10 | - public const TARGET_CLASS_CONSTANT = 16; |
|
| 11 | - public const TARGET_PARAMETER = 32; |
|
| 12 | - public const TARGET_ALL = 63; |
|
| 13 | - public const IS_REPEATABLE = 64; |
|
| 6 | + public const TARGET_CLASS = 1; |
|
| 7 | + public const TARGET_FUNCTION = 2; |
|
| 8 | + public const TARGET_METHOD = 4; |
|
| 9 | + public const TARGET_PROPERTY = 8; |
|
| 10 | + public const TARGET_CLASS_CONSTANT = 16; |
|
| 11 | + public const TARGET_PARAMETER = 32; |
|
| 12 | + public const TARGET_ALL = 63; |
|
| 13 | + public const IS_REPEATABLE = 64; |
|
| 14 | 14 | |
| 15 | - /** @var int */ |
|
| 16 | - public $flags; |
|
| 15 | + /** @var int */ |
|
| 16 | + public $flags; |
|
| 17 | 17 | |
| 18 | - public function __construct(int $flags = self::TARGET_ALL) |
|
| 19 | - { |
|
| 20 | - $this->flags = $flags; |
|
| 21 | - } |
|
| 18 | + public function __construct(int $flags = self::TARGET_ALL) |
|
| 19 | + { |
|
| 20 | + $this->flags = $flags; |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (\PHP_VERSION_ID < 80000) { |
| 4 | - class ValueError extends Error |
|
| 5 | - { |
|
| 6 | - } |
|
| 4 | + class ValueError extends Error |
|
| 5 | + { |
|
| 6 | + } |
|
| 7 | 7 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (\PHP_VERSION_ID < 80000) { |
| 4 | - class UnhandledMatchError extends Error |
|
| 5 | - { |
|
| 6 | - } |
|
| 4 | + class UnhandledMatchError extends Error |
|
| 5 | + { |
|
| 6 | + } |
|
| 7 | 7 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (\PHP_VERSION_ID < 80000 && \extension_loaded('tokenizer')) { |
| 4 | - class PhpToken extends Symfony\Polyfill\Php80\PhpToken |
|
| 5 | - { |
|
| 6 | - } |
|
| 4 | + class PhpToken extends Symfony\Polyfill\Php80\PhpToken |
|
| 5 | + { |
|
| 6 | + } |
|
| 7 | 7 | } |
@@ -20,96 +20,96 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | final class Php80 |
| 22 | 22 | { |
| 23 | - public static function fdiv(float $dividend, float $divisor): float |
|
| 24 | - { |
|
| 25 | - return @($dividend / $divisor); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - public static function get_debug_type($value): string |
|
| 29 | - { |
|
| 30 | - switch (true) { |
|
| 31 | - case null === $value: return 'null'; |
|
| 32 | - case \is_bool($value): return 'bool'; |
|
| 33 | - case \is_string($value): return 'string'; |
|
| 34 | - case \is_array($value): return 'array'; |
|
| 35 | - case \is_int($value): return 'int'; |
|
| 36 | - case \is_float($value): return 'float'; |
|
| 37 | - case \is_object($value): break; |
|
| 38 | - case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class'; |
|
| 39 | - default: |
|
| 40 | - if (null === $type = @get_resource_type($value)) { |
|
| 41 | - return 'unknown'; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - if ('Unknown' === $type) { |
|
| 45 | - $type = 'closed'; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - return "resource ($type)"; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $class = \get_class($value); |
|
| 52 | - |
|
| 53 | - if (false === strpos($class, '@')) { |
|
| 54 | - return $class; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous'; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public static function get_resource_id($res): int |
|
| 61 | - { |
|
| 62 | - if (!\is_resource($res) && null === @get_resource_type($res)) { |
|
| 63 | - throw new \TypeError(sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res))); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return (int) $res; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - public static function preg_last_error_msg(): string |
|
| 70 | - { |
|
| 71 | - switch (preg_last_error()) { |
|
| 72 | - case \PREG_INTERNAL_ERROR: |
|
| 73 | - return 'Internal error'; |
|
| 74 | - case \PREG_BAD_UTF8_ERROR: |
|
| 75 | - return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
| 76 | - case \PREG_BAD_UTF8_OFFSET_ERROR: |
|
| 77 | - return 'The offset did not correspond to the beginning of a valid UTF-8 code point'; |
|
| 78 | - case \PREG_BACKTRACK_LIMIT_ERROR: |
|
| 79 | - return 'Backtrack limit exhausted'; |
|
| 80 | - case \PREG_RECURSION_LIMIT_ERROR: |
|
| 81 | - return 'Recursion limit exhausted'; |
|
| 82 | - case \PREG_JIT_STACKLIMIT_ERROR: |
|
| 83 | - return 'JIT stack limit exhausted'; |
|
| 84 | - case \PREG_NO_ERROR: |
|
| 85 | - return 'No error'; |
|
| 86 | - default: |
|
| 87 | - return 'Unknown error'; |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - public static function str_contains(string $haystack, string $needle): bool |
|
| 92 | - { |
|
| 93 | - return '' === $needle || false !== strpos($haystack, $needle); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - public static function str_starts_with(string $haystack, string $needle): bool |
|
| 97 | - { |
|
| 98 | - return 0 === strncmp($haystack, $needle, \strlen($needle)); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - public static function str_ends_with(string $haystack, string $needle): bool |
|
| 102 | - { |
|
| 103 | - if ('' === $needle || $needle === $haystack) { |
|
| 104 | - return true; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ('' === $haystack) { |
|
| 108 | - return false; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $needleLength = \strlen($needle); |
|
| 112 | - |
|
| 113 | - return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength); |
|
| 114 | - } |
|
| 23 | + public static function fdiv(float $dividend, float $divisor): float |
|
| 24 | + { |
|
| 25 | + return @($dividend / $divisor); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public static function get_debug_type($value): string |
|
| 29 | + { |
|
| 30 | + switch (true) { |
|
| 31 | + case null === $value: return 'null'; |
|
| 32 | + case \is_bool($value): return 'bool'; |
|
| 33 | + case \is_string($value): return 'string'; |
|
| 34 | + case \is_array($value): return 'array'; |
|
| 35 | + case \is_int($value): return 'int'; |
|
| 36 | + case \is_float($value): return 'float'; |
|
| 37 | + case \is_object($value): break; |
|
| 38 | + case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class'; |
|
| 39 | + default: |
|
| 40 | + if (null === $type = @get_resource_type($value)) { |
|
| 41 | + return 'unknown'; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + if ('Unknown' === $type) { |
|
| 45 | + $type = 'closed'; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + return "resource ($type)"; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $class = \get_class($value); |
|
| 52 | + |
|
| 53 | + if (false === strpos($class, '@')) { |
|
| 54 | + return $class; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous'; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public static function get_resource_id($res): int |
|
| 61 | + { |
|
| 62 | + if (!\is_resource($res) && null === @get_resource_type($res)) { |
|
| 63 | + throw new \TypeError(sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res))); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return (int) $res; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + public static function preg_last_error_msg(): string |
|
| 70 | + { |
|
| 71 | + switch (preg_last_error()) { |
|
| 72 | + case \PREG_INTERNAL_ERROR: |
|
| 73 | + return 'Internal error'; |
|
| 74 | + case \PREG_BAD_UTF8_ERROR: |
|
| 75 | + return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
|
| 76 | + case \PREG_BAD_UTF8_OFFSET_ERROR: |
|
| 77 | + return 'The offset did not correspond to the beginning of a valid UTF-8 code point'; |
|
| 78 | + case \PREG_BACKTRACK_LIMIT_ERROR: |
|
| 79 | + return 'Backtrack limit exhausted'; |
|
| 80 | + case \PREG_RECURSION_LIMIT_ERROR: |
|
| 81 | + return 'Recursion limit exhausted'; |
|
| 82 | + case \PREG_JIT_STACKLIMIT_ERROR: |
|
| 83 | + return 'JIT stack limit exhausted'; |
|
| 84 | + case \PREG_NO_ERROR: |
|
| 85 | + return 'No error'; |
|
| 86 | + default: |
|
| 87 | + return 'Unknown error'; |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + public static function str_contains(string $haystack, string $needle): bool |
|
| 92 | + { |
|
| 93 | + return '' === $needle || false !== strpos($haystack, $needle); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + public static function str_starts_with(string $haystack, string $needle): bool |
|
| 97 | + { |
|
| 98 | + return 0 === strncmp($haystack, $needle, \strlen($needle)); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + public static function str_ends_with(string $haystack, string $needle): bool |
|
| 102 | + { |
|
| 103 | + if ('' === $needle || $needle === $haystack) { |
|
| 104 | + return true; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ('' === $haystack) { |
|
| 108 | + return false; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $needleLength = \strlen($needle); |
|
| 112 | + |
|
| 113 | + return $needleLength <= \strlen($haystack) && 0 === substr_compare($haystack, $needle, -$needleLength); |
|
| 114 | + } |
|
| 115 | 115 | } |
@@ -59,7 +59,7 @@ |
||
| 59 | 59 | |
| 60 | 60 | public static function get_resource_id($res): int |
| 61 | 61 | { |
| 62 | - if (!\is_resource($res) && null === @get_resource_type($res)) { |
|
| 62 | + if ( ! \is_resource($res) && null === @get_resource_type($res)) { |
|
| 63 | 63 | throw new \TypeError(sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res))); |
| 64 | 64 | } |
| 65 | 65 | |
@@ -12,31 +12,31 @@ |
||
| 12 | 12 | use Symfony\Polyfill\Php80 as p; |
| 13 | 13 | |
| 14 | 14 | if (\PHP_VERSION_ID >= 80000) { |
| 15 | - return; |
|
| 15 | + return; |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) { |
| 19 | - define('FILTER_VALIDATE_BOOL', \FILTER_VALIDATE_BOOLEAN); |
|
| 19 | + define('FILTER_VALIDATE_BOOL', \FILTER_VALIDATE_BOOLEAN); |
|
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | if (!function_exists('fdiv')) { |
| 23 | - function fdiv(float $num1, float $num2): float { return p\Php80::fdiv($num1, $num2); } |
|
| 23 | + function fdiv(float $num1, float $num2): float { return p\Php80::fdiv($num1, $num2); } |
|
| 24 | 24 | } |
| 25 | 25 | if (!function_exists('preg_last_error_msg')) { |
| 26 | - function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); } |
|
| 26 | + function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); } |
|
| 27 | 27 | } |
| 28 | 28 | if (!function_exists('str_contains')) { |
| 29 | - function str_contains(?string $haystack, ?string $needle): bool { return p\Php80::str_contains($haystack ?? '', $needle ?? ''); } |
|
| 29 | + function str_contains(?string $haystack, ?string $needle): bool { return p\Php80::str_contains($haystack ?? '', $needle ?? ''); } |
|
| 30 | 30 | } |
| 31 | 31 | if (!function_exists('str_starts_with')) { |
| 32 | - function str_starts_with(?string $haystack, ?string $needle): bool { return p\Php80::str_starts_with($haystack ?? '', $needle ?? ''); } |
|
| 32 | + function str_starts_with(?string $haystack, ?string $needle): bool { return p\Php80::str_starts_with($haystack ?? '', $needle ?? ''); } |
|
| 33 | 33 | } |
| 34 | 34 | if (!function_exists('str_ends_with')) { |
| 35 | - function str_ends_with(?string $haystack, ?string $needle): bool { return p\Php80::str_ends_with($haystack ?? '', $needle ?? ''); } |
|
| 35 | + function str_ends_with(?string $haystack, ?string $needle): bool { return p\Php80::str_ends_with($haystack ?? '', $needle ?? ''); } |
|
| 36 | 36 | } |
| 37 | 37 | if (!function_exists('get_debug_type')) { |
| 38 | - function get_debug_type($value): string { return p\Php80::get_debug_type($value); } |
|
| 38 | + function get_debug_type($value): string { return p\Php80::get_debug_type($value); } |
|
| 39 | 39 | } |
| 40 | 40 | if (!function_exists('get_resource_id')) { |
| 41 | - function get_resource_id($resource): int { return p\Php80::get_resource_id($resource); } |
|
| 41 | + function get_resource_id($resource): int { return p\Php80::get_resource_id($resource); } |
|
| 42 | 42 | } |
@@ -15,28 +15,28 @@ |
||
| 15 | 15 | return; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | -if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) { |
|
| 18 | +if ( ! defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) { |
|
| 19 | 19 | define('FILTER_VALIDATE_BOOL', \FILTER_VALIDATE_BOOLEAN); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | -if (!function_exists('fdiv')) { |
|
| 22 | +if ( ! function_exists('fdiv')) { |
|
| 23 | 23 | function fdiv(float $num1, float $num2): float { return p\Php80::fdiv($num1, $num2); } |
| 24 | 24 | } |
| 25 | -if (!function_exists('preg_last_error_msg')) { |
|
| 25 | +if ( ! function_exists('preg_last_error_msg')) { |
|
| 26 | 26 | function preg_last_error_msg(): string { return p\Php80::preg_last_error_msg(); } |
| 27 | 27 | } |
| 28 | -if (!function_exists('str_contains')) { |
|
| 28 | +if ( ! function_exists('str_contains')) { |
|
| 29 | 29 | function str_contains(?string $haystack, ?string $needle): bool { return p\Php80::str_contains($haystack ?? '', $needle ?? ''); } |
| 30 | 30 | } |
| 31 | -if (!function_exists('str_starts_with')) { |
|
| 31 | +if ( ! function_exists('str_starts_with')) { |
|
| 32 | 32 | function str_starts_with(?string $haystack, ?string $needle): bool { return p\Php80::str_starts_with($haystack ?? '', $needle ?? ''); } |
| 33 | 33 | } |
| 34 | -if (!function_exists('str_ends_with')) { |
|
| 34 | +if ( ! function_exists('str_ends_with')) { |
|
| 35 | 35 | function str_ends_with(?string $haystack, ?string $needle): bool { return p\Php80::str_ends_with($haystack ?? '', $needle ?? ''); } |
| 36 | 36 | } |
| 37 | -if (!function_exists('get_debug_type')) { |
|
| 37 | +if ( ! function_exists('get_debug_type')) { |
|
| 38 | 38 | function get_debug_type($value): string { return p\Php80::get_debug_type($value); } |
| 39 | 39 | } |
| 40 | -if (!function_exists('get_resource_id')) { |
|
| 40 | +if ( ! function_exists('get_resource_id')) { |
|
| 41 | 41 | function get_resource_id($resource): int { return p\Php80::get_resource_id($resource); } |
| 42 | 42 | } |