scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 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 * @param XPathExpr $xpath
48 *
49 * @return XPathExpr
50 */
51 public function translateRoot(XPathExpr $xpath)
52 {
53 return $xpath->addCondition('not(parent::*)');
54 }
55  
56 /**
57 * @param XPathExpr $xpath
58 *
59 * @return XPathExpr
60 */
61 public function translateFirstChild(XPathExpr $xpath)
62 {
63 return $xpath
64 ->addStarPrefix()
65 ->addNameTest()
66 ->addCondition('position() = 1');
67 }
68  
69 /**
70 * @param XPathExpr $xpath
71 *
72 * @return XPathExpr
73 */
74 public function translateLastChild(XPathExpr $xpath)
75 {
76 return $xpath
77 ->addStarPrefix()
78 ->addNameTest()
79 ->addCondition('position() = last()');
80 }
81  
82 /**
83 * @param XPathExpr $xpath
84 *
85 * @return XPathExpr
86 *
87 * @throws ExpressionErrorException
88 */
89 public function translateFirstOfType(XPathExpr $xpath)
90 {
91 if ('*' === $xpath->getElement()) {
92 throw new ExpressionErrorException('"*:first-of-type" is not implemented.');
93 }
94  
95 return $xpath
96 ->addStarPrefix()
97 ->addCondition('position() = 1');
98 }
99  
100 /**
101 * @param XPathExpr $xpath
102 *
103 * @return XPathExpr
104 *
105 * @throws ExpressionErrorException
106 */
107 public function translateLastOfType(XPathExpr $xpath)
108 {
109 if ('*' === $xpath->getElement()) {
110 throw new ExpressionErrorException('"*:last-of-type" is not implemented.');
111 }
112  
113 return $xpath
114 ->addStarPrefix()
115 ->addCondition('position() = last()');
116 }
117  
118 /**
119 * @param XPathExpr $xpath
120 *
121 * @return XPathExpr
122 */
123 public function translateOnlyChild(XPathExpr $xpath)
124 {
125 return $xpath
126 ->addStarPrefix()
127 ->addNameTest()
128 ->addCondition('last() = 1');
129 }
130  
131 /**
132 * @param XPathExpr $xpath
133 *
134 * @return XPathExpr
135 *
136 * @throws ExpressionErrorException
137 */
138 public function translateOnlyOfType(XPathExpr $xpath)
139 {
140 if ('*' === $xpath->getElement()) {
141 throw new ExpressionErrorException('"*:only-of-type" is not implemented.');
142 }
143  
144 return $xpath->addCondition('last() = 1');
145 }
146  
147 /**
148 * @param XPathExpr $xpath
149 *
150 * @return XPathExpr
151 */
152 public function translateEmpty(XPathExpr $xpath)
153 {
154 return $xpath->addCondition('not(*) and not(string-length())');
155 }
156  
157 /**
158 * {@inheritdoc}
159 */
160 public function getName()
161 {
162 return 'pseudo-class';
163 }
164 }