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\Exception\ExpressionErrorException;
15 use Symfony\Component\CssSelector\Node\FunctionNode;
16 use Symfony\Component\CssSelector\XPath\Translator;
17 use Symfony\Component\CssSelector\XPath\XPathExpr;
20 * XPath expression translator HTML extension.
22 * This component is a port of the Python cssselect library,
23 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
25 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
29 class HtmlExtension extends AbstractExtension
34 * @param Translator $translator
36 public function __construct(Translator $translator)
39 ->getExtension('node')
40 ->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true)
41 ->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true);
47 public function getPseudoClassTranslators()
50 'checked' => array($this, 'translateChecked'),
51 'link' => array($this, 'translateLink'),
52 'disabled' => array($this, 'translateDisabled'),
53 'enabled' => array($this, 'translateEnabled'),
54 'selected' => array($this, 'translateSelected'),
55 'invalid' => array($this, 'translateInvalid'),
56 'hover' => array($this, 'translateHover'),
57 'visited' => array($this, 'translateVisited'),
64 public function getFunctionTranslators()
67 'lang' => array($this, 'translateLang'),
72 * @param XPathExpr $xpath
76 public function translateChecked(XPathExpr $xpath)
78 return $xpath->addCondition(
80 ."and (name(.) = 'input' or name(.) = 'command')"
81 ."and (@type = 'checkbox' or @type = 'radio'))"
86 * @param XPathExpr $xpath
90 public function translateLink(XPathExpr $xpath)
92 return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
96 * @param XPathExpr $xpath
100 public function translateDisabled(XPathExpr $xpath)
102 return $xpath->addCondition(
106 ."(name(.) = 'input' and @type != 'hidden')"
107 ." or name(.) = 'button'"
108 ." or name(.) = 'select'"
109 ." or name(.) = 'textarea'"
110 ." or name(.) = 'command'"
111 ." or name(.) = 'fieldset'"
112 ." or name(.) = 'optgroup'"
113 ." or name(.) = 'option'"
116 ."(name(.) = 'input' and @type != 'hidden')"
117 ." or name(.) = 'button'"
118 ." or name(.) = 'select'"
119 ." or name(.) = 'textarea'"
121 .' and ancestor::fieldset[@disabled]'
123 // todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any."
127 * @param XPathExpr $xpath
131 public function translateEnabled(XPathExpr $xpath)
133 return $xpath->addCondition(
137 ." or name(.) = 'link'"
138 ." or name(.) = 'area'"
142 ."name(.) = 'command'"
143 ." or name(.) = 'fieldset'"
144 ." or name(.) = 'optgroup'"
146 .' and not(@disabled)'
149 ."(name(.) = 'input' and @type != 'hidden')"
150 ." or name(.) = 'button'"
151 ." or name(.) = 'select'"
152 ." or name(.) = 'textarea'"
153 ." or name(.) = 'keygen'"
155 .' and not (@disabled or ancestor::fieldset[@disabled])'
157 ."name(.) = 'option' and not("
158 .'@disabled or ancestor::optgroup[@disabled]'
165 * @param XPathExpr $xpath
166 * @param FunctionNode $function
170 * @throws ExpressionErrorException
172 public function translateLang(XPathExpr $xpath, FunctionNode $function)
174 $arguments = $function->getArguments();
175 foreach ($arguments as $token) {
176 if (!($token->isString() || $token->isIdentifier())) {
177 throw new ExpressionErrorException(
178 'Expected a single string or identifier for :lang(), got '
179 .implode(', ', $arguments)
184 return $xpath->addCondition(sprintf(
185 'ancestor-or-self::*[@lang][1][starts-with(concat('
186 ."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')"
189 Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-')
194 * @param XPathExpr $xpath
198 public function translateSelected(XPathExpr $xpath)
200 return $xpath->addCondition("(@selected and name(.) = 'option')");
204 * @param XPathExpr $xpath
208 public function translateInvalid(XPathExpr $xpath)
210 return $xpath->addCondition('0');
214 * @param XPathExpr $xpath
218 public function translateHover(XPathExpr $xpath)
220 return $xpath->addCondition('0');
224 * @param XPathExpr $xpath
228 public function translateVisited(XPathExpr $xpath)
230 return $xpath->addCondition('0');
236 public function getName()