|
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; |
|
13 |
|
14 /** |
|
15 * XPathExpr represents an XPath expression. |
|
16 * |
|
17 * This component is a port of the Python lxml library, |
|
18 * which is copyright Infrae and distributed under the BSD license. |
|
19 * |
|
20 * @author Fabien Potencier <fabien@symfony.com> |
|
21 */ |
|
22 class XPathExpr |
|
23 { |
|
24 private $prefix; |
|
25 private $path; |
|
26 private $element; |
|
27 private $condition; |
|
28 private $starPrefix; |
|
29 |
|
30 /** |
|
31 * Constructor. |
|
32 * |
|
33 * @param string $prefix Prefix for the XPath expression. |
|
34 * @param string $path Actual path of the expression. |
|
35 * @param string $element The element in the expression. |
|
36 * @param string $condition A condition for the expression. |
|
37 * @param Boolean $starPrefix Indicates whether to use a star prefix. |
|
38 */ |
|
39 public function __construct($prefix = null, $path = null, $element = '*', $condition = null, $starPrefix = false) |
|
40 { |
|
41 $this->prefix = $prefix; |
|
42 $this->path = $path; |
|
43 $this->element = $element; |
|
44 $this->condition = $condition; |
|
45 $this->starPrefix = $starPrefix; |
|
46 } |
|
47 |
|
48 /** |
|
49 * Gets the prefix of this XPath expression. |
|
50 * |
|
51 * @return string |
|
52 */ |
|
53 public function getPrefix() |
|
54 { |
|
55 return $this->prefix; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Gets the path of this XPath expression. |
|
60 * |
|
61 * @return string |
|
62 */ |
|
63 public function getPath() |
|
64 { |
|
65 return $this->path; |
|
66 } |
|
67 |
|
68 /** |
|
69 * Answers whether this XPath expression has a star prefix. |
|
70 * |
|
71 * @return Boolean |
|
72 */ |
|
73 public function hasStarPrefix() |
|
74 { |
|
75 return $this->starPrefix; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Gets the element of this XPath expression. |
|
80 * |
|
81 * @return string |
|
82 */ |
|
83 public function getElement() |
|
84 { |
|
85 return $this->element; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Gets the condition of this XPath expression. |
|
90 * |
|
91 * @return string |
|
92 */ |
|
93 public function getCondition() |
|
94 { |
|
95 return $this->condition; |
|
96 } |
|
97 |
|
98 /** |
|
99 * Gets a string representation for this XPath expression. |
|
100 * |
|
101 * @return string |
|
102 */ |
|
103 public function __toString() |
|
104 { |
|
105 $path = ''; |
|
106 if (null !== $this->prefix) { |
|
107 $path .= $this->prefix; |
|
108 } |
|
109 |
|
110 if (null !== $this->path) { |
|
111 $path .= $this->path; |
|
112 } |
|
113 |
|
114 $path .= $this->element; |
|
115 |
|
116 if ($this->condition) { |
|
117 $path .= sprintf('[%s]', $this->condition); |
|
118 } |
|
119 |
|
120 return $path; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Adds a condition to this XPath expression. |
|
125 * Any pre-existent condition will be ANDed to it. |
|
126 * |
|
127 * @param string $condition The condition to add. |
|
128 */ |
|
129 public function addCondition($condition) |
|
130 { |
|
131 if ($this->condition) { |
|
132 $this->condition = sprintf('%s and (%s)', $this->condition, $condition); |
|
133 } else { |
|
134 $this->condition = $condition; |
|
135 } |
|
136 } |
|
137 |
|
138 /** |
|
139 * Adds a prefix to this XPath expression. |
|
140 * It will be prepended to any pre-existent prefixes. |
|
141 * |
|
142 * @param string $prefix The prefix to add. |
|
143 */ |
|
144 public function addPrefix($prefix) |
|
145 { |
|
146 if ($this->prefix) { |
|
147 $this->prefix = $prefix.$this->prefix; |
|
148 } else { |
|
149 $this->prefix = $prefix; |
|
150 } |
|
151 } |
|
152 |
|
153 /** |
|
154 * Adds a condition to this XPath expression using the name of the element |
|
155 * as the desired value. |
|
156 * This method resets the element to '*'. |
|
157 */ |
|
158 public function addNameTest() |
|
159 { |
|
160 if ($this->element == '*') { |
|
161 // We weren't doing a test anyway |
|
162 return; |
|
163 } |
|
164 |
|
165 $this->addCondition(sprintf('name() = %s', XPathExpr::xpathLiteral($this->element))); |
|
166 $this->element = '*'; |
|
167 } |
|
168 |
|
169 /** |
|
170 * Adds a star prefix to this XPath expression. |
|
171 * This method will prepend a '*' to the path and set the star prefix flag |
|
172 * to true. |
|
173 */ |
|
174 public function addStarPrefix() |
|
175 { |
|
176 /* |
|
177 Adds a /* prefix if there is no prefix. This is when you need |
|
178 to keep context's constrained to a single parent. |
|
179 */ |
|
180 if ($this->path) { |
|
181 $this->path .= '*/'; |
|
182 } else { |
|
183 $this->path = '*/'; |
|
184 } |
|
185 |
|
186 $this->starPrefix = true; |
|
187 } |
|
188 |
|
189 /** |
|
190 * Joins this XPath expression with $other (another XPath expression) using |
|
191 * $combiner to join them. |
|
192 * |
|
193 * @param string $combiner The combiner string. |
|
194 * @param XPathExpr $other The other XPath expression to combine with |
|
195 * this one. |
|
196 */ |
|
197 public function join($combiner, $other) |
|
198 { |
|
199 $prefix = (string) $this; |
|
200 |
|
201 $prefix .= $combiner; |
|
202 $path = $other->getPrefix().$other->getPath(); |
|
203 |
|
204 /* We don't need a star prefix if we are joining to this other |
|
205 prefix; so we'll get rid of it */ |
|
206 if ($other->hasStarPrefix() && '*/' == $path) { |
|
207 $path = ''; |
|
208 } |
|
209 $this->prefix = $prefix; |
|
210 $this->path = $path; |
|
211 $this->element = $other->getElement(); |
|
212 $this->condition = $other->GetCondition(); |
|
213 } |
|
214 |
|
215 /** |
|
216 * Gets an XPath literal for $s. |
|
217 * |
|
218 * @param mixed $s Can either be a Node\ElementNode or a string. |
|
219 * |
|
220 * @return string |
|
221 */ |
|
222 static public function xpathLiteral($s) |
|
223 { |
|
224 if ($s instanceof Node\ElementNode) { |
|
225 // This is probably a symbol that looks like an expression... |
|
226 $s = $s->formatElement(); |
|
227 } else { |
|
228 $s = (string) $s; |
|
229 } |
|
230 |
|
231 if (false === strpos($s, "'")) { |
|
232 return sprintf("'%s'", $s); |
|
233 } |
|
234 |
|
235 if (false === strpos($s, '"')) { |
|
236 return sprintf('"%s"', $s); |
|
237 } |
|
238 |
|
239 $string = $s; |
|
240 $parts = array(); |
|
241 while (true) { |
|
242 if (false !== $pos = strpos($string, "'")) { |
|
243 $parts[] = sprintf("'%s'", substr($string, 0, $pos)); |
|
244 $parts[] = "\"'\""; |
|
245 $string = substr($string, $pos + 1); |
|
246 } else { |
|
247 $parts[] = "'$string'"; |
|
248 break; |
|
249 } |
|
250 } |
|
251 |
|
252 return sprintf('concat(%s)', implode($parts, ', ')); |
|
253 } |
|
254 } |