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
FunctionExtension.php
99 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\CssSelector\XPath\Extension; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\CssSelector\Exception\ExpressionErrorException; |
| 5 | use MailPoetVendor\Symfony\Component\CssSelector\Exception\SyntaxErrorException; |
| 6 | use MailPoetVendor\Symfony\Component\CssSelector\Node\FunctionNode; |
| 7 | use MailPoetVendor\Symfony\Component\CssSelector\Parser\Parser; |
| 8 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\Translator; |
| 9 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\XPathExpr; |
| 10 | class FunctionExtension extends AbstractExtension |
| 11 | { |
| 12 | public function getFunctionTranslators() : array |
| 13 | { |
| 14 | return ['nth-child' => [$this, 'translateNthChild'], 'nth-last-child' => [$this, 'translateNthLastChild'], 'nth-of-type' => [$this, 'translateNthOfType'], 'nth-last-of-type' => [$this, 'translateNthLastOfType'], 'contains' => [$this, 'translateContains'], 'lang' => [$this, 'translateLang']]; |
| 15 | } |
| 16 | public function translateNthChild(XPathExpr $xpath, FunctionNode $function, bool $last = \false, bool $addNameTest = \true) : XPathExpr |
| 17 | { |
| 18 | try { |
| 19 | [$a, $b] = Parser::parseSeries($function->getArguments()); |
| 20 | } catch (SyntaxErrorException $e) { |
| 21 | throw new ExpressionErrorException(\sprintf('Invalid series: "%s".', \implode('", "', $function->getArguments())), 0, $e); |
| 22 | } |
| 23 | $xpath->addStarPrefix(); |
| 24 | if ($addNameTest) { |
| 25 | $xpath->addNameTest(); |
| 26 | } |
| 27 | if (0 === $a) { |
| 28 | return $xpath->addCondition('position() = ' . ($last ? 'last() - ' . ($b - 1) : $b)); |
| 29 | } |
| 30 | if ($a < 0) { |
| 31 | if ($b < 1) { |
| 32 | return $xpath->addCondition('false()'); |
| 33 | } |
| 34 | $sign = '<='; |
| 35 | } else { |
| 36 | $sign = '>='; |
| 37 | } |
| 38 | $expr = 'position()'; |
| 39 | if ($last) { |
| 40 | $expr = 'last() - ' . $expr; |
| 41 | --$b; |
| 42 | } |
| 43 | if (0 !== $b) { |
| 44 | $expr .= ' - ' . $b; |
| 45 | } |
| 46 | $conditions = [\sprintf('%s %s 0', $expr, $sign)]; |
| 47 | if (1 !== $a && -1 !== $a) { |
| 48 | $conditions[] = \sprintf('(%s) mod %d = 0', $expr, $a); |
| 49 | } |
| 50 | return $xpath->addCondition(\implode(' and ', $conditions)); |
| 51 | // todo: handle an+b, odd, even |
| 52 | // an+b means every-a, plus b, e.g., 2n+1 means odd |
| 53 | // 0n+b means b |
| 54 | // n+0 means a=1, i.e., all elements |
| 55 | // an means every a elements, i.e., 2n means even |
| 56 | // -n means -1n |
| 57 | // -1n+6 means elements 6 and previous |
| 58 | } |
| 59 | public function translateNthLastChild(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 60 | { |
| 61 | return $this->translateNthChild($xpath, $function, \true); |
| 62 | } |
| 63 | public function translateNthOfType(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 64 | { |
| 65 | return $this->translateNthChild($xpath, $function, \false, \false); |
| 66 | } |
| 67 | public function translateNthLastOfType(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 68 | { |
| 69 | if ('*' === $xpath->getElement()) { |
| 70 | throw new ExpressionErrorException('"*:nth-of-type()" is not implemented.'); |
| 71 | } |
| 72 | return $this->translateNthChild($xpath, $function, \true, \false); |
| 73 | } |
| 74 | public function translateContains(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 75 | { |
| 76 | $arguments = $function->getArguments(); |
| 77 | foreach ($arguments as $token) { |
| 78 | if (!($token->isString() || $token->isIdentifier())) { |
| 79 | throw new ExpressionErrorException('Expected a single string or identifier for :contains(), got ' . \implode(', ', $arguments)); |
| 80 | } |
| 81 | } |
| 82 | return $xpath->addCondition(\sprintf('contains(string(.), %s)', Translator::getXpathLiteral($arguments[0]->getValue()))); |
| 83 | } |
| 84 | public function translateLang(XPathExpr $xpath, FunctionNode $function) : XPathExpr |
| 85 | { |
| 86 | $arguments = $function->getArguments(); |
| 87 | foreach ($arguments as $token) { |
| 88 | if (!($token->isString() || $token->isIdentifier())) { |
| 89 | throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got ' . \implode(', ', $arguments)); |
| 90 | } |
| 91 | } |
| 92 | return $xpath->addCondition(\sprintf('lang(%s)', Translator::getXpathLiteral($arguments[0]->getValue()))); |
| 93 | } |
| 94 | public function getName() : string |
| 95 | { |
| 96 | return 'function'; |
| 97 | } |
| 98 | } |
| 99 |