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
PseudoClassExtension.php
56 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\XPath\XPathExpr; |
| 6 | class PseudoClassExtension extends AbstractExtension |
| 7 | { |
| 8 | public function getPseudoClassTranslators() : array |
| 9 | { |
| 10 | return ['root' => [$this, 'translateRoot'], 'first-child' => [$this, 'translateFirstChild'], 'last-child' => [$this, 'translateLastChild'], 'first-of-type' => [$this, 'translateFirstOfType'], 'last-of-type' => [$this, 'translateLastOfType'], 'only-child' => [$this, 'translateOnlyChild'], 'only-of-type' => [$this, 'translateOnlyOfType'], 'empty' => [$this, 'translateEmpty']]; |
| 11 | } |
| 12 | public function translateRoot(XPathExpr $xpath) : XPathExpr |
| 13 | { |
| 14 | return $xpath->addCondition('not(parent::*)'); |
| 15 | } |
| 16 | public function translateFirstChild(XPathExpr $xpath) : XPathExpr |
| 17 | { |
| 18 | return $xpath->addStarPrefix()->addNameTest()->addCondition('position() = 1'); |
| 19 | } |
| 20 | public function translateLastChild(XPathExpr $xpath) : XPathExpr |
| 21 | { |
| 22 | return $xpath->addStarPrefix()->addNameTest()->addCondition('position() = last()'); |
| 23 | } |
| 24 | public function translateFirstOfType(XPathExpr $xpath) : XPathExpr |
| 25 | { |
| 26 | if ('*' === $xpath->getElement()) { |
| 27 | throw new ExpressionErrorException('"*:first-of-type" is not implemented.'); |
| 28 | } |
| 29 | return $xpath->addStarPrefix()->addCondition('position() = 1'); |
| 30 | } |
| 31 | public function translateLastOfType(XPathExpr $xpath) : XPathExpr |
| 32 | { |
| 33 | if ('*' === $xpath->getElement()) { |
| 34 | throw new ExpressionErrorException('"*:last-of-type" is not implemented.'); |
| 35 | } |
| 36 | return $xpath->addStarPrefix()->addCondition('position() = last()'); |
| 37 | } |
| 38 | public function translateOnlyChild(XPathExpr $xpath) : XPathExpr |
| 39 | { |
| 40 | return $xpath->addStarPrefix()->addNameTest()->addCondition('last() = 1'); |
| 41 | } |
| 42 | public function translateOnlyOfType(XPathExpr $xpath) : XPathExpr |
| 43 | { |
| 44 | $element = $xpath->getElement(); |
| 45 | return $xpath->addCondition(\sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element)); |
| 46 | } |
| 47 | public function translateEmpty(XPathExpr $xpath) : XPathExpr |
| 48 | { |
| 49 | return $xpath->addCondition('not(*) and not(string-length())'); |
| 50 | } |
| 51 | public function getName() : string |
| 52 | { |
| 53 | return 'pseudo-class'; |
| 54 | } |
| 55 | } |
| 56 |