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\XPath\XPathExpr;
18 * XPath expression translator pseudo-class 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 PseudoClassExtension extends AbstractExtension
32 public function getPseudoClassTranslators()
35 'root' => array($this, 'translateRoot'),
36 'first-child' => array($this, 'translateFirstChild'),
37 'last-child' => array($this, 'translateLastChild'),
38 'first-of-type' => array($this, 'translateFirstOfType'),
39 'last-of-type' => array($this, 'translateLastOfType'),
40 'only-child' => array($this, 'translateOnlyChild'),
41 'only-of-type' => array($this, 'translateOnlyOfType'),
42 'empty' => array($this, 'translateEmpty'),
49 public function translateRoot(XPathExpr $xpath)
51 return $xpath->addCondition('not(parent::*)');
57 public function translateFirstChild(XPathExpr $xpath)
62 ->addCondition('position() = 1');
68 public function translateLastChild(XPathExpr $xpath)
73 ->addCondition('position() = last()');
79 * @throws ExpressionErrorException
81 public function translateFirstOfType(XPathExpr $xpath)
83 if ('*' === $xpath->getElement()) {
84 throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
89 ->addCondition('position() = 1');
95 * @throws ExpressionErrorException
97 public function translateLastOfType(XPathExpr $xpath)
99 if ('*' === $xpath->getElement()) {
100 throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
105 ->addCondition('position() = last()');
111 public function translateOnlyChild(XPathExpr $xpath)
116 ->addCondition('last() = 1');
122 * @throws ExpressionErrorException
124 public function translateOnlyOfType(XPathExpr $xpath)
126 if ('*' === $xpath->getElement()) {
127 throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
130 return $xpath->addCondition('last() = 1');
136 public function translateEmpty(XPathExpr $xpath)
138 return $xpath->addCondition('not(*) and not(string-length())');
144 public function getName()
146 return 'pseudo-class';