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' => [$this, 'translateExists'],
36 '=' => [$this, 'translateEquals'],
37 '~=' => [$this, 'translateIncludes'],
38 '|=' => [$this, 'translateDashMatch'],
39 '^=' => [$this, 'translatePrefixMatch'],
40 '$=' => [$this, 'translateSuffixMatch'],
41 '*=' => [$this, 'translateSubstringMatch'],
42 '!=' => [$this, 'translateDifferent'],
47 * @param XPathExpr $xpath
48 * @param string $attribute
49 * @param string $value
53 public function translateExists(XPathExpr $xpath, $attribute, $value)
55 return $xpath->addCondition($attribute);
59 * @param XPathExpr $xpath
60 * @param string $attribute
61 * @param string $value
65 public function translateEquals(XPathExpr $xpath, $attribute, $value)
67 return $xpath->addCondition(sprintf('%s = %s', $attribute, Translator::getXpathLiteral($value)));
71 * @param XPathExpr $xpath
72 * @param string $attribute
73 * @param string $value
77 public function translateIncludes(XPathExpr $xpath, $attribute, $value)
79 return $xpath->addCondition($value ? sprintf(
80 '%1$s and contains(concat(\' \', normalize-space(%1$s), \' \'), %2$s)',
82 Translator::getXpathLiteral(' '.$value.' ')
87 * @param XPathExpr $xpath
88 * @param string $attribute
89 * @param string $value
93 public function translateDashMatch(XPathExpr $xpath, $attribute, $value)
95 return $xpath->addCondition(sprintf(
96 '%1$s and (%1$s = %2$s or starts-with(%1$s, %3$s))',
98 Translator::getXpathLiteral($value),
99 Translator::getXpathLiteral($value.'-')
104 * @param XPathExpr $xpath
105 * @param string $attribute
106 * @param string $value
110 public function translatePrefixMatch(XPathExpr $xpath, $attribute, $value)
112 return $xpath->addCondition($value ? sprintf(
113 '%1$s and starts-with(%1$s, %2$s)',
115 Translator::getXpathLiteral($value)
120 * @param XPathExpr $xpath
121 * @param string $attribute
122 * @param string $value
126 public function translateSuffixMatch(XPathExpr $xpath, $attribute, $value)
128 return $xpath->addCondition($value ? sprintf(
129 '%1$s and substring(%1$s, string-length(%1$s)-%2$s) = %3$s',
132 Translator::getXpathLiteral($value)
137 * @param XPathExpr $xpath
138 * @param string $attribute
139 * @param string $value
143 public function translateSubstringMatch(XPathExpr $xpath, $attribute, $value)
145 return $xpath->addCondition($value ? sprintf(
146 '%1$s and contains(%1$s, %2$s)',
148 Translator::getXpathLiteral($value)
153 * @param XPathExpr $xpath
154 * @param string $attribute
155 * @param string $value
159 public function translateDifferent(XPathExpr $xpath, $attribute, $value)
161 return $xpath->addCondition(sprintf(
162 $value ? 'not(%1$s) or %1$s != %2$s' : '%s != %s',
164 Translator::getXpathLiteral($value)
171 public function getName()
173 return 'attribute-matching';