1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sulao\HtmlQuery; |
4
|
|
|
|
5
|
|
|
use DOMDocument, DOMNode, DOMNodeList; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class HtmlQuery |
9
|
|
|
* |
10
|
|
|
* @package Sulao\HtmlQuery |
11
|
|
|
*/ |
12
|
|
|
class HtmlQuery extends Selection |
13
|
|
|
{ |
14
|
|
|
const VERSION = '1.0.0'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var DOMDocument |
18
|
|
|
*/ |
19
|
|
|
protected $doc; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var DOMNode[] |
23
|
|
|
*/ |
24
|
|
|
protected $nodes; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HtmlQuery constructor. |
28
|
|
|
* |
29
|
|
|
* @param DOMDocument $doc |
30
|
|
|
* @param DOMNode|DOMNode[]|DOMNodeList $nodes |
31
|
|
|
* |
32
|
|
|
* @throws Exception |
33
|
|
|
*/ |
34
|
|
|
public function __construct(DOMDocument $doc, $nodes) |
35
|
|
|
{ |
36
|
|
|
$this->doc = $doc; |
37
|
|
|
$this->nodes = $this->validateNodes($nodes); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the outer HTML contents of the first matched node. |
42
|
|
|
* |
43
|
|
|
* @return string|null |
44
|
|
|
*/ |
45
|
|
|
public function outerHtml() |
46
|
|
|
{ |
47
|
|
|
return $this->mapFirst(function (HtmlNode $node) { |
48
|
|
|
return $node->outerHtml(); |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the inner HTML contents of the first matched node or |
54
|
|
|
* set the inner HTML contents of every matched node. |
55
|
|
|
* |
56
|
|
|
* @param string|null $html |
57
|
|
|
* |
58
|
|
|
* @return string|null|static |
59
|
|
|
*/ |
60
|
|
|
public function html(?string $html = null) |
61
|
|
|
{ |
62
|
|
|
if (!is_null($html)) { |
63
|
|
|
return $this->setHtml($html); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->getHtml(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the inner HTML contents of the first matched node. |
71
|
|
|
* |
72
|
|
|
* @return string|null |
73
|
|
|
*/ |
74
|
|
|
public function getHtml() |
75
|
|
|
{ |
76
|
|
|
return $this->mapFirst(function (HtmlNode $node) { |
77
|
|
|
return $node->getHtml(); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set the inner HTML contents of every matched node. |
83
|
|
|
* |
84
|
|
|
* @param string $html |
85
|
|
|
* |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
|
|
public function setHtml(string $html) |
89
|
|
|
{ |
90
|
|
|
$this->empty(); |
91
|
|
|
|
92
|
|
|
if ($html !== '') { |
93
|
|
|
$this->append($html); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the combined text contents of the first matched node, including |
101
|
|
|
* it's descendants, or set the text contents of every matched node. |
102
|
|
|
* |
103
|
|
|
* @param string|null $text |
104
|
|
|
* |
105
|
|
|
* @return string|null|static |
106
|
|
|
*/ |
107
|
|
|
public function text(?string $text = null) |
108
|
|
|
{ |
109
|
|
|
if (!is_null($text)) { |
110
|
|
|
return $this->setText($text); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->getText(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the combined text contents of the first matched node, |
118
|
|
|
* including it's descendants. |
119
|
|
|
* |
120
|
|
|
* @return string|null |
121
|
|
|
*/ |
122
|
|
|
public function getText() |
123
|
|
|
{ |
124
|
|
|
return $this->mapFirst(function (HtmlNode $node) { |
125
|
|
|
return $node->getText(); |
126
|
|
|
}); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* set the text contents of every matched node. |
131
|
|
|
* |
132
|
|
|
* @param string $text |
133
|
|
|
* |
134
|
|
|
* @return static |
135
|
|
|
*/ |
136
|
|
|
public function setText(string $text) |
137
|
|
|
{ |
138
|
|
|
return $this->each(function (HtmlNode $node) use ($text) { |
139
|
|
|
$node->setText($text); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the value of an attribute for the first matched node |
145
|
|
|
* or set one or more attributes for every matched node. |
146
|
|
|
* |
147
|
|
|
* @param string|array $name |
148
|
|
|
* @param string|null $value |
149
|
|
|
* |
150
|
|
|
* @return static|mixed|null |
151
|
|
|
*/ |
152
|
|
|
public function attr($name, $value = null) |
153
|
|
|
{ |
154
|
|
|
if (is_array($name)) { |
155
|
|
|
foreach ($name as $key => $val) { |
156
|
|
|
$this->setAttr($key, $val); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if (!is_null($value)) { |
163
|
|
|
return $this->setAttr($name, $value); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $this->getAttr($name); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the value of an attribute for the first matched node |
171
|
|
|
* |
172
|
|
|
* @param string $name |
173
|
|
|
* |
174
|
|
|
* @return string|null |
175
|
|
|
*/ |
176
|
|
|
public function getAttr(string $name) |
177
|
|
|
{ |
178
|
|
|
return $this->mapFirst(function (HtmlElement $node) use ($name) { |
179
|
|
|
return $node->getAttr($name); |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set one or more attributes for every matched node. |
185
|
|
|
* |
186
|
|
|
* @param string $name |
187
|
|
|
* @param string $value |
188
|
|
|
* |
189
|
|
|
* @return static |
190
|
|
|
*/ |
191
|
|
|
public function setAttr(string $name, string $value) |
192
|
|
|
{ |
193
|
|
|
return $this->each(function (HtmlElement $node) use ($name, $value) { |
194
|
|
|
$node->setAttr($name, $value); |
195
|
|
|
}); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Remove an attribute from every matched nodes. |
200
|
|
|
* |
201
|
|
|
* @param string $attributeName |
202
|
|
|
* |
203
|
|
|
* @return static |
204
|
|
|
*/ |
205
|
|
|
public function removeAttr(string $attributeName) |
206
|
|
|
{ |
207
|
|
|
return $this->each(function (HtmlElement $node) use ($attributeName) { |
208
|
|
|
$node->removeAttr($attributeName); |
209
|
|
|
}); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Remove all attributes from every matched nodes except the specified ones. |
214
|
|
|
* |
215
|
|
|
* @param string|array $except The attribute name(s) that won't be removed |
216
|
|
|
* |
217
|
|
|
* @return static |
218
|
|
|
*/ |
219
|
|
|
public function removeAllAttrs($except = []) |
220
|
|
|
{ |
221
|
|
|
return $this->each(function (HtmlElement $node) use ($except) { |
222
|
|
|
$node->removeAllAttrs($except); |
223
|
|
|
}); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Determine whether any of the nodes have the given attribute. |
228
|
|
|
* |
229
|
|
|
* @param string $attributeName |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function hasAttr(string $attributeName) |
234
|
|
|
{ |
235
|
|
|
return $this->mapAnyTrue( |
236
|
|
|
function (HtmlElement $node) use ($attributeName) { |
237
|
|
|
return $node->hasAttr($attributeName); |
238
|
|
|
} |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Alias of attr |
244
|
|
|
* |
245
|
|
|
* @param string|array $name |
246
|
|
|
* @param string|null $value |
247
|
|
|
* |
248
|
|
|
* @return static|mixed|null |
249
|
|
|
*/ |
250
|
|
|
public function prop($name, $value = null) |
251
|
|
|
{ |
252
|
|
|
return $this->attr($name, $value); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Alias of removeAttr |
257
|
|
|
* |
258
|
|
|
* @param string $attributeName |
259
|
|
|
* |
260
|
|
|
* @return static |
261
|
|
|
*/ |
262
|
|
|
public function removeProp(string $attributeName) |
263
|
|
|
{ |
264
|
|
|
return $this->removeAttr($attributeName); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Get the value of an attribute with prefix data- for the first matched |
269
|
|
|
* node, if the value is valid json string, returns the value encoded in |
270
|
|
|
* json in appropriate PHP type |
271
|
|
|
* |
272
|
|
|
* or set one or more attributes with prefix data- for every matched node. |
273
|
|
|
* |
274
|
|
|
* @param string|array $name |
275
|
|
|
* @param string|array|null $value |
276
|
|
|
* |
277
|
|
|
* @return static|mixed|null |
278
|
|
|
*/ |
279
|
|
|
public function data($name, $value = null) |
280
|
|
|
{ |
281
|
|
|
if (is_array($name)) { |
282
|
|
|
array_walk($name, function ($val, $key) { |
283
|
|
|
$this->data($key, $val); |
284
|
|
|
}); |
285
|
|
|
|
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$name = 'data-' . $name; |
290
|
|
|
|
291
|
|
|
if (is_null($value)) { |
292
|
|
|
$result = $this->getAttr($name); |
293
|
|
|
|
294
|
|
|
$json = json_decode($result); |
295
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
296
|
|
|
return $json; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $result; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if (is_array($value)) { |
303
|
|
|
$value = (string) json_encode($value); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this->setAttr($name, $value); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Determine whether any of the nodes have the given attribute |
311
|
|
|
* prefix with data-. |
312
|
|
|
* |
313
|
|
|
* @param string $name |
314
|
|
|
* |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
|
|
public function hasData(string $name) |
318
|
|
|
{ |
319
|
|
|
return $this->hasAttr('data-' . $name); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Remove an attribute prefix with data- from every matched nodes. |
324
|
|
|
* |
325
|
|
|
* @param string $name |
326
|
|
|
* |
327
|
|
|
* @return static |
328
|
|
|
*/ |
329
|
|
|
public function removeData(string $name) |
330
|
|
|
{ |
331
|
|
|
return $this->removeAttr('data-' . $name); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Remove all child nodes of all matched nodes from the DOM. |
336
|
|
|
* |
337
|
|
|
* @return static |
338
|
|
|
*/ |
339
|
|
|
public function empty() |
340
|
|
|
{ |
341
|
|
|
return $this->each(function (HtmlNode $node) { |
342
|
|
|
$node->empty(); |
343
|
|
|
}); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Remove the matched nodes from the DOM. |
348
|
|
|
* optionally filtered by a selector. |
349
|
|
|
* |
350
|
|
|
* @param string|null $selector |
351
|
|
|
* |
352
|
|
|
* @return static |
353
|
|
|
*/ |
354
|
|
|
public function remove(?string $selector = null) |
355
|
|
|
{ |
356
|
|
|
if (!is_null($selector)) { |
357
|
|
|
$this->filter($selector)->remove(); |
358
|
|
|
} else { |
359
|
|
|
$this->each(function (HtmlNode $node) { |
360
|
|
|
$node->remove(); |
361
|
|
|
}); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
return $this; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Get the current value of the first matched node |
369
|
|
|
* or set the value of every matched node. |
370
|
|
|
* |
371
|
|
|
* @param string|null $value |
372
|
|
|
* |
373
|
|
|
* @return string|null|static |
374
|
|
|
*/ |
375
|
|
|
public function val(?string $value = null) |
376
|
|
|
{ |
377
|
|
|
if (is_null($value)) { |
378
|
|
|
return $this->getVal(); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return $this->setVal($value); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* Get the current value of the first matched node |
386
|
|
|
* |
387
|
|
|
* @return string|null |
388
|
|
|
*/ |
389
|
|
|
public function getVal() |
390
|
|
|
{ |
391
|
|
|
return $this->mapFirst(function (HtmlElement $node) { |
392
|
|
|
return $node->getVal(); |
393
|
|
|
}); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Set the value of every matched node. |
398
|
|
|
* |
399
|
|
|
* @param string $value |
400
|
|
|
* |
401
|
|
|
* @return static |
402
|
|
|
*/ |
403
|
|
|
public function setVal(string $value) |
404
|
|
|
{ |
405
|
|
|
return $this->each(function (HtmlElement $node) use ($value) { |
406
|
|
|
$node->setVal($value); |
407
|
|
|
}); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Adds the specified class(es) to each node in the matched nodes. |
412
|
|
|
* |
413
|
|
|
* @param string $className |
414
|
|
|
* |
415
|
|
|
* @return static |
416
|
|
|
*/ |
417
|
|
|
public function addClass(string $className) |
418
|
|
|
{ |
419
|
|
|
return $this->each(function (HtmlElement $node) use ($className) { |
420
|
|
|
$node->addClass($className); |
421
|
|
|
}); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Determine whether any of the matched nodes are assigned the given class. |
426
|
|
|
* |
427
|
|
|
* @param string $className |
428
|
|
|
* |
429
|
|
|
* @return bool |
430
|
|
|
*/ |
431
|
|
|
public function hasClass(string $className) |
432
|
|
|
{ |
433
|
|
|
return $this->mapAnyTrue( |
434
|
|
|
function (HtmlElement $node) use ($className) { |
435
|
|
|
return $node->hasClass($className); |
436
|
|
|
} |
437
|
|
|
); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Remove a single class, multiple classes, or all classes |
442
|
|
|
* from each matched node. |
443
|
|
|
* |
444
|
|
|
* @param string|null $className |
445
|
|
|
* |
446
|
|
|
* @return static |
447
|
|
|
*/ |
448
|
|
|
public function removeClass(?string $className = null) |
449
|
|
|
{ |
450
|
|
|
return $this->each(function (HtmlElement $node) use ($className) { |
451
|
|
|
$node->removeClass($className); |
452
|
|
|
}); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* Add or remove class(es) from each matched node, depending on |
457
|
|
|
* either the class's presence or the value of the state argument. |
458
|
|
|
* |
459
|
|
|
* @param string $className |
460
|
|
|
* @param bool|null $state |
461
|
|
|
* |
462
|
|
|
* @return static |
463
|
|
|
*/ |
464
|
|
|
public function toggleClass(string $className, ?bool $state = null) |
465
|
|
|
{ |
466
|
|
|
return $this->each(function (HtmlElement $node) use ($className, $state) { |
467
|
|
|
$node->toggleClass($className, $state); |
468
|
|
|
}); |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* Get the value of a computed style property for the first matched node |
473
|
|
|
* or set one or more CSS properties for every matched node. |
474
|
|
|
* |
475
|
|
|
* @param string|array $name |
476
|
|
|
* @param string|null $value |
477
|
|
|
* |
478
|
|
|
* @return static|string|null |
479
|
|
|
*/ |
480
|
|
|
public function css($name, $value = null) |
481
|
|
|
{ |
482
|
|
|
if (is_null($value) && !is_array($name)) { |
483
|
|
|
return $this->getCss($name); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
if (is_array($name)) { |
487
|
|
|
foreach ($name as $key => $val) { |
488
|
|
|
$this->setCss($key, $val); |
489
|
|
|
} |
490
|
|
|
} else { |
491
|
|
|
$this->setCss($name, $value); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return $this; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Get the value of a computed style property for the first matched node |
500
|
|
|
* |
501
|
|
|
* @param string $name |
502
|
|
|
* |
503
|
|
|
* @return string|null |
504
|
|
|
*/ |
505
|
|
|
public function getCss(string $name) |
506
|
|
|
{ |
507
|
|
|
return $this->mapFirst(function (HtmlElement $node) use ($name) { |
508
|
|
|
return $node->getCss($name); |
509
|
|
|
}); |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* Set or Remove one CSS property for every matched node. |
514
|
|
|
* |
515
|
|
|
* @param string $name |
516
|
|
|
* @param string|null $value |
517
|
|
|
* |
518
|
|
|
* @return static |
519
|
|
|
*/ |
520
|
|
|
public function setCss(string $name, ?string $value) |
521
|
|
|
{ |
522
|
|
|
return $this->each(function (HtmlElement $node) use ($name, $value) { |
523
|
|
|
$node->setCss($name, $value); |
524
|
|
|
}); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
/** |
528
|
|
|
* Remove one CSS property for every matched node. |
529
|
|
|
* |
530
|
|
|
* @param string $name |
531
|
|
|
* |
532
|
|
|
* @return static |
533
|
|
|
*/ |
534
|
|
|
public function removeCss(string $name) |
535
|
|
|
{ |
536
|
|
|
return $this->each(function (HtmlElement $node) use ($name) { |
537
|
|
|
$node->removeCss($name); |
538
|
|
|
}); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
/** |
542
|
|
|
* Insert content or node(s) before each matched node. |
543
|
|
|
* |
544
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
545
|
|
|
* |
546
|
|
|
* @return static |
547
|
|
|
*/ |
548
|
|
|
public function before($content) |
549
|
|
|
{ |
550
|
|
|
$content = $this->contentResolve($content); |
551
|
|
|
|
552
|
|
|
return $this->each(function (HtmlNode $node, $index) use ($content) { |
553
|
|
|
$content->each(function (DOMNode $newNode) use ($node, $index) { |
554
|
|
|
$newNode = $this->newNode($newNode, $index); |
555
|
|
|
$node->before($newNode); |
556
|
|
|
}); |
557
|
|
|
}); |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
/** |
561
|
|
|
* Insert every matched node before the target. |
562
|
|
|
* |
563
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $selector |
564
|
|
|
* |
565
|
|
|
* @return static |
566
|
|
|
*/ |
567
|
|
|
public function insertBefore($selector) |
568
|
|
|
{ |
569
|
|
|
$target = $this->targetResolve($selector); |
570
|
|
|
|
571
|
|
|
return $target->before($this); |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
/** |
575
|
|
|
* Insert content or node(s) after each matched node. |
576
|
|
|
* |
577
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
578
|
|
|
* |
579
|
|
|
* @return static |
580
|
|
|
*/ |
581
|
|
|
public function after($content) |
582
|
|
|
{ |
583
|
|
|
$content = $this->contentResolve($content); |
584
|
|
|
|
585
|
|
|
return $this->each(function (HtmlNode $node, $index) use ($content) { |
586
|
|
|
$content->each(function (DOMNode $newNode) use ($node, $index) { |
587
|
|
|
$newNode = $this->newNode($newNode, $index); |
588
|
|
|
$node->after($newNode); |
589
|
|
|
}, true); |
590
|
|
|
}); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
/** |
594
|
|
|
* Insert every matched node after the target. |
595
|
|
|
* |
596
|
|
|
* @param string|DOMNode|DOMNode[]DOMNodeList|static $selector |
597
|
|
|
* |
598
|
|
|
* @return static |
599
|
|
|
*/ |
600
|
|
|
public function insertAfter($selector) |
601
|
|
|
{ |
602
|
|
|
$target = $this->targetResolve($selector); |
603
|
|
|
|
604
|
|
|
return $target->after($this); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* Insert content or node(s) to the end of every matched node. |
609
|
|
|
* |
610
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
611
|
|
|
* |
612
|
|
|
* @return static |
613
|
|
|
*/ |
614
|
|
|
public function append($content) |
615
|
|
|
{ |
616
|
|
|
$content = $this->contentResolve($content); |
617
|
|
|
|
618
|
|
|
return $this->each(function (HtmlNode $node, $index) use ($content) { |
619
|
|
|
$content->each(function (DOMNode $newNode) use ($node, $index) { |
620
|
|
|
$newNode = $this->newNode($newNode, $index); |
621
|
|
|
$node->append($newNode); |
622
|
|
|
}); |
623
|
|
|
}); |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Insert every matched node to the end of the target. |
628
|
|
|
* |
629
|
|
|
* @param string|DOMNode|DOMNode[]DOMNodeList|static $selector |
630
|
|
|
* |
631
|
|
|
* @return static |
632
|
|
|
*/ |
633
|
|
|
public function appendTo($selector) |
634
|
|
|
{ |
635
|
|
|
$target = $this->targetResolve($selector); |
636
|
|
|
|
637
|
|
|
return $target->append($this); |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Insert content or node(s) to the beginning of each matched node. |
642
|
|
|
* |
643
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
644
|
|
|
* |
645
|
|
|
* @return static |
646
|
|
|
*/ |
647
|
|
|
public function prepend($content) |
648
|
|
|
{ |
649
|
|
|
$content = $this->contentResolve($content); |
650
|
|
|
|
651
|
|
|
return $this->each(function (HtmlNode $node, $index) use ($content) { |
652
|
|
|
$content->each(function (DOMNode $newNode) use ($node, $index) { |
653
|
|
|
$newNode = $this->newNode($newNode, $index); |
654
|
|
|
$node->prepend($newNode); |
655
|
|
|
}, true); |
656
|
|
|
}); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
/** |
660
|
|
|
* Insert every matched node to the beginning of the target. |
661
|
|
|
* |
662
|
|
|
* @param string|DOMNode|DOMNode[]DOMNodeList|static $selector |
663
|
|
|
* |
664
|
|
|
* @return static |
665
|
|
|
*/ |
666
|
|
|
public function prependTo($selector) |
667
|
|
|
{ |
668
|
|
|
$target = $this->targetResolve($selector); |
669
|
|
|
|
670
|
|
|
return $target->prepend($this); |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Replace each matched node with the provided new content or node(s) |
675
|
|
|
* |
676
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
677
|
|
|
* |
678
|
|
|
* @return static |
679
|
|
|
*/ |
680
|
|
|
public function replaceWith($content) |
681
|
|
|
{ |
682
|
|
|
$content = $this->contentResolve($content); |
683
|
|
|
return $this->each(function (DOMNode $node, $index) use ($content) { |
684
|
|
|
if (!$node->parentNode) { |
685
|
|
|
return; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
$len = $content->count(); |
689
|
|
|
$content->each( |
690
|
|
|
function (DOMNode $newNode) use ($node, $index, $len) { |
691
|
|
|
$newNode = $this->newNode($newNode, $index); |
692
|
|
|
|
693
|
|
|
if ($len === 1) { |
694
|
|
|
$node->parentNode->replaceChild($newNode, $node); |
695
|
|
|
} else { |
696
|
|
|
$this->resolve($newNode)->insertAfter($node); |
697
|
|
|
} |
698
|
|
|
}, |
699
|
|
|
true |
700
|
|
|
); |
701
|
|
|
|
702
|
|
|
if ($len !== 1) { |
703
|
|
|
$node->parentNode->removeChild($node); |
704
|
|
|
} |
705
|
|
|
}); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Replace each target node with the matched node(s) |
710
|
|
|
* |
711
|
|
|
* @param string|DOMNode|DOMNode[]DOMNodeList|static $selector |
712
|
|
|
* |
713
|
|
|
* @return static |
714
|
|
|
*/ |
715
|
|
|
public function replaceAll($selector) |
716
|
|
|
{ |
717
|
|
|
$target = $this->targetResolve($selector); |
718
|
|
|
|
719
|
|
|
return $target->replaceWith($this); |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Wrap an HTML structure around each matched node. |
724
|
|
|
* |
725
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
726
|
|
|
* |
727
|
|
|
* @return static |
728
|
|
|
*/ |
729
|
|
|
public function wrap($content) |
730
|
|
|
{ |
731
|
|
|
$content = $this->contentResolve($content); |
732
|
|
|
$newNode = $content[0]; |
733
|
|
|
|
734
|
|
|
if (empty($newNode)) { |
735
|
|
|
return $this; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
return $this->each(function (DOMNode $node, $index) use ($newNode) { |
739
|
|
|
$newNode = $this->newNode($newNode, $index); |
740
|
|
|
|
741
|
|
|
$nodes = $this->xpathQuery('descendant::*[last()]', $newNode); |
742
|
|
|
if (!$nodes) { |
743
|
|
|
throw new Exception('Invalid wrap html format.'); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
$deepestNode = end($nodes); |
747
|
|
|
$node->parentNode->replaceChild($newNode, $node); |
748
|
|
|
$deepestNode->appendChild($node); |
749
|
|
|
}); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Wrap an HTML structure around the content of each matched node. |
754
|
|
|
* |
755
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
756
|
|
|
* |
757
|
|
|
* @return static |
758
|
|
|
*/ |
759
|
|
|
public function wrapInner($content) |
760
|
|
|
{ |
761
|
|
|
$content = $this->contentResolve($content); |
762
|
|
|
$newNode = $content[0]; |
763
|
|
|
|
764
|
|
|
if (empty($newNode)) { |
765
|
|
|
return $this; |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
return $this->each(function (DOMNode $node, $index) use ($newNode) { |
769
|
|
|
$newNode = $this->newNode($newNode, $index); |
770
|
|
|
|
771
|
|
|
$nodes = $this->xpathQuery('descendant::*[last()]', $newNode); |
772
|
|
|
if (!$nodes) { |
773
|
|
|
throw new Exception('Invalid wrap html format.'); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
$deepestNode = end($nodes); |
777
|
|
|
|
778
|
|
|
foreach (iterator_to_array($node->childNodes) as $childNode) { |
779
|
|
|
$deepestNode->appendChild($childNode); |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
$node->appendChild($newNode); |
783
|
|
|
}); |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Wrap an HTML structure around all matched nodes. |
788
|
|
|
* |
789
|
|
|
* @param string|DOMNode|DOMNode[]|DOMNodeList|static $content |
790
|
|
|
* |
791
|
|
|
* @return static |
792
|
|
|
*/ |
793
|
|
|
public function wrapAll($content) |
794
|
|
|
{ |
795
|
|
|
$content = $this->contentResolve($content); |
796
|
|
|
if (!$content->count()) { |
797
|
|
|
return $this; |
798
|
|
|
} |
799
|
|
|
|
800
|
|
|
$newNode = $content[0]; |
801
|
|
|
$this->each(function (DOMNode $node, $index) use ($newNode) { |
802
|
|
|
if ($index === 0) { |
803
|
|
|
$this->resolve($node)->wrap($newNode); |
804
|
|
|
} else { |
805
|
|
|
$this->nodes[0]->parentNode->appendChild($node); |
806
|
|
|
} |
807
|
|
|
}); |
808
|
|
|
|
809
|
|
|
return $this; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Remove the parents of the matched nodes from the DOM. |
814
|
|
|
* A optional selector to check the parent node against. |
815
|
|
|
* |
816
|
|
|
* @param string|null $selector |
817
|
|
|
* |
818
|
|
|
* @return static |
819
|
|
|
*/ |
820
|
|
|
public function unwrap(?string $selector = null) |
821
|
|
|
{ |
822
|
|
|
return $this->parent($selector)->unwrapSelf(); |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
/** |
826
|
|
|
* Remove the HTML tag of the matched nodes from the DOM. |
827
|
|
|
* Leaving the child nodes in their place. |
828
|
|
|
* |
829
|
|
|
* @return static |
830
|
|
|
*/ |
831
|
|
|
public function unwrapSelf() |
832
|
|
|
{ |
833
|
|
|
return $this->each(function (HtmlNode $node) { |
834
|
|
|
$node->unwrapSelf(); |
835
|
|
|
}); |
836
|
|
|
} |
837
|
|
|
} |
838
|
|
|
|