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