4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\CssSelector\XPath\Extension;
14 use Symfony\Component\CssSelector\XPath\Translator;
15 use Symfony\Component\CssSelector\XPath\XPathExpr;
18 * XPath expression translator attribute extension.
20 * This component is a port of the Python cssselect library,
21 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
23 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
27 class AttributeMatchingExtension extends AbstractExtension
32 public function getAttributeMatchingTranslators()
35 'exists' => array($this, 'translateExists'),
36 '=' => array($this, 'translateEquals'),
37 '~=' => array($this, 'translateIncludes'),
38 '|=' => array($this, 'translateDashMatch'),
39 '^=' => array($this, 'translatePrefixMatch'),
40 '$=' => array($this, 'translateSuffixMatch'),
41 '*=' => array($this, 'translateSubstringMatch'),
42 '!=' => array($this, 'translateDifferent'),
46 public function translateExists(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
48 return $xpath->addCondition($attribute);
51 public function translateEquals(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
53 return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
56 public function translateIncludes(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
58 return $xpath->addCondition($value ? sprintf(
59 '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
61 Translator::getXpathLiteral(' '.$value.' ')
65 public function translateDashMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
67 return $xpath->addCondition(sprintf(
68 '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
70 Translator::getXpathLiteral($value),
71 Translator::getXpathLiteral($value.'-')
75 public function translatePrefixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
77 return $xpath->addCondition($value ? sprintf(
78 '%1$s and starts-with(%1$s, %2$s)',
80 Translator::getXpathLiteral($value)
84 public function translateSuffixMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
86 return $xpath->addCondition($value ? sprintf(
87 '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
90 Translator::getXpathLiteral($value)
94 public function translateSubstringMatch(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
96 return $xpath->addCondition($value ? sprintf(
97 '%1$s and contains(%1$s, %2$s)',
99 Translator::getXpathLiteral($value)
103 public function translateDifferent(XPathExpr $xpath, string $attribute, ?string $value): XPathExpr
105 return $xpath->addCondition(sprintf(
106 $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
108 Translator::getXpathLiteral($value)
115 public function getName()
117 return 'attribute-matching';