1
|
|
|
<?php |
2
|
|
|
namespace Wa72\HtmlPageDom; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Extends \Symfony\Component\DomCrawler\Crawler by adding tree manipulation functions |
8
|
|
|
* for HTML documents inspired by jQuery such as html(), css(), append(), prepend(), before(), |
9
|
|
|
* addClass(), removeClass() |
10
|
|
|
* |
11
|
|
|
* @author Christoph Singer |
12
|
|
|
* @license MIT |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class HtmlPageCrawler extends Crawler |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* the (internal) root element name used when importing html fragments |
19
|
|
|
* */ |
20
|
|
|
const FRAGMENT_ROOT_TAGNAME = '_root'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get an HtmlPageCrawler object from a HTML string, DOMNode, DOMNodeList or HtmlPageCrawler |
24
|
|
|
* |
25
|
|
|
* This is the equivalent to jQuery's $() function when used for wrapping DOMNodes or creating DOMElements from HTML code. |
26
|
|
|
* |
27
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|array $content |
28
|
|
|
* @return HtmlPageCrawler |
29
|
|
|
* @api |
30
|
|
|
*/ |
31
|
11 |
|
public static function create($content) |
32
|
|
|
{ |
33
|
11 |
|
if ($content instanceof HtmlPageCrawler) { |
34
|
3 |
|
return $content; |
35
|
|
|
} else { |
36
|
11 |
|
return new HtmlPageCrawler($content); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adds the specified class(es) to each element in the set of matched elements. |
42
|
|
|
* |
43
|
|
|
* @param string $name One or more space-separated classes to be added to the class attribute of each matched element. |
44
|
|
|
* @return HtmlPageCrawler $this for chaining |
45
|
|
|
* @api |
46
|
|
|
*/ |
47
|
1 |
|
public function addClass($name) |
48
|
|
|
{ |
49
|
1 |
|
foreach ($this as $node) { |
50
|
1 |
|
if ($node instanceof \DOMElement) { |
51
|
|
|
/** @var \DOMElement $node */ |
52
|
1 |
|
$classes = preg_split('/\s+/s', $node->getAttribute('class')); |
53
|
1 |
|
$found = false; |
54
|
1 |
|
$count = count($classes); |
55
|
1 |
|
for ($i = 0; $i < $count; $i++) { |
56
|
1 |
|
if ($classes[$i] == $name) { |
57
|
|
|
$found = true; |
58
|
|
|
} |
59
|
|
|
} |
60
|
1 |
|
if (!$found) { |
61
|
1 |
|
$classes[] = $name; |
62
|
1 |
|
$node->setAttribute('class', trim(join(' ', $classes))); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Insert content, specified by the parameter, after each element in the set of matched elements. |
71
|
|
|
* |
72
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
73
|
|
|
* @return HtmlPageCrawler $this for chaining |
74
|
|
|
* @api |
75
|
|
|
*/ |
76
|
3 |
|
public function after($content) |
77
|
|
|
{ |
78
|
3 |
|
$content = self::create($content); |
79
|
3 |
|
$newnodes = array(); |
80
|
3 |
|
foreach ($this as $i => $node) { |
81
|
|
|
/** @var \DOMNode $node */ |
82
|
3 |
|
$refnode = $node->nextSibling; |
83
|
3 |
|
foreach ($content as $newnode) { |
84
|
|
|
/** @var \DOMNode $newnode */ |
85
|
3 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
86
|
3 |
|
if ($refnode === null) { |
87
|
3 |
|
$node->parentNode->appendChild($newnode); |
88
|
|
|
} else { |
89
|
|
|
$node->parentNode->insertBefore($newnode, $refnode); |
90
|
|
|
} |
91
|
3 |
|
$newnodes[] = $newnode; |
92
|
|
|
} |
93
|
|
|
} |
94
|
3 |
|
$content->clear(); |
95
|
3 |
|
$content->add($newnodes); |
96
|
3 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Insert HTML content as child nodes of each element after existing children |
101
|
|
|
* |
102
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment or DOMNode to append |
103
|
|
|
* @return HtmlPageCrawler $this for chaining |
104
|
|
|
* @api |
105
|
|
|
*/ |
106
|
2 |
|
public function append($content) |
107
|
|
|
{ |
108
|
2 |
|
$content = self::create($content); |
109
|
2 |
|
$newnodes = array(); |
110
|
2 |
|
foreach ($this as $i => $node) { |
111
|
|
|
/** @var \DOMNode $node */ |
112
|
2 |
|
foreach ($content as $newnode) { |
113
|
|
|
/** @var \DOMNode $newnode */ |
114
|
2 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
115
|
|
|
// if ($newnode->ownerDocument !== $node->ownerDocument) { |
116
|
|
|
// $newnode = $node->ownerDocument->importNode($newnode, true); |
117
|
|
|
// } else { |
118
|
|
|
// if ($i > 0) { |
119
|
|
|
// $newnode = $newnode->cloneNode(true); |
120
|
|
|
// } |
121
|
|
|
// } |
122
|
2 |
|
$node->appendChild($newnode); |
123
|
2 |
|
$newnodes[] = $newnode; |
124
|
|
|
} |
125
|
|
|
} |
126
|
2 |
|
$content->clear(); |
127
|
2 |
|
$content->add($newnodes); |
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Insert every element in the set of matched elements to the end of the target. |
133
|
|
|
* |
134
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
135
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
136
|
|
|
* @api |
137
|
|
|
*/ |
138
|
1 |
|
public function appendTo($element) |
139
|
|
|
{ |
140
|
1 |
|
$e = self::create($element); |
141
|
1 |
|
$newnodes = array(); |
142
|
1 |
|
foreach ($e as $i => $node) { |
143
|
|
|
/** @var \DOMNode $node */ |
144
|
1 |
|
foreach ($this as $newnode) { |
145
|
|
|
/** @var \DOMNode $newnode */ |
146
|
1 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
147
|
1 |
|
$node->appendChild($newnode); |
148
|
1 |
|
$newnodes[] = $newnode; |
149
|
|
|
} |
150
|
|
|
} |
151
|
1 |
|
return self::create($newnodes); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns the attribute value of the first node of the list, or sets an attribute on each element |
156
|
|
|
* |
157
|
|
|
* @see HtmlPageCrawler::getAttribute() |
158
|
|
|
* @see HtmlPageCrawler::setAttribute |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @param null|string $value |
162
|
|
|
* @return null|string|HtmlPageCrawler |
163
|
|
|
* @api |
164
|
|
|
*/ |
165
|
1 |
|
public function attr($name, $value = null) |
166
|
|
|
{ |
167
|
1 |
|
if ($value === null) { |
168
|
1 |
|
return $this->getAttribute($name); |
169
|
|
|
} else { |
170
|
1 |
|
return $this->setAttribute($name, $value); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Sets an attribute on each element |
176
|
|
|
* |
177
|
|
|
* @param string $name |
178
|
|
|
* @param string $value |
179
|
|
|
* @return HtmlPageCrawler $this for chaining |
180
|
|
|
*/ |
181
|
2 |
|
public function setAttribute($name, $value) |
182
|
|
|
{ |
183
|
2 |
|
foreach ($this as $node) { |
184
|
2 |
|
if ($node instanceof \DOMElement) { |
185
|
|
|
/** @var \DOMElement $node */ |
186
|
2 |
|
$node->setAttribute($name, $value); |
187
|
|
|
} |
188
|
|
|
} |
189
|
2 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns the attribute value of the first node of the list. |
194
|
|
|
* |
195
|
|
|
* @param string $name The attribute name |
196
|
|
|
* @return string|null The attribute value or null if the attribute does not exist |
197
|
|
|
* @throws \InvalidArgumentException When current node is empty |
198
|
|
|
* |
199
|
|
|
*/ |
200
|
1 |
|
public function getAttribute($name) |
201
|
|
|
{ |
202
|
1 |
|
if (!count($this)) { |
203
|
|
|
throw new \InvalidArgumentException('The current node list is empty.'); |
204
|
|
|
} |
205
|
1 |
|
$node = $this->getNode(0); |
206
|
1 |
|
return $node->hasAttribute($name) ? $node->getAttribute($name) : null; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Insert content, specified by the parameter, before each element in the set of matched elements. |
211
|
|
|
* |
212
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
213
|
|
|
* @return HtmlPageCrawler $this for chaining |
214
|
|
|
* @api |
215
|
|
|
*/ |
216
|
2 |
|
public function before($content) |
217
|
|
|
{ |
218
|
2 |
|
$content = self::create($content); |
219
|
2 |
|
$newnodes = array(); |
220
|
2 |
|
foreach ($this as $i => $node) { |
221
|
|
|
/** @var \DOMNode $node */ |
222
|
2 |
|
foreach ($content as $newnode) { |
223
|
|
|
/** @var \DOMNode $newnode */ |
224
|
2 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
225
|
2 |
|
$node->parentNode->insertBefore($newnode, $node); |
226
|
2 |
|
$newnodes[] = $newnode; |
227
|
|
|
} |
228
|
|
|
} |
229
|
2 |
|
$content->clear(); |
230
|
2 |
|
$content->add($newnodes); |
231
|
2 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Create a deep copy of the set of matched elements. |
236
|
|
|
* |
237
|
|
|
* Equivalent to clone() in jQuery (clone is not a valid PHP function name) |
238
|
|
|
* |
239
|
|
|
* @return HtmlPageCrawler |
240
|
|
|
* @api |
241
|
|
|
*/ |
242
|
1 |
|
public function makeClone() |
243
|
|
|
{ |
244
|
1 |
|
return clone $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
1 |
|
public function __clone() |
248
|
|
|
{ |
249
|
1 |
|
$newnodes = array(); |
250
|
1 |
|
foreach ($this as $node) { |
251
|
|
|
/** @var \DOMNode $node */ |
252
|
1 |
|
$newnodes[] = $node->cloneNode(true); |
253
|
|
|
} |
254
|
1 |
|
$this->clear(); |
255
|
1 |
|
$this->add($newnodes); |
256
|
1 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get one CSS style property of the first element or set it for all elements in the list |
260
|
|
|
* |
261
|
|
|
* Function is here for compatibility with jQuery; it is the same as getStyle() and setStyle() |
262
|
|
|
* |
263
|
|
|
* @see HtmlPageCrawler::getStyle() |
264
|
|
|
* @see HtmlPageCrawler::setStyle() |
265
|
|
|
* |
266
|
|
|
* @param string $key The name of the style property |
267
|
|
|
* @param null|string $value The CSS value to set, or NULL to get the current value |
268
|
|
|
* @return HtmlPageCrawler|string If no param is provided, returns the CSS styles of the first element |
269
|
|
|
* @api |
270
|
|
|
*/ |
271
|
1 |
|
public function css($key, $value = null) |
272
|
|
|
{ |
273
|
1 |
|
if (null === $value) { |
274
|
1 |
|
return $this->getStyle($key); |
275
|
|
|
} else { |
276
|
1 |
|
return $this->setStyle($key, $value); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* get one CSS style property of the first element |
282
|
|
|
* |
283
|
|
|
* @param string $key name of the property |
284
|
|
|
* @return string|null value of the property |
285
|
|
|
*/ |
286
|
1 |
|
public function getStyle($key) |
287
|
|
|
{ |
288
|
1 |
|
$styles = Helpers::cssStringToArray($this->getAttribute('style')); |
289
|
1 |
|
return (isset($styles[$key]) ? $styles[$key] : null); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* set one CSS style property for all elements in the list |
294
|
|
|
* |
295
|
|
|
* @param string $key name of the property |
296
|
|
|
* @param string $value value of the property |
297
|
|
|
* @return HtmlPageCrawler $this for chaining |
298
|
|
|
*/ |
299
|
1 |
|
public function setStyle($key, $value) |
300
|
|
|
{ |
301
|
1 |
|
foreach ($this as $node) { |
302
|
1 |
|
if ($node instanceof \DOMElement) { |
303
|
|
|
/** @var \DOMElement $node */ |
304
|
1 |
|
$styles = Helpers::cssStringToArray($node->getAttribute('style')); |
305
|
1 |
|
if ($value != '') { |
306
|
1 |
|
$styles[$key] = $value; |
307
|
1 |
|
} elseif (isset($styles[$key])) { |
308
|
1 |
|
unset($styles[$key]); |
309
|
|
|
} |
310
|
1 |
|
$node->setAttribute('style', Helpers::cssArrayToString($styles)); |
311
|
|
|
} |
312
|
|
|
} |
313
|
1 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Removes all child nodes and text from all nodes in set |
318
|
|
|
* |
319
|
|
|
* Equivalent to jQuery's empty() function which is not a valid function name in PHP |
320
|
|
|
* @return HtmlPageCrawler $this |
321
|
|
|
* @api |
322
|
|
|
*/ |
323
|
|
|
public function makeEmpty() |
324
|
|
|
{ |
325
|
|
|
foreach ($this as $node) { |
326
|
|
|
$node->nodeValue = ''; |
327
|
|
|
} |
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Determine whether any of the matched elements are assigned the given class. |
333
|
|
|
* |
334
|
|
|
* @param string $name |
335
|
|
|
* @return bool |
336
|
|
|
* @api |
337
|
|
|
*/ |
338
|
2 |
|
public function hasClass($name) |
339
|
|
|
{ |
340
|
2 |
|
foreach ($this as $node) { |
341
|
2 |
|
if ($node instanceof \DOMElement && $class = $node->getAttribute('class')) { |
342
|
2 |
|
$classes = preg_split('/\s+/s', $class); |
343
|
2 |
|
if (in_array($name, $classes)) { |
344
|
2 |
|
return true; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
348
|
2 |
|
return false; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Get the HTML contents of the first element in the set of matched elements |
353
|
|
|
* or set the HTML contents of every matched element. |
354
|
|
|
* |
355
|
|
|
* Function is here for compatibility with jQuery: When called with a parameter, it is |
356
|
|
|
* equivalent to setInnerHtml(), without parameter it is the same as getInnerHtml() |
357
|
|
|
* |
358
|
|
|
* @see HtmlPageCrawler::setInnerHtml() |
359
|
|
|
* @see HtmlPageCrawler::getInnerHtml() |
360
|
|
|
* |
361
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList|null $html The HTML content to set, or NULL to get the current content |
362
|
|
|
* |
363
|
|
|
* @return HtmlPageCrawler|string If no param is provided, returns the HTML content of the first element |
364
|
|
|
* @api |
365
|
|
|
*/ |
366
|
|
|
public function html($html = null) |
367
|
|
|
{ |
368
|
|
|
if (null === $html) { |
369
|
|
|
return $this->getInnerHtml(); |
370
|
|
|
} else { |
371
|
|
|
$this->setInnerHtml($html); |
372
|
|
|
return $this; |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Get the innerHTML contents of the first element |
378
|
|
|
* |
379
|
|
|
* @return string HTML code fragment |
380
|
|
|
*/ |
381
|
2 |
|
public function getInnerHtml() |
382
|
|
|
{ |
383
|
2 |
|
return parent::html(); |
|
|
|
|
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Set the HTML contents of each element |
388
|
|
|
* |
389
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
390
|
|
|
* @return HtmlPageCrawler $this for chaining |
391
|
|
|
*/ |
392
|
1 |
|
public function setInnerHtml($content) |
393
|
|
|
{ |
394
|
1 |
|
$content = self::create($content); |
395
|
1 |
|
foreach ($this as $node) { |
396
|
1 |
|
$node->nodeValue = ''; |
397
|
1 |
|
foreach ($content as $newnode) { |
398
|
|
|
/** @var \DOMNode $node */ |
399
|
|
|
/** @var \DOMNode $newnode */ |
400
|
1 |
|
$newnode = static::importNewnode($newnode, $node); |
401
|
1 |
|
$node->appendChild($newnode); |
402
|
|
|
} |
403
|
|
|
} |
404
|
1 |
|
return $this; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Insert every element in the set of matched elements after the target. |
409
|
|
|
* |
410
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
411
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
412
|
|
|
* @api |
413
|
|
|
*/ |
414
|
1 |
|
public function insertAfter($element) |
415
|
|
|
{ |
416
|
1 |
|
$e = self::create($element); |
417
|
1 |
|
$newnodes = array(); |
418
|
1 |
|
foreach ($e as $i => $node) { |
419
|
|
|
/** @var \DOMNode $node */ |
420
|
1 |
|
$refnode = $node->nextSibling; |
421
|
1 |
|
foreach ($this as $newnode) { |
422
|
|
|
/** @var \DOMNode $newnode */ |
423
|
1 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
424
|
1 |
|
if ($refnode === null) { |
425
|
1 |
|
$node->parentNode->appendChild($newnode); |
426
|
|
|
} else { |
427
|
1 |
|
$node->parentNode->insertBefore($newnode, $refnode); |
428
|
|
|
} |
429
|
1 |
|
$newnodes[] = $newnode; |
430
|
|
|
} |
431
|
|
|
} |
432
|
1 |
|
return self::create($newnodes); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Insert every element in the set of matched elements before the target. |
437
|
|
|
* |
438
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
439
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
440
|
|
|
* @api |
441
|
|
|
*/ |
442
|
1 |
|
public function insertBefore($element) |
443
|
|
|
{ |
444
|
1 |
|
$e = self::create($element); |
445
|
1 |
|
$newnodes = array(); |
446
|
1 |
|
foreach ($e as $i => $node) { |
447
|
|
|
/** @var \DOMNode $node */ |
448
|
1 |
|
foreach ($this as $newnode) { |
449
|
|
|
/** @var \DOMNode $newnode */ |
450
|
1 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
451
|
1 |
|
$node->parentNode->insertBefore($newnode, $node); |
452
|
1 |
|
$newnodes[] = $newnode; |
453
|
|
|
} |
454
|
|
|
} |
455
|
1 |
|
return self::create($newnodes); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. |
460
|
|
|
* |
461
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content HTML code fragment |
462
|
|
|
* @return HtmlPageCrawler $this for chaining |
463
|
|
|
* @api |
464
|
|
|
*/ |
465
|
1 |
|
public function prepend($content) |
466
|
|
|
{ |
467
|
1 |
|
$content = self::create($content); |
468
|
1 |
|
$newnodes = array(); |
469
|
1 |
|
foreach ($this as $i => $node) { |
470
|
1 |
|
$refnode = $node->firstChild; |
471
|
|
|
/** @var \DOMNode $node */ |
472
|
1 |
|
foreach ($content as $newnode) { |
473
|
|
|
/** @var \DOMNode $newnode */ |
474
|
1 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
475
|
1 |
|
if ($refnode === null) { |
476
|
|
|
$node->appendChild($newnode); |
477
|
|
|
} else { |
478
|
1 |
|
$node->insertBefore($newnode, $refnode); |
479
|
|
|
} |
480
|
1 |
|
$newnodes[] = $newnode; |
481
|
|
|
} |
482
|
|
|
} |
483
|
1 |
|
$content->clear(); |
484
|
1 |
|
$content->add($newnodes); |
485
|
1 |
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Insert every element in the set of matched elements to the beginning of the target. |
490
|
|
|
* |
491
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
492
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements prepended to the target elements |
493
|
|
|
* @api |
494
|
|
|
*/ |
495
|
|
|
public function prependTo($element) |
496
|
|
|
{ |
497
|
|
|
$e = self::create($element); |
498
|
|
|
$newnodes = array(); |
499
|
|
|
foreach ($e as $i => $node) { |
500
|
|
|
$refnode = $node->firstChild; |
501
|
|
|
/** @var \DOMNode $node */ |
502
|
|
|
foreach ($this as $newnode) { |
503
|
|
|
/** @var \DOMNode $newnode */ |
504
|
|
|
$newnode = static::importNewnode($newnode, $node, $i); |
505
|
|
|
if ($refnode === null) { |
506
|
|
|
$node->appendChild($newnode); |
507
|
|
|
} else { |
508
|
|
|
$node->insertBefore($newnode, $refnode); |
509
|
|
|
} |
510
|
|
|
$newnodes[] = $newnode; |
511
|
|
|
} |
512
|
|
|
} |
513
|
|
|
return self::create($newnodes); |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
/** |
517
|
|
|
* Remove the set of matched elements from the DOM. |
518
|
|
|
* |
519
|
|
|
* (as opposed to Crawler::clear() which detaches the nodes only from Crawler |
520
|
|
|
* but leaves them in the DOM) |
521
|
|
|
* |
522
|
|
|
* @api |
523
|
|
|
*/ |
524
|
2 |
|
public function remove() |
525
|
|
|
{ |
526
|
2 |
|
foreach ($this as $node) { |
527
|
|
|
/** |
528
|
|
|
* @var \DOMNode $node |
529
|
|
|
*/ |
530
|
2 |
|
if ($node->parentNode instanceof \DOMElement) { |
531
|
2 |
|
$node->parentNode->removeChild($node); |
532
|
|
|
} |
533
|
|
|
} |
534
|
2 |
|
$this->clear(); |
535
|
2 |
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* Remove an attribute from each element in the set of matched elements. |
539
|
|
|
* |
540
|
|
|
* Alias for removeAttribute for compatibility with jQuery |
541
|
|
|
* |
542
|
|
|
* @param string $name |
543
|
|
|
* @return HtmlPageCrawler |
544
|
|
|
* @api |
545
|
|
|
*/ |
546
|
1 |
|
public function removeAttr($name) |
547
|
|
|
{ |
548
|
1 |
|
return $this->removeAttribute($name); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Remove an attribute from each element in the set of matched elements. |
553
|
|
|
* |
554
|
|
|
* @param string $name |
555
|
|
|
* @return HtmlPageCrawler |
556
|
|
|
*/ |
557
|
1 |
|
public function removeAttribute($name) |
558
|
|
|
{ |
559
|
1 |
|
foreach ($this as $node) { |
560
|
1 |
|
if ($node instanceof \DOMElement) { |
561
|
|
|
/** @var \DOMElement $node */ |
562
|
1 |
|
if ($node->hasAttribute($name)) { |
563
|
1 |
|
$node->removeAttribute($name); |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
} |
567
|
1 |
|
return $this; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Remove a class from each element in the list |
572
|
|
|
* |
573
|
|
|
* @param string $name |
574
|
|
|
* @return HtmlPageCrawler $this for chaining |
575
|
|
|
* @api |
576
|
|
|
*/ |
577
|
2 |
|
public function removeClass($name) |
578
|
|
|
{ |
579
|
2 |
|
foreach ($this as $node) { |
580
|
2 |
|
if ($node instanceof \DOMElement) { |
581
|
|
|
/** @var \DOMElement $node */ |
582
|
2 |
|
$classes = preg_split('/\s+/s', $node->getAttribute('class')); |
583
|
2 |
|
$count = count($classes); |
584
|
2 |
|
for ($i = 0; $i < $count; $i++) { |
585
|
2 |
|
if ($classes[$i] == $name) { |
586
|
2 |
|
unset($classes[$i]); |
587
|
|
|
} |
588
|
|
|
} |
589
|
2 |
|
$node->setAttribute('class', trim(join(' ', $classes))); |
590
|
|
|
} |
591
|
|
|
} |
592
|
2 |
|
return $this; |
593
|
|
|
} |
594
|
|
|
|
595
|
|
|
/** |
596
|
|
|
* Replace each target element with the set of matched elements. |
597
|
|
|
* |
598
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $element |
599
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler A new Crawler object containing all elements appended to the target elements |
600
|
|
|
* @api |
601
|
|
|
*/ |
602
|
2 |
|
public function replaceAll($element) |
603
|
|
|
{ |
604
|
2 |
|
$e = self::create($element); |
605
|
2 |
|
$newnodes = array(); |
606
|
2 |
|
foreach ($e as $i => $node) { |
607
|
|
|
/** @var \DOMNode $node */ |
608
|
2 |
|
$parent = $node->parentNode; |
609
|
2 |
|
$refnode = $node->nextSibling; |
610
|
2 |
|
foreach ($this as $j => $newnode) { |
611
|
|
|
/** @var \DOMNode $newnode */ |
612
|
2 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
613
|
2 |
|
if ($j == 0) { |
614
|
2 |
|
$parent->replaceChild($newnode, $node); |
615
|
|
|
} else { |
616
|
1 |
|
$parent->insertBefore($newnode, $refnode); |
617
|
|
|
} |
618
|
2 |
|
$newnodes[] = $newnode; |
619
|
|
|
} |
620
|
|
|
} |
621
|
2 |
|
return self::create($newnodes); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
/** |
625
|
|
|
* Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed. |
626
|
|
|
* |
627
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
628
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
629
|
|
|
* @api |
630
|
|
|
*/ |
631
|
2 |
|
public function replaceWith($content) |
632
|
|
|
{ |
633
|
2 |
|
$content = self::create($content); |
634
|
2 |
|
$newnodes = array(); |
635
|
2 |
|
foreach ($this as $i => $node) { |
636
|
|
|
/** @var \DOMNode $node */ |
637
|
2 |
|
$parent = $node->parentNode; |
638
|
2 |
|
$refnode = $node->nextSibling; |
639
|
2 |
|
foreach ($content as $j => $newnode) { |
640
|
|
|
/** @var \DOMNode $newnode */ |
641
|
2 |
|
$newnode = static::importNewnode($newnode, $node, $i); |
642
|
2 |
|
if ($j == 0) { |
643
|
2 |
|
$parent->replaceChild($newnode, $node); |
644
|
|
|
} else { |
645
|
1 |
|
$parent->insertBefore($newnode, $refnode); |
646
|
|
|
} |
647
|
2 |
|
$newnodes[] = $newnode; |
648
|
|
|
} |
649
|
|
|
} |
650
|
2 |
|
$content->clear(); |
651
|
2 |
|
$content->add($newnodes); |
652
|
2 |
|
return $this; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Get the combined text contents of each element in the set of matched elements, including their descendants, |
657
|
|
|
* or set the text contents of the matched elements. |
658
|
|
|
* |
659
|
|
|
* ATTENTION: Contrary to the parent Crawler class, which returns the text from the first element only, |
660
|
|
|
* this functions returns the combined text of all elements (as jQuery does). If this is not what you need you |
661
|
|
|
* must call ->first() before calling ->text(), e.g. |
662
|
|
|
* |
663
|
|
|
* in Symfony\DOMCrawler\Crawler: $c->filter('p')->text() returns the text of the first paragraph only |
664
|
|
|
* in HtmlPageCrawler you need to call: $c->filter('p')->first()->text() |
665
|
|
|
* |
666
|
|
|
* @param null|string $text |
667
|
|
|
* @return string|HtmlPageCrawler |
668
|
|
|
* @api |
669
|
|
|
*/ |
670
|
1 |
|
public function text($text = null) |
671
|
|
|
{ |
672
|
1 |
|
if ($text === null) { |
673
|
1 |
|
$text = ''; |
674
|
1 |
|
foreach ($this as $node) { |
675
|
|
|
/** @var \DOMNode $node */ |
676
|
1 |
|
$text .= $node->nodeValue; |
677
|
|
|
} |
678
|
1 |
|
return $text; |
679
|
|
|
} else { |
680
|
1 |
|
foreach ($this as $node) { |
681
|
|
|
/** @var \DOMNode $node */ |
682
|
1 |
|
$node->nodeValue = $text; |
683
|
|
|
} |
684
|
1 |
|
return $this; |
685
|
|
|
} |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Add or remove one or more classes from each element in the set of matched elements, depending the class’s presence. |
691
|
|
|
* |
692
|
|
|
* @param string $classname One or more classnames separated by spaces |
693
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
694
|
|
|
* @api |
695
|
|
|
*/ |
696
|
1 |
|
public function toggleClass($classname) |
697
|
|
|
{ |
698
|
1 |
|
$classes = explode(' ', $classname); |
699
|
1 |
|
foreach ($this as $i => $node) { |
700
|
1 |
|
$c = self::create($node); |
701
|
|
|
/** @var \DOMNode $node */ |
702
|
1 |
|
foreach ($classes as $class) { |
703
|
1 |
|
if ($c->hasClass($class)) { |
704
|
1 |
|
$c->removeClass($class); |
705
|
|
|
} else { |
706
|
1 |
|
$c->addClass($class); |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
} |
710
|
1 |
|
return $this; |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place. |
715
|
|
|
* |
716
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
717
|
|
|
* @api |
718
|
|
|
*/ |
719
|
1 |
|
public function unwrap() |
720
|
|
|
{ |
721
|
1 |
|
$parents = array(); |
722
|
1 |
|
foreach($this as $i => $node) { |
|
|
|
|
723
|
1 |
|
$parents[] = $node->parentNode; |
724
|
|
|
} |
725
|
|
|
|
726
|
1 |
|
self::create($parents)->unwrapInner(); |
727
|
1 |
|
return $this; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* Remove the matched elements, but promote the children to take their place. |
732
|
|
|
* |
733
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
734
|
|
|
* @api |
735
|
|
|
*/ |
736
|
1 |
|
public function unwrapInner() |
737
|
|
|
{ |
738
|
1 |
|
foreach($this as $i => $node) { |
|
|
|
|
739
|
1 |
|
if (!$node->parentNode instanceof \DOMElement) { |
740
|
|
|
throw new \InvalidArgumentException('DOMElement does not have a parent DOMElement node.'); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** @var \DOMNode[] $children */ |
744
|
1 |
|
$children = iterator_to_array($node->childNodes); |
745
|
1 |
|
foreach ($children as $child) { |
746
|
1 |
|
$node->parentNode->insertBefore($child, $node); |
747
|
|
|
} |
748
|
|
|
|
749
|
1 |
|
$node->parentNode->removeChild($node); |
750
|
|
|
} |
751
|
1 |
|
} |
752
|
|
|
|
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Wrap an HTML structure around each element in the set of matched elements |
756
|
|
|
* |
757
|
|
|
* The HTML structure must contain only one root node, e.g.: |
758
|
|
|
* Works: <div><div></div></div> |
759
|
|
|
* Does not work: <div></div><div></div> |
760
|
|
|
* |
761
|
|
|
* @param string|HtmlPageCrawler|\DOMNode $wrappingElement |
762
|
|
|
* @return HtmlPageCrawler $this for chaining |
763
|
|
|
* @api |
764
|
|
|
*/ |
765
|
1 |
|
public function wrap($wrappingElement) |
766
|
|
|
{ |
767
|
1 |
|
$content = self::create($wrappingElement); |
768
|
1 |
|
$newnodes = array(); |
769
|
1 |
|
foreach ($this as $i => $node) { |
770
|
|
|
/** @var \DOMNode $node */ |
771
|
1 |
|
$newnode = $content->getNode(0); |
772
|
|
|
/** @var \DOMNode $newnode */ |
773
|
|
|
// $newnode = static::importNewnode($newnode, $node, $i); |
774
|
1 |
|
if ($newnode->ownerDocument !== $node->ownerDocument) { |
775
|
1 |
|
$newnode = $node->ownerDocument->importNode($newnode, true); |
776
|
|
|
} else { |
777
|
|
|
if ($i > 0) { |
778
|
|
|
$newnode = $newnode->cloneNode(true); |
779
|
|
|
} |
780
|
|
|
} |
781
|
1 |
|
$oldnode = $node->parentNode->replaceChild($newnode, $node); |
782
|
1 |
|
while ($newnode->hasChildNodes()) { |
783
|
1 |
|
$elementFound = false; |
784
|
1 |
|
foreach ($newnode->childNodes as $child) { |
785
|
1 |
|
if ($child instanceof \DOMElement) { |
786
|
1 |
|
$newnode = $child; |
787
|
1 |
|
$elementFound = true; |
788
|
1 |
|
break; |
789
|
|
|
} |
790
|
|
|
} |
791
|
1 |
|
if (!$elementFound) { |
792
|
|
|
break; |
793
|
|
|
} |
794
|
|
|
} |
795
|
1 |
|
$newnode->appendChild($oldnode); |
796
|
1 |
|
$newnodes[] = $newnode; |
797
|
|
|
} |
798
|
1 |
|
$content->clear(); |
799
|
1 |
|
$content->add($newnodes); |
800
|
1 |
|
return $this; |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
/** |
804
|
|
|
* Wrap an HTML structure around all elements in the set of matched elements. |
805
|
|
|
* |
806
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
807
|
|
|
* @throws \LogicException |
808
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
809
|
|
|
* @api |
810
|
|
|
*/ |
811
|
1 |
|
public function wrapAll($content) |
812
|
|
|
{ |
813
|
1 |
|
$content = self::create($content); |
814
|
1 |
|
$parent = $this->getNode(0)->parentNode; |
815
|
1 |
|
foreach ($this as $i => $node) { |
816
|
|
|
/** @var \DOMNode $node */ |
817
|
1 |
|
if ($node->parentNode !== $parent) { |
818
|
1 |
|
throw new \LogicException('Nodes to be wrapped with wrapAll() must all have the same parent'); |
819
|
|
|
} |
820
|
|
|
} |
821
|
|
|
|
822
|
1 |
|
$newnode = $content->getNode(0); |
823
|
|
|
/** @var \DOMNode $newnode */ |
824
|
1 |
|
$newnode = static::importNewnode($newnode, $parent); |
825
|
|
|
|
826
|
1 |
|
$newnode = $parent->insertBefore($newnode,$this->getNode(0)); |
827
|
1 |
|
$content->clear(); |
828
|
1 |
|
$content->add($newnode); |
829
|
|
|
|
830
|
1 |
|
while ($newnode->hasChildNodes()) { |
831
|
|
|
$elementFound = false; |
832
|
|
|
foreach ($newnode->childNodes as $child) { |
833
|
|
|
if ($child instanceof \DOMElement) { |
834
|
|
|
$newnode = $child; |
835
|
|
|
$elementFound = true; |
836
|
|
|
break; |
837
|
|
|
} |
838
|
|
|
} |
839
|
|
|
if (!$elementFound) { |
840
|
|
|
break; |
841
|
|
|
} |
842
|
|
|
} |
843
|
1 |
|
foreach ($this as $i => $node) { |
844
|
|
|
/** @var \DOMNode $node */ |
845
|
1 |
|
$newnode->appendChild($node); |
846
|
|
|
} |
847
|
1 |
|
return $this; |
848
|
|
|
} |
849
|
|
|
|
850
|
|
|
/** |
851
|
|
|
* Wrap an HTML structure around the content of each element in the set of matched elements. |
852
|
|
|
* |
853
|
|
|
* @param string|HtmlPageCrawler|\DOMNode|\DOMNodeList $content |
854
|
|
|
* @return \Wa72\HtmlPageDom\HtmlPageCrawler $this for chaining |
855
|
|
|
* @api |
856
|
|
|
*/ |
857
|
1 |
|
public function wrapInner($content) |
858
|
|
|
{ |
859
|
1 |
|
foreach ($this as $i => $node) { |
860
|
|
|
/** @var \DOMNode $node */ |
861
|
1 |
|
self::create($node->childNodes)->wrapAll($content); |
862
|
|
|
} |
863
|
1 |
|
return $this; |
864
|
|
|
} |
865
|
|
|
|
866
|
|
|
/** |
867
|
|
|
* Get the HTML code fragment of all elements and their contents. |
868
|
|
|
* |
869
|
|
|
* If the first node contains a complete HTML document return only |
870
|
|
|
* the full code of this document. |
871
|
|
|
* |
872
|
|
|
* @return string HTML code (fragment) |
873
|
|
|
* @api |
874
|
|
|
*/ |
875
|
3 |
|
public function saveHTML() |
876
|
|
|
{ |
877
|
3 |
|
$html = ''; |
878
|
3 |
|
foreach ($this as $node) { |
879
|
3 |
|
$html .= $node->ownerDocument->saveHTML($node); |
880
|
|
|
} |
881
|
3 |
|
return $html; |
882
|
|
|
} |
883
|
|
|
|
884
|
|
|
public function __toString() |
885
|
|
|
{ |
886
|
|
|
return $this->saveHTML(); |
887
|
|
|
} |
888
|
|
|
|
889
|
|
|
/** |
890
|
|
|
* checks whether the first node contains a complete html document |
891
|
|
|
* (as opposed to a document fragment) |
892
|
|
|
* |
893
|
|
|
* @return boolean |
894
|
|
|
*/ |
895
|
1 |
|
public function isHtmlDocument() |
896
|
|
|
{ |
897
|
1 |
|
$node = $this->getNode(0); |
898
|
1 |
|
if ($node instanceof \DOMElement |
899
|
1 |
|
&& $node->ownerDocument instanceof \DOMDocument |
900
|
1 |
|
&& $node->ownerDocument->documentElement === $node |
901
|
1 |
|
&& $node->nodeName == 'html' |
902
|
|
|
) { |
903
|
1 |
|
return true; |
904
|
|
|
} else { |
905
|
1 |
|
return false; |
906
|
|
|
} |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
/** |
910
|
|
|
* get ownerDocument of the first element |
911
|
|
|
* |
912
|
|
|
* @return \DOMDocument|null |
913
|
|
|
*/ |
914
|
|
|
public function getDOMDocument() |
915
|
|
|
{ |
916
|
|
|
$node = $this->getNode(0); |
917
|
|
|
$r = null; |
918
|
|
|
if ($node instanceof \DOMElement |
919
|
|
|
&& $node->ownerDocument instanceof \DOMDocument |
920
|
|
|
) { |
921
|
|
|
$r = $node->ownerDocument; |
922
|
|
|
} |
923
|
|
|
return $r; |
924
|
|
|
} |
925
|
|
|
|
926
|
|
|
/** |
927
|
|
|
* Filters the list of nodes with a CSS selector. |
928
|
|
|
* |
929
|
|
|
* @param string $selector |
930
|
|
|
* @return HtmlPageCrawler |
931
|
|
|
*/ |
932
|
6 |
|
public function filter($selector) |
933
|
|
|
{ |
934
|
6 |
|
return parent::filter($selector); |
935
|
|
|
} |
936
|
|
|
|
937
|
|
|
/** |
938
|
|
|
* Filters the list of nodes with an XPath expression. |
939
|
|
|
* |
940
|
|
|
* @param string $xpath An XPath expression |
941
|
|
|
* |
942
|
|
|
* @return HtmlPageCrawler A new instance of Crawler with the filtered list of nodes |
943
|
|
|
* |
944
|
|
|
* @api |
945
|
|
|
*/ |
946
|
1 |
|
public function filterXPath($xpath) |
947
|
|
|
{ |
948
|
1 |
|
return parent::filterXPath($xpath); |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Adds HTML/XML content to the HtmlPageCrawler object (but not to the DOM of an already attached node). |
953
|
|
|
* |
954
|
|
|
* Function overriden from Crawler because HTML fragments are always added as complete documents there |
955
|
|
|
* |
956
|
|
|
* |
957
|
|
|
* @param string $content A string to parse as HTML/XML |
958
|
|
|
* @param null|string $type The content type of the string |
959
|
|
|
* |
960
|
|
|
* @return null|void |
961
|
|
|
*/ |
962
|
12 |
|
public function addContent($content, $type = null) |
963
|
|
|
{ |
964
|
12 |
|
if (empty($type)) { |
965
|
12 |
|
$type = 'text/html;charset=UTF-8'; |
966
|
|
|
} |
967
|
12 |
|
if (substr($type, 0, 9) == 'text/html' && !preg_match('/<html\b[^>]*>/i', $content)) { |
968
|
|
|
// string contains no <html> Tag => no complete document but an HTML fragment! |
969
|
10 |
|
$this->addHtmlFragment($content); |
970
|
|
|
} else { |
971
|
2 |
|
parent::addContent($content, $type); |
972
|
|
|
} |
973
|
12 |
|
} |
974
|
|
|
|
975
|
10 |
|
public function addHtmlFragment($content, $charset = 'UTF-8') |
976
|
|
|
{ |
977
|
10 |
|
$d = new \DOMDocument('1.0', $charset); |
978
|
10 |
|
$root = $d->appendChild($d->createElement(self::FRAGMENT_ROOT_TAGNAME)); |
979
|
10 |
|
$bodynode = Helpers::getBodyNodeFromHtmlFragment($content, $charset); |
980
|
10 |
|
foreach ($bodynode->childNodes as $child) { |
981
|
10 |
|
$inode = $root->appendChild($d->importNode($child, true)); |
982
|
10 |
|
if ($inode) { |
983
|
10 |
|
$this->addNode($inode); |
984
|
|
|
} |
985
|
|
|
} |
986
|
10 |
|
} |
987
|
|
|
|
988
|
|
|
/** |
989
|
|
|
* returns the first node |
990
|
|
|
* deprecated, use getNode(0) instead |
991
|
|
|
* |
992
|
|
|
* @return \DOMNode|null |
993
|
|
|
* @deprecated |
994
|
|
|
* @see Crawler::getNode |
995
|
|
|
*/ |
996
|
1 |
|
public function getFirstNode() |
997
|
|
|
{ |
998
|
1 |
|
return $this->getNode(0); |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
/** |
1002
|
|
|
* @param int $position |
1003
|
|
|
* |
1004
|
|
|
* overridden from Crawler because it is not public in Symfony 2.3 |
1005
|
|
|
* TODO: throw away as soon as we don't need to support SF 2.3 any more |
1006
|
|
|
* |
1007
|
|
|
* @return \DOMElement|null |
1008
|
|
|
*/ |
1009
|
3 |
|
public function getNode($position) |
1010
|
|
|
{ |
1011
|
3 |
|
return parent::getNode($position); |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
/** |
1015
|
|
|
* Returns the node name of the first node of the list. |
1016
|
|
|
* |
1017
|
|
|
* in Crawler (parent), this function will be available starting with 2.6.0, |
1018
|
|
|
* therefore this method be removed from here as soon as we don't need to keep compatibility |
1019
|
|
|
* with Symfony < 2.6 |
1020
|
|
|
* |
1021
|
|
|
* TODO: throw away as soon as we don't need to support SF 2.3 any more |
1022
|
|
|
* |
1023
|
|
|
* @return string The node name |
1024
|
|
|
* |
1025
|
|
|
* @throws \InvalidArgumentException When current node is empty |
1026
|
|
|
*/ |
1027
|
1 |
|
public function nodeName() |
1028
|
|
|
{ |
1029
|
1 |
|
if (!count($this)) { |
1030
|
|
|
throw new \InvalidArgumentException('The current node list is empty.'); |
1031
|
|
|
} |
1032
|
1 |
|
return $this->getNode(0)->nodeName; |
1033
|
|
|
} |
1034
|
|
|
|
1035
|
|
|
/** |
1036
|
|
|
* Adds a node to the current list of nodes. |
1037
|
|
|
* |
1038
|
|
|
* This method uses the appropriate specialized add*() method based |
1039
|
|
|
* on the type of the argument. |
1040
|
|
|
* |
1041
|
|
|
* Overwritten from parent to allow Crawler to be added |
1042
|
|
|
* |
1043
|
|
|
* @param null|\DOMNodeList|array|\DOMNode|Crawler $node A node |
1044
|
|
|
* |
1045
|
|
|
* @api |
1046
|
|
|
*/ |
1047
|
14 |
|
public function add($node) |
1048
|
|
|
{ |
1049
|
14 |
|
if ($node instanceof Crawler) { |
1050
|
|
|
foreach ($node as $childnode) { |
1051
|
|
|
$this->addNode($childnode); |
1052
|
|
|
} |
1053
|
|
|
} else { |
1054
|
14 |
|
parent::add($node); |
1055
|
|
|
} |
1056
|
14 |
|
} |
1057
|
|
|
|
1058
|
|
|
/** |
1059
|
|
|
* @param \DOMNode $newnode |
1060
|
|
|
* @param \DOMNode $referencenode |
1061
|
|
|
* @param int $clone |
1062
|
|
|
* @return \DOMNode |
1063
|
|
|
*/ |
1064
|
5 |
|
protected static function importNewnode(\DOMNode $newnode, \DOMNode $referencenode, $clone = 0) { |
1065
|
5 |
|
if ($newnode->ownerDocument !== $referencenode->ownerDocument) { |
1066
|
4 |
|
$newnode = $referencenode->ownerDocument->importNode($newnode, true); |
1067
|
|
|
} else { |
1068
|
2 |
|
if ($clone > 0) { |
1069
|
|
|
$newnode = $newnode->cloneNode(true); |
1070
|
|
|
} |
1071
|
|
|
} |
1072
|
5 |
|
return $newnode; |
1073
|
|
|
} |
1074
|
|
|
|
1075
|
|
|
/** |
1076
|
|
|
* Checks whether the first node in the set is disconnected (has no parent node) |
1077
|
|
|
* |
1078
|
|
|
* @return bool |
1079
|
|
|
*/ |
1080
|
1 |
|
public function isDisconnected() |
1081
|
|
|
{ |
1082
|
1 |
|
$parent = $this->getNode(0)->parentNode; |
1083
|
1 |
|
return ($parent == null || $parent->tagName == self::FRAGMENT_ROOT_TAGNAME); |
1084
|
|
|
} |
1085
|
|
|
|
1086
|
1 |
|
public function __get($name) |
1087
|
|
|
{ |
1088
|
|
|
switch ($name) { |
1089
|
1 |
|
case 'count': |
1090
|
1 |
|
case 'length': |
1091
|
1 |
|
return count($this); |
1092
|
|
|
} |
1093
|
1 |
|
throw new \Exception('No such property ' . $name); |
1094
|
|
|
} |
1095
|
|
|
} |
1096
|
|
|
|
The number of this metric differs depending on the chosen design (inheritance vs. composition). For inheritance, the number should generally be a bit lower.
A high number indicates a reusable class. It might also make the class harder to change without breaking other classes though.