mailpoet
/
vendor-prefixed
/
symfony
/
css-selector
/
XPath
/
Extension
/
AttributeMatchingExtension.php
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
AttributeMatchingExtension.php
49 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\CssSelector\XPath\Extension; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\Translator; |
| 5 | use MailPoetVendor\Symfony\Component\CssSelector\XPath\XPathExpr; |
| 6 | class AttributeMatchingExtension extends AbstractExtension |
| 7 | { |
| 8 | public function getAttributeMatchingTranslators() : array |
| 9 | { |
| 10 | return ['exists' => [$this, 'translateExists'], '=' => [$this, 'translateEquals'], '~=' => [$this, 'translateIncludes'], '|=' => [$this, 'translateDashMatch'], '^=' => [$this, 'translatePrefixMatch'], '$=' => [$this, 'translateSuffixMatch'], '*=' => [$this, 'translateSubstringMatch'], '!=' => [$this, 'translateDifferent']]; |
| 11 | } |
| 12 | public function translateExists(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 13 | { |
| 14 | return $xpath->addCondition($attribute); |
| 15 | } |
| 16 | public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 17 | { |
| 18 | return $xpath->addCondition(\sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value))); |
| 19 | } |
| 20 | public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 21 | { |
| 22 | return $xpath->addCondition($value ? \sprintf('%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)', $attribute, Translator::getXpathLiteral(' ' . $value . ' ')) : '0'); |
| 23 | } |
| 24 | public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 25 | { |
| 26 | return $xpath->addCondition(\sprintf('%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))', $attribute, Translator::getXpathLiteral($value), Translator::getXpathLiteral($value . '-'))); |
| 27 | } |
| 28 | public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 29 | { |
| 30 | return $xpath->addCondition($value ? \sprintf('%1$s and starts-with(%1$s, %2$s)', $attribute, Translator::getXpathLiteral($value)) : '0'); |
| 31 | } |
| 32 | public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 33 | { |
| 34 | return $xpath->addCondition($value ? \sprintf('%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s', $attribute, \strlen($value) - 1, Translator::getXpathLiteral($value)) : '0'); |
| 35 | } |
| 36 | public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 37 | { |
| 38 | return $xpath->addCondition($value ? \sprintf('%1$s and contains(%1$s, %2$s)', $attribute, Translator::getXpathLiteral($value)) : '0'); |
| 39 | } |
| 40 | public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value) : XPathExpr |
| 41 | { |
| 42 | return $xpath->addCondition(\sprintf($value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s', $attribute, Translator::getXpathLiteral($value))); |
| 43 | } |
| 44 | public function getName() : string |
| 45 | { |
| 46 | return 'attribute-matching'; |
| 47 | } |
| 48 | } |
| 49 |