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
31 public function __construct(Translator $translator)
34 ->getExtension('node')
35 ->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true)
36 ->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true);
42 public function getPseudoClassTranslators()
45 'checked' => array($this, 'translateChecked'),
46 'link' => array($this, 'translateLink'),
47 'disabled' => array($this, 'translateDisabled'),
48 'enabled' => array($this, 'translateEnabled'),
49 'selected' => array($this, 'translateSelected'),
50 'invalid' => array($this, 'translateInvalid'),
51 'hover' => array($this, 'translateHover'),
52 'visited' => array($this, 'translateVisited'),
59 public function getFunctionTranslators()
62 'lang' => array($this, 'translateLang'),
69 public function translateChecked(XPathExpr $xpath)
71 return $xpath->addCondition(
73 ."and (name(.) = 'input' or name(.) = 'command')"
74 ."and (@type = 'checkbox' or @type = 'radio'))"
81 public function translateLink(XPathExpr $xpath)
83 return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')");
89 public function translateDisabled(XPathExpr $xpath)
91 return $xpath->addCondition(
95 ."(name(.) = 'input' and @type != 'hidden')"
96 ." or name(.) = 'button'"
97 ." or name(.) = 'select'"
98 ." or name(.) = 'textarea'"
99 ." or name(.) = 'command'"
100 ." or name(.) = 'fieldset'"
101 ." or name(.) = 'optgroup'"
102 ." or name(.) = 'option'"
105 ."(name(.) = 'input' and @type != 'hidden')"
106 ." or name(.) = 'button'"
107 ." or name(.) = 'select'"
108 ." or name(.) = 'textarea'"
110 .' and ancestor::fieldset[@disabled]'
112 // todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any."
118 public function translateEnabled(XPathExpr $xpath)
120 return $xpath->addCondition(
124 ." or name(.) = 'link'"
125 ." or name(.) = 'area'"
129 ."name(.) = 'command'"
130 ." or name(.) = 'fieldset'"
131 ." or name(.) = 'optgroup'"
133 .' and not(@disabled)'
136 ."(name(.) = 'input' and @type != 'hidden')"
137 ." or name(.) = 'button'"
138 ." or name(.) = 'select'"
139 ." or name(.) = 'textarea'"
140 ." or name(.) = 'keygen'"
142 .' and not (@disabled or ancestor::fieldset[@disabled])'
144 ."name(.) = 'option' and not("
145 .'@disabled or ancestor::optgroup[@disabled]'
154 * @throws ExpressionErrorException
156 public function translateLang(XPathExpr $xpath, FunctionNode $function)
158 $arguments = $function->getArguments();
159 foreach ($arguments as $token) {
160 if (!($token->isString() || $token->isIdentifier())) {
161 throw new ExpressionErrorException('Expected a single string or identifier for :lang(), got '.implode(', ', $arguments));
165 return $xpath->addCondition(sprintf(
166 'ancestor-or-self::*[@lang][1][starts-with(concat('
167 ."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')"
170 Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-')
177 public function translateSelected(XPathExpr $xpath)
179 return $xpath->addCondition("(@selected and name(.) = 'option')");
185 public function translateInvalid(XPathExpr $xpath)
187 return $xpath->addCondition('0');
193 public function translateHover(XPathExpr $xpath)
195 return $xpath->addCondition('0');
201 public function translateVisited(XPathExpr $xpath)
203 return $xpath->addCondition('0');
209 public function getName()