1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\CssSelector\XPath\Extension;
13
14use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
15use Symfony\Component\CssSelector\XPath\XPathExpr;
16
17/**
18 * XPath expression translator pseudo-class extension.
19 *
20 * This component is a port of the Python cssselect library,
21 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
22 *
23 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
24 *
25 * @internal
26 */
27class PseudoClassExtension extends AbstractExtension
28{
29    /**
30     * {@inheritdoc}
31     */
32    public function getPseudoClassTranslators(): array
33    {
34        return [
35            'root' => [$this, 'translateRoot'],
36            'first-child' => [$this, 'translateFirstChild'],
37            'last-child' => [$this, 'translateLastChild'],
38            'first-of-type' => [$this, 'translateFirstOfType'],
39            'last-of-type' => [$this, 'translateLastOfType'],
40            'only-child' => [$this, 'translateOnlyChild'],
41            'only-of-type' => [$this, 'translateOnlyOfType'],
42            'empty' => [$this, 'translateEmpty'],
43        ];
44    }
45
46    public function translateRoot(XPathExpr $xpath): XPathExpr
47    {
48        return $xpath->addCondition('not(parent::*)');
49    }
50
51    public function translateFirstChild(XPathExpr $xpath): XPathExpr
52    {
53        return $xpath
54            ->addStarPrefix()
55            ->addNameTest()
56            ->addCondition('position() = 1');
57    }
58
59    public function translateLastChild(XPathExpr $xpath): XPathExpr
60    {
61        return $xpath
62            ->addStarPrefix()
63            ->addNameTest()
64            ->addCondition('position() = last()');
65    }
66
67    /**
68     * @throws ExpressionErrorException
69     */
70    public function translateFirstOfType(XPathExpr $xpath): XPathExpr
71    {
72        if ('*' === $xpath->getElement()) {
73            throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
74        }
75
76        return $xpath
77            ->addStarPrefix()
78            ->addCondition('position() = 1');
79    }
80
81    /**
82     * @throws ExpressionErrorException
83     */
84    public function translateLastOfType(XPathExpr $xpath): XPathExpr
85    {
86        if ('*' === $xpath->getElement()) {
87            throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
88        }
89
90        return $xpath
91            ->addStarPrefix()
92            ->addCondition('position() = last()');
93    }
94
95    public function translateOnlyChild(XPathExpr $xpath): XPathExpr
96    {
97        return $xpath
98            ->addStarPrefix()
99            ->addNameTest()
100            ->addCondition('last() = 1');
101    }
102
103    public function translateOnlyOfType(XPathExpr $xpath): XPathExpr
104    {
105        $element = $xpath->getElement();
106
107        return $xpath->addCondition(sprintf('count(preceding-sibling::%s)=0 and count(following-sibling::%s)=0', $element, $element));
108    }
109
110    public function translateEmpty(XPathExpr $xpath): XPathExpr
111    {
112        return $xpath->addCondition('not(*) and not(string-length())');
113    }
114
115    /**
116     * {@inheritdoc}
117     */
118    public function getName(): string
119    {
120        return 'pseudo-class';
121    }
122}
123