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;
14 use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
15 use Symfony\Component\CssSelector\Node\FunctionNode;
16 use Symfony\Component\CssSelector\Node\NodeInterface;
17 use Symfony\Component\CssSelector\Node\SelectorNode;
18 use Symfony\Component\CssSelector\Parser\Parser;
19 use Symfony\Component\CssSelector\Parser\ParserInterface;
22 * XPath expression translator interface.
24 * This component is a port of the Python cssselect library,
25 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
27 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
31 class Translator implements TranslatorInterface
36 * @var ParserInterface[]
38 private $shortcutParsers = array();
41 * @var Extension\ExtensionInterface[]
43 private $extensions = array();
45 private $nodeTranslators = array();
46 private $combinationTranslators = array();
47 private $functionTranslators = array();
48 private $pseudoClassTranslators = array();
49 private $attributeMatchingTranslators = array();
51 public function __construct(ParserInterface $parser = null)
53 $this->mainParser = $parser ?: new Parser();
56 ->registerExtension(new Extension\NodeExtension())
57 ->registerExtension(new Extension\CombinationExtension())
58 ->registerExtension(new Extension\FunctionExtension())
59 ->registerExtension(new Extension\PseudoClassExtension())
60 ->registerExtension(new Extension\AttributeMatchingExtension())
65 * @param string $element
69 public static function getXpathLiteral($element)
71 if (false === strpos($element, "'")) {
72 return "'".$element."'";
75 if (false === strpos($element, '"')) {
76 return '"'.$element.'"';
82 if (false !== $pos = strpos($string, "'")) {
83 $parts[] = sprintf("'%s'", substr($string, 0, $pos));
85 $string = substr($string, $pos + 1);
87 $parts[] = "'$string'";
92 return sprintf('concat(%s)', implode($parts, ', '));
98 public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
100 $selectors = $this->parseSelectors($cssExpr);
102 /** @var SelectorNode $selector */
103 foreach ($selectors as $index => $selector) {
104 if (null !== $selector->getPseudoElement()) {
105 throw new ExpressionErrorException('Pseudo-elements are not supported.');
108 $selectors[$index] = $this->selectorToXPath($selector, $prefix);
111 return implode(' | ', $selectors);
117 public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
119 return ($prefix ?: '').$this->nodeToXPath($selector);
123 * Registers an extension.
127 public function registerExtension(Extension\ExtensionInterface $extension)
129 $this->extensions[$extension->getName()] = $extension;
131 $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
132 $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
133 $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
134 $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
135 $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
141 * @param string $name
143 * @return Extension\ExtensionInterface
145 * @throws ExpressionErrorException
147 public function getExtension($name)
149 if (!isset($this->extensions[$name])) {
150 throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
153 return $this->extensions[$name];
157 * Registers a shortcut parser.
161 public function registerParserShortcut(ParserInterface $shortcut)
163 $this->shortcutParsers[] = $shortcut;
171 * @throws ExpressionErrorException
173 public function nodeToXPath(NodeInterface $node)
175 if (!isset($this->nodeTranslators[$node->getNodeName()])) {
176 throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
179 return call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
183 * @param string $combiner
184 * @param NodeInterface $xpath
185 * @param NodeInterface $combinedXpath
189 * @throws ExpressionErrorException
191 public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
193 if (!isset($this->combinationTranslators[$combiner])) {
194 throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
197 return call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
203 * @throws ExpressionErrorException
205 public function addFunction(XPathExpr $xpath, FunctionNode $function)
207 if (!isset($this->functionTranslators[$function->getName()])) {
208 throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
211 return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
215 * @param XPathExpr $xpath
216 * @param string $pseudoClass
220 * @throws ExpressionErrorException
222 public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
224 if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
225 throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
228 return call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
232 * @param XPathExpr $xpath
233 * @param string $operator
234 * @param string $attribute
235 * @param string $value
239 * @throws ExpressionErrorException
241 public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
243 if (!isset($this->attributeMatchingTranslators[$operator])) {
244 throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
247 return call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
253 * @return SelectorNode[]
255 private function parseSelectors($css)
257 foreach ($this->shortcutParsers as $shortcut) {
258 $tokens = $shortcut->parse($css);
260 if (!empty($tokens)) {
265 return $this->mainParser->parse($css);