Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/css-selector/XPath/Extension/HtmlExtension.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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\Node\FunctionNode; | |
16 use Symfony\Component\CssSelector\XPath\Translator; | |
17 use Symfony\Component\CssSelector\XPath\XPathExpr; | |
18 | |
19 /** | |
20 * XPath expression translator HTML extension. | |
21 * | |
22 * This component is a port of the Python cssselect library, | |
23 * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. | |
24 * | |
25 * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com> | |
26 * | |
27 * @internal | |
28 */ | |
29 class HtmlExtension extends AbstractExtension | |
30 { | |
31 /** | |
32 * Constructor. | |
33 * | |
34 * @param Translator $translator | |
35 */ | |
36 public function __construct(Translator $translator) | |
37 { | |
38 $translator | |
39 ->getExtension('node') | |
40 ->setFlag(NodeExtension::ELEMENT_NAME_IN_LOWER_CASE, true) | |
41 ->setFlag(NodeExtension::ATTRIBUTE_NAME_IN_LOWER_CASE, true); | |
42 } | |
43 | |
44 /** | |
45 * {@inheritdoc} | |
46 */ | |
47 public function getPseudoClassTranslators() | |
48 { | |
49 return array( | |
50 'checked' => array($this, 'translateChecked'), | |
51 'link' => array($this, 'translateLink'), | |
52 'disabled' => array($this, 'translateDisabled'), | |
53 'enabled' => array($this, 'translateEnabled'), | |
54 'selected' => array($this, 'translateSelected'), | |
55 'invalid' => array($this, 'translateInvalid'), | |
56 'hover' => array($this, 'translateHover'), | |
57 'visited' => array($this, 'translateVisited'), | |
58 ); | |
59 } | |
60 | |
61 /** | |
62 * {@inheritdoc} | |
63 */ | |
64 public function getFunctionTranslators() | |
65 { | |
66 return array( | |
67 'lang' => array($this, 'translateLang'), | |
68 ); | |
69 } | |
70 | |
71 /** | |
72 * @param XPathExpr $xpath | |
73 * | |
74 * @return XPathExpr | |
75 */ | |
76 public function translateChecked(XPathExpr $xpath) | |
77 { | |
78 return $xpath->addCondition( | |
79 '(@checked ' | |
80 ."and (name(.) = 'input' or name(.) = 'command')" | |
81 ."and (@type = 'checkbox' or @type = 'radio'))" | |
82 ); | |
83 } | |
84 | |
85 /** | |
86 * @param XPathExpr $xpath | |
87 * | |
88 * @return XPathExpr | |
89 */ | |
90 public function translateLink(XPathExpr $xpath) | |
91 { | |
92 return $xpath->addCondition("@href and (name(.) = 'a' or name(.) = 'link' or name(.) = 'area')"); | |
93 } | |
94 | |
95 /** | |
96 * @param XPathExpr $xpath | |
97 * | |
98 * @return XPathExpr | |
99 */ | |
100 public function translateDisabled(XPathExpr $xpath) | |
101 { | |
102 return $xpath->addCondition( | |
103 '(' | |
104 .'@disabled and' | |
105 .'(' | |
106 ."(name(.) = 'input' and @type != 'hidden')" | |
107 ." or name(.) = 'button'" | |
108 ." or name(.) = 'select'" | |
109 ." or name(.) = 'textarea'" | |
110 ." or name(.) = 'command'" | |
111 ." or name(.) = 'fieldset'" | |
112 ." or name(.) = 'optgroup'" | |
113 ." or name(.) = 'option'" | |
114 .')' | |
115 .') or (' | |
116 ."(name(.) = 'input' and @type != 'hidden')" | |
117 ." or name(.) = 'button'" | |
118 ." or name(.) = 'select'" | |
119 ." or name(.) = 'textarea'" | |
120 .')' | |
121 .' and ancestor::fieldset[@disabled]' | |
122 ); | |
123 // todo: in the second half, add "and is not a descendant of that fieldset element's first legend element child, if any." | |
124 } | |
125 | |
126 /** | |
127 * @param XPathExpr $xpath | |
128 * | |
129 * @return XPathExpr | |
130 */ | |
131 public function translateEnabled(XPathExpr $xpath) | |
132 { | |
133 return $xpath->addCondition( | |
134 '(' | |
135 .'@href and (' | |
136 ."name(.) = 'a'" | |
137 ." or name(.) = 'link'" | |
138 ." or name(.) = 'area'" | |
139 .')' | |
140 .') or (' | |
141 .'(' | |
142 ."name(.) = 'command'" | |
143 ." or name(.) = 'fieldset'" | |
144 ." or name(.) = 'optgroup'" | |
145 .')' | |
146 .' and not(@disabled)' | |
147 .') or (' | |
148 .'(' | |
149 ."(name(.) = 'input' and @type != 'hidden')" | |
150 ." or name(.) = 'button'" | |
151 ." or name(.) = 'select'" | |
152 ." or name(.) = 'textarea'" | |
153 ." or name(.) = 'keygen'" | |
154 .')' | |
155 .' and not (@disabled or ancestor::fieldset[@disabled])' | |
156 .') or (' | |
157 ."name(.) = 'option' and not(" | |
158 .'@disabled or ancestor::optgroup[@disabled]' | |
159 .')' | |
160 .')' | |
161 ); | |
162 } | |
163 | |
164 /** | |
165 * @param XPathExpr $xpath | |
166 * @param FunctionNode $function | |
167 * | |
168 * @return XPathExpr | |
169 * | |
170 * @throws ExpressionErrorException | |
171 */ | |
172 public function translateLang(XPathExpr $xpath, FunctionNode $function) | |
173 { | |
174 $arguments = $function->getArguments(); | |
175 foreach ($arguments as $token) { | |
176 if (!($token->isString() || $token->isIdentifier())) { | |
177 throw new ExpressionErrorException( | |
178 'Expected a single string or identifier for :lang(), got ' | |
179 .implode(', ', $arguments) | |
180 ); | |
181 } | |
182 } | |
183 | |
184 return $xpath->addCondition(sprintf( | |
185 'ancestor-or-self::*[@lang][1][starts-with(concat(' | |
186 ."translate(@%s, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '-')" | |
187 .', %s)]', | |
188 'lang', | |
189 Translator::getXpathLiteral(strtolower($arguments[0]->getValue()).'-') | |
190 )); | |
191 } | |
192 | |
193 /** | |
194 * @param XPathExpr $xpath | |
195 * | |
196 * @return XPathExpr | |
197 */ | |
198 public function translateSelected(XPathExpr $xpath) | |
199 { | |
200 return $xpath->addCondition("(@selected and name(.) = 'option')"); | |
201 } | |
202 | |
203 /** | |
204 * @param XPathExpr $xpath | |
205 * | |
206 * @return XPathExpr | |
207 */ | |
208 public function translateInvalid(XPathExpr $xpath) | |
209 { | |
210 return $xpath->addCondition('0'); | |
211 } | |
212 | |
213 /** | |
214 * @param XPathExpr $xpath | |
215 * | |
216 * @return XPathExpr | |
217 */ | |
218 public function translateHover(XPathExpr $xpath) | |
219 { | |
220 return $xpath->addCondition('0'); | |
221 } | |
222 | |
223 /** | |
224 * @param XPathExpr $xpath | |
225 * | |
226 * @return XPathExpr | |
227 */ | |
228 public function translateVisited(XPathExpr $xpath) | |
229 { | |
230 return $xpath->addCondition('0'); | |
231 } | |
232 | |
233 /** | |
234 * {@inheritdoc} | |
235 */ | |
236 public function getName() | |
237 { | |
238 return 'html'; | |
239 } | |
240 } |