378dfb7de17736700131d27cc3b88964f6252ffb
[yaffs-website] / vendor / symfony / css-selector / XPath / Extension / PseudoClassExtension.php
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
12 namespace Symfony\Component\CssSelector\XPath\Extension;
13
14 use Symfony\Component\CssSelector\Exception\ExpressionErrorException;
15 use 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  */
27 class PseudoClassExtension extends AbstractExtension
28 {
29     /**
30      * {@inheritdoc}
31      */
32     public function getPseudoClassTranslators()
33     {
34         return array(
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'),
43         );
44     }
45
46     /**
47      * @return XPathExpr
48      */
49     public function translateRoot(XPathExpr $xpath)
50     {
51         return $xpath->addCondition('not(parent::*)');
52     }
53
54     /**
55      * @return XPathExpr
56      */
57     public function translateFirstChild(XPathExpr $xpath)
58     {
59         return $xpath
60             ->addStarPrefix()
61             ->addNameTest()
62             ->addCondition('position() = 1');
63     }
64
65     /**
66      * @return XPathExpr
67      */
68     public function translateLastChild(XPathExpr $xpath)
69     {
70         return $xpath
71             ->addStarPrefix()
72             ->addNameTest()
73             ->addCondition('position() = last()');
74     }
75
76     /**
77      * @return XPathExpr
78      *
79      * @throws ExpressionErrorException
80      */
81     public function translateFirstOfType(XPathExpr $xpath)
82     {
83         if ('*' === $xpath->getElement()) {
84             throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
85         }
86
87         return $xpath
88             ->addStarPrefix()
89             ->addCondition('position() = 1');
90     }
91
92     /**
93      * @return XPathExpr
94      *
95      * @throws ExpressionErrorException
96      */
97     public function translateLastOfType(XPathExpr $xpath)
98     {
99         if ('*' === $xpath->getElement()) {
100             throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
101         }
102
103         return $xpath
104             ->addStarPrefix()
105             ->addCondition('position() = last()');
106     }
107
108     /**
109      * @return XPathExpr
110      */
111     public function translateOnlyChild(XPathExpr $xpath)
112     {
113         return $xpath
114             ->addStarPrefix()
115             ->addNameTest()
116             ->addCondition('last() = 1');
117     }
118
119     /**
120      * @return XPathExpr
121      *
122      * @throws ExpressionErrorException
123      */
124     public function translateOnlyOfType(XPathExpr $xpath)
125     {
126         if ('*' === $xpath->getElement()) {
127             throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
128         }
129
130         return $xpath->addCondition('last() = 1');
131     }
132
133     /**
134      * @return XPathExpr
135      */
136     public function translateEmpty(XPathExpr $xpath)
137     {
138         return $xpath->addCondition('not(*) and not(string-length())');
139     }
140
141     /**
142      * {@inheritdoc}
143      */
144     public function getName()
145     {
146         return 'pseudo-class';
147     }
148 }