-
Notifications
You must be signed in to change notification settings - Fork 25
/
pQuery.php
279 lines (236 loc) · 6.93 KB
/
pQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* @author Niels A.D.
* @author Todd Burry <todd@vanillaforums.com>
* @copyright 2010 Niels A.D., 2014 Todd Burry
* @license http://opensource.org/licenses/LGPL-2.1 LGPL-2.1
* @package pQuery
*/
use pQuery\IQuery;
/**
* A jQuery-like object for php.
*/
class pQuery implements ArrayAccess, IteratorAggregate, IQuery {
/// Properties ///
/**
* @var IQuery[]
*/
protected $nodes = array();
/// Methods ///
public function __construct($nodes = array()) {
$this->nodes = $nodes;
}
public function addClass($classname) {
foreach ($this->nodes as $node) {
$node->addClass($classname);
}
return $this;
}
public function after($content) {
foreach ($this->nodes as $node) {
$node->after($content);
}
return $this;
}
public function append($content) {
foreach ($this->nodes as $node) {
$node->append($content);
}
return $this;
}
public function attr($name, $value = null) {
if (empty($this->nodes) && $value === null)
return '';
foreach ($this->nodes as $node) {
if ($value === null)
return $node->attr($name);
$node->attr($name, $value);
}
return $this;
}
public function before($content) {
foreach ($this->nodes as $node) {
$node->before($content);
}
return $this;
}
public function clear() {
foreach ($this->nodes as $node) {
$node->clear();
}
return $this;
}
/**
* Get the count of matched elements.
*
* @return int Returns the count of matched elements.
*/
public function count() {
return count($this->nodes);
}
/**
* Format/beautify a DOM.
*
* @param pQuery\DomNode $dom The dom to format.
* @param array $options Extra formatting options. See {@link pQuery\HtmlFormatter::$options}.
* @return bool Returns `true` on sucess and `false` on failure.
*/
// public static function format($dom, $options = array()) {
// $formatter = new pQuery\HtmlFormatter($options);
// return $formatter->format($dom);
// }
public function getIterator() {
return new ArrayIterator($this->nodes);
}
public function hasClass($classname) {
foreach ($this->nodes as $node) {
if ($node->hasClass($classname))
return true;
}
return false;
}
public function html($value = null) {
if (empty($this->nodes) && $value === null)
return '';
foreach ($this->nodes as $node) {
if ($value === null)
return $node->html();
$node->html($value);
}
return $this;
}
public function offsetExists($offset) {
return isset($this->nodes[$offset]);
}
public function offsetGet($offset) {
return isset($this->nodes[$offset]) ? $this->nodes[$offset] : null;
}
public function offsetSet($offset, $value) {
if (is_null($offset) || !isset($this->nodes[$offset])) {
throw new \BadMethodCallException("You are not allowed to add new nodes to the pQuery object.");
} else {
$this->nodes[$offset]->replaceWith($value);
}
}
public function offsetUnset($offset) {
if (isset($this->nodes[$offset])) {
$this->nodes[$offset]->remove();
unset($this->nodes[$offset]);
}
}
/**
* Query a file or url.
*
* @param string $path The path to the url.
* @param resource $context A context suitable to be passed into {@link file_get_contents}
* @return pQuery\DomNode Returns the root dom node for the html file.
*/
public static function parseFile($path, $context = null) {
$html_str = file_get_contents($path, false, $context);
return static::parseStr($html_str);
}
/**
* Query a string of html.
*
* @param string $html
* @return pQuery\DomNode Returns the root dom node for the html string.
*/
public static function parseStr($html) {
$parser = new pQuery\Html5Parser($html);
return $parser->root;
}
public function prepend($content = null) {
foreach ($this->nodes as $node) {
$node->prepend($content);
}
return $this;
}
public function prop($name, $value = null) {
if (empty($this->nodes) && $value === null)
return '';
foreach ($this->nodes as $node) {
if ($value === null)
return $node->prop($name);
$node->prop($name, $value);
}
return $this;
}
public function remove($selector = null) {
foreach ($this->nodes as $node) {
$node->remove($selector);
}
if ($selector === null)
$this->nodes = array();
return $this;
}
public function removeAttr($name) {
foreach ($this->nodes as $node) {
$node->removeAttr($name);
}
return $this;
}
public function removeClass($classname) {
foreach ($this->nodes as $node) {
$node->removeClass($classname);
}
return $this;
}
public function replaceWith($content) {
foreach ($this->nodes as &$node) {
$node = $node->replaceWith($content);
}
return $this;
}
public function tagName($value = null) {
foreach ($this->nodes as $node) {
if ($value === null)
return $node->tagName();
$node->tagName($value);
}
return $this;
}
public function text($value = null) {
if (empty($this->nodes) && $value === null)
return '';
foreach ($this->nodes as $node) {
if ($value === null)
return $node->text();
$node->text($value);
}
return $this;
}
public function toggleClass($classname, $switch = null) {
foreach ($this->nodes as $node) {
$node->toggleClass($classname, $switch);
}
return $this;
}
public function unwrap() {
foreach ($this->nodes as $node) {
$node->unwrap();
}
return $this;
}
public function val($value = null) {
if (empty($this->nodes) && $value === null)
return '';
foreach ($this->nodes as $node) {
if ($value === null)
return $node->val();
$node->val($value);
}
return $this;
}
public function wrap($wrapping_element) {
foreach ($this->nodes as $node) {
$node->wrap($wrapping_element);
}
return $this;
}
public function wrapInner($wrapping_element) {
foreach ($this->nodes as $node) {
$node->wrapInner($wrapping_element);
}
return $this;
}
}