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
34 * @var ParserInterface
39 * @var ParserInterface[]
41 private $shortcutParsers = array();
44 * @var Extension\ExtensionInterface
46 private $extensions = array();
51 private $nodeTranslators = array();
56 private $combinationTranslators = array();
61 private $functionTranslators = array();
66 private $pseudoClassTranslators = array();
71 private $attributeMatchingTranslators = array();
73 public function __construct(ParserInterface $parser = null)
75 $this->mainParser = $parser ?: new Parser();
78 ->registerExtension(new Extension\NodeExtension())
79 ->registerExtension(new Extension\CombinationExtension())
80 ->registerExtension(new Extension\FunctionExtension())
81 ->registerExtension(new Extension\PseudoClassExtension())
82 ->registerExtension(new Extension\AttributeMatchingExtension())
87 * @param string $element
91 public static function getXpathLiteral($element)
93 if (false === strpos($element, "'")) {
94 return "'".$element."'";
97 if (false === strpos($element, '"')) {
98 return '"'.$element.'"';
104 if (false !== $pos = strpos($string, "'")) {
105 $parts[] = sprintf("'%s'", substr($string, 0, $pos));
107 $string = substr($string, $pos + 1);
109 $parts[] = "'$string'";
114 return sprintf('concat(%s)', implode($parts, ', '));
120 public function cssToXPath($cssExpr, $prefix = 'descendant-or-self::')
122 $selectors = $this->parseSelectors($cssExpr);
124 /** @var SelectorNode $selector */
125 foreach ($selectors as $index => $selector) {
126 if (null !== $selector->getPseudoElement()) {
127 throw new ExpressionErrorException('Pseudo-elements are not supported.');
130 $selectors[$index] = $this->selectorToXPath($selector, $prefix);
133 return implode(' | ', $selectors);
139 public function selectorToXPath(SelectorNode $selector, $prefix = 'descendant-or-self::')
141 return ($prefix ?: '').$this->nodeToXPath($selector);
145 * Registers an extension.
147 * @param Extension\ExtensionInterface $extension
151 public function registerExtension(Extension\ExtensionInterface $extension)
153 $this->extensions[$extension->getName()] = $extension;
155 $this->nodeTranslators = array_merge($this->nodeTranslators, $extension->getNodeTranslators());
156 $this->combinationTranslators = array_merge($this->combinationTranslators, $extension->getCombinationTranslators());
157 $this->functionTranslators = array_merge($this->functionTranslators, $extension->getFunctionTranslators());
158 $this->pseudoClassTranslators = array_merge($this->pseudoClassTranslators, $extension->getPseudoClassTranslators());
159 $this->attributeMatchingTranslators = array_merge($this->attributeMatchingTranslators, $extension->getAttributeMatchingTranslators());
165 * @param string $name
167 * @return Extension\ExtensionInterface
169 * @throws ExpressionErrorException
171 public function getExtension($name)
173 if (!isset($this->extensions[$name])) {
174 throw new ExpressionErrorException(sprintf('Extension "%s" not registered.', $name));
177 return $this->extensions[$name];
181 * Registers a shortcut parser.
183 * @param ParserInterface $shortcut
187 public function registerParserShortcut(ParserInterface $shortcut)
189 $this->shortcutParsers[] = $shortcut;
195 * @param NodeInterface $node
199 * @throws ExpressionErrorException
201 public function nodeToXPath(NodeInterface $node)
203 if (!isset($this->nodeTranslators[$node->getNodeName()])) {
204 throw new ExpressionErrorException(sprintf('Node "%s" not supported.', $node->getNodeName()));
207 return call_user_func($this->nodeTranslators[$node->getNodeName()], $node, $this);
211 * @param string $combiner
212 * @param NodeInterface $xpath
213 * @param NodeInterface $combinedXpath
217 * @throws ExpressionErrorException
219 public function addCombination($combiner, NodeInterface $xpath, NodeInterface $combinedXpath)
221 if (!isset($this->combinationTranslators[$combiner])) {
222 throw new ExpressionErrorException(sprintf('Combiner "%s" not supported.', $combiner));
225 return call_user_func($this->combinationTranslators[$combiner], $this->nodeToXPath($xpath), $this->nodeToXPath($combinedXpath));
229 * @param XPathExpr $xpath
230 * @param FunctionNode $function
234 * @throws ExpressionErrorException
236 public function addFunction(XPathExpr $xpath, FunctionNode $function)
238 if (!isset($this->functionTranslators[$function->getName()])) {
239 throw new ExpressionErrorException(sprintf('Function "%s" not supported.', $function->getName()));
242 return call_user_func($this->functionTranslators[$function->getName()], $xpath, $function);
246 * @param XPathExpr $xpath
247 * @param string $pseudoClass
251 * @throws ExpressionErrorException
253 public function addPseudoClass(XPathExpr $xpath, $pseudoClass)
255 if (!isset($this->pseudoClassTranslators[$pseudoClass])) {
256 throw new ExpressionErrorException(sprintf('Pseudo-class "%s" not supported.', $pseudoClass));
259 return call_user_func($this->pseudoClassTranslators[$pseudoClass], $xpath);
263 * @param XPathExpr $xpath
264 * @param string $operator
265 * @param string $attribute
266 * @param string $value
270 * @throws ExpressionErrorException
272 public function addAttributeMatching(XPathExpr $xpath, $operator, $attribute, $value)
274 if (!isset($this->attributeMatchingTranslators[$operator])) {
275 throw new ExpressionErrorException(sprintf('Attribute matcher operator "%s" not supported.', $operator));
278 return call_user_func($this->attributeMatchingTranslators[$operator], $xpath, $attribute, $value);
284 * @return SelectorNode[]
286 private function parseSelectors($css)
288 foreach ($this->shortcutParsers as $shortcut) {
289 $tokens = $shortcut->parse($css);
291 if (!empty($tokens)) {
296 return $this->mainParser->parse($css);