1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\helper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @noinspection PhpHierarchyChecksInspection |
9
|
|
|
* |
10
|
|
|
* {@inheritdoc} |
11
|
|
|
* |
12
|
|
|
* @implements \IteratorAggregate<int, \DOMNode> |
13
|
|
|
*/ |
14
|
|
|
class SimpleHtmlDom extends AbstractSimpleHtmlDom implements \IteratorAggregate, SimpleHtmlDomInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param \DOMElement|\DOMNode $node |
18
|
|
|
*/ |
19
|
156 |
|
public function __construct(\DOMNode $node) |
20
|
|
|
{ |
21
|
156 |
|
$this->node = $node; |
22
|
156 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $name |
26
|
|
|
* @param array $arguments |
27
|
|
|
* |
28
|
|
|
* @throws \BadMethodCallException |
29
|
|
|
* |
30
|
|
|
* @return SimpleHtmlDomInterface|string|null |
31
|
|
|
*/ |
32
|
10 |
|
public function __call($name, $arguments) |
33
|
|
|
{ |
34
|
10 |
|
$name = \strtolower($name); |
35
|
|
|
|
36
|
10 |
|
if (isset(self::$functionAliases[$name])) { |
37
|
10 |
|
return \call_user_func_array([$this, self::$functionAliases[$name]], $arguments); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
throw new \BadMethodCallException('Method does not exist'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Find list of nodes with a CSS selector. |
45
|
|
|
* |
46
|
|
|
* @param string $selector |
47
|
|
|
* @param int|null $idx |
48
|
|
|
* |
49
|
|
|
* @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
50
|
|
|
*/ |
51
|
27 |
|
public function find(string $selector, $idx = null) |
52
|
|
|
{ |
53
|
27 |
|
return $this->getHtmlDomParser()->find($selector, $idx); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns an array of attributes. |
58
|
|
|
* |
59
|
|
|
* @return string[]|null |
60
|
|
|
*/ |
61
|
3 |
View Code Duplication |
public function getAllAttributes() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if ( |
64
|
3 |
|
$this->node |
65
|
|
|
&& |
66
|
3 |
|
$this->node->hasAttributes() |
67
|
|
|
) { |
68
|
3 |
|
$attributes = []; |
69
|
3 |
|
foreach ($this->node->attributes ?? [] as $attr) { |
70
|
3 |
|
$attributes[$attr->name] = HtmlDomParser::putReplacedBackToPreserveHtmlEntities($attr->value); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
return $attributes; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function hasAttributes(): bool |
83
|
|
|
{ |
84
|
|
|
return $this->node && $this->node->hasAttributes(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return attribute value. |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
25 |
|
public function getAttribute(string $name): string |
95
|
|
|
{ |
96
|
25 |
|
if ($this->node instanceof \DOMElement) { |
97
|
25 |
|
return HtmlDomParser::putReplacedBackToPreserveHtmlEntities( |
98
|
25 |
|
$this->node->getAttribute($name) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return ''; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determine if an attribute exists on the element. |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
2 |
|
public function hasAttribute(string $name): bool |
113
|
|
|
{ |
114
|
2 |
|
if (!$this->node instanceof \DOMElement) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $this->node->hasAttribute($name); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get dom node's outer html. |
123
|
|
|
* |
124
|
|
|
* @param bool $multiDecodeNewHtmlEntity |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
34 |
|
public function html(bool $multiDecodeNewHtmlEntity = false): string |
129
|
|
|
{ |
130
|
34 |
|
return $this->getHtmlDomParser()->html($multiDecodeNewHtmlEntity); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get dom node's inner html. |
135
|
|
|
* |
136
|
|
|
* @param bool $multiDecodeNewHtmlEntity |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
23 |
|
public function innerHtml(bool $multiDecodeNewHtmlEntity = false): string |
141
|
|
|
{ |
142
|
23 |
|
return $this->getHtmlDomParser()->innerHtml($multiDecodeNewHtmlEntity); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Remove attribute. |
147
|
|
|
* |
148
|
|
|
* @param string $name <p>The name of the html-attribute.</p> |
149
|
|
|
* |
150
|
|
|
* @return SimpleHtmlDomInterface |
151
|
|
|
*/ |
152
|
2 |
|
public function removeAttribute(string $name): SimpleHtmlDomInterface |
153
|
|
|
{ |
154
|
2 |
|
if (\method_exists($this->node, 'removeAttribute')) { |
155
|
2 |
|
$this->node->removeAttribute($name); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
return $this; |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Replace child node. |
163
|
|
|
* |
164
|
|
|
* @param string $string |
165
|
|
|
* |
166
|
|
|
* @return SimpleHtmlDomInterface |
167
|
|
|
*/ |
168
|
9 |
|
protected function replaceChildWithString(string $string): SimpleHtmlDomInterface |
169
|
|
|
{ |
170
|
9 |
|
if (!empty($string)) { |
171
|
8 |
|
$newDocument = new HtmlDomParser($string); |
172
|
|
|
|
173
|
8 |
|
$tmpDomString = $this->normalizeStringForComparision($newDocument); |
174
|
8 |
|
$tmpStr = $this->normalizeStringForComparision($string); |
175
|
8 |
|
if ($tmpDomString !== $tmpStr) { |
176
|
|
|
throw new \RuntimeException( |
177
|
|
|
'Not valid HTML fragment!' . "\n" . |
178
|
|
|
$tmpDomString . "\n" . |
179
|
|
|
$tmpStr |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** @var \DOMNode[] $remove_nodes */ |
185
|
9 |
|
$remove_nodes = []; |
186
|
9 |
|
if ($this->node->childNodes->length > 0) { |
187
|
|
|
// INFO: We need to fetch the nodes first, before we can delete them, because of missing references in the dom, |
188
|
|
|
// if we delete the elements on the fly. |
189
|
9 |
|
foreach ($this->node->childNodes as $node) { |
190
|
9 |
|
$remove_nodes[] = $node; |
191
|
|
|
} |
192
|
|
|
} |
193
|
9 |
|
foreach ($remove_nodes as $remove_node) { |
194
|
9 |
|
$this->node->removeChild($remove_node); |
195
|
|
|
} |
196
|
|
|
|
197
|
9 |
|
if (!empty($newDocument)) { |
198
|
8 |
|
$newDocument = $this->cleanHtmlWrapper($newDocument); |
199
|
8 |
|
$ownerDocument = $this->node->ownerDocument; |
200
|
|
|
if ( |
201
|
8 |
|
$ownerDocument |
202
|
|
|
&& |
203
|
8 |
|
$newDocument->getDocument()->documentElement |
204
|
|
|
) { |
205
|
8 |
|
$newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
206
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
207
|
8 |
|
$this->node->appendChild($newNode); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
9 |
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Replace this node. |
216
|
|
|
* |
217
|
|
|
* @param string $string |
218
|
|
|
* |
219
|
|
|
* @return SimpleHtmlDomInterface |
220
|
|
|
*/ |
221
|
6 |
|
protected function replaceNodeWithString(string $string): SimpleHtmlDomInterface |
222
|
|
|
{ |
223
|
6 |
|
if (empty($string)) { |
224
|
2 |
|
$this->node->parentNode->removeChild($this->node); |
225
|
|
|
|
226
|
2 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
5 |
|
$newDocument = new HtmlDomParser($string); |
230
|
|
|
|
231
|
5 |
|
$tmpDomOuterTextString = $this->normalizeStringForComparision($newDocument); |
232
|
5 |
|
$tmpStr = $this->normalizeStringForComparision($string); |
233
|
5 |
View Code Duplication |
if ($tmpDomOuterTextString !== $tmpStr) { |
|
|
|
|
234
|
|
|
throw new \RuntimeException( |
235
|
|
|
'Not valid HTML fragment!' . "\n" |
236
|
|
|
. $tmpDomOuterTextString . "\n" . |
237
|
|
|
$tmpStr |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
5 |
|
$newDocument = $this->cleanHtmlWrapper($newDocument, true); |
242
|
5 |
|
$ownerDocument = $this->node->ownerDocument; |
243
|
|
|
if ( |
244
|
5 |
|
$ownerDocument === null |
245
|
|
|
|| |
246
|
5 |
|
$newDocument->getDocument()->documentElement === null |
247
|
|
|
) { |
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
5 |
|
$newNode = $ownerDocument->importNode($newDocument->getDocument()->documentElement, true); |
252
|
|
|
|
253
|
5 |
|
$this->node->parentNode->replaceChild($newNode, $this->node); |
254
|
5 |
|
$this->node = $newNode; |
255
|
|
|
|
256
|
|
|
// Remove head element, preserving child nodes. (again) |
257
|
|
View Code Duplication |
if ( |
|
|
|
|
258
|
5 |
|
$this->node->parentNode instanceof \DOMElement |
259
|
|
|
&& |
260
|
5 |
|
$newDocument->getIsDOMDocumentCreatedWithoutHeadWrapper() |
261
|
|
|
) { |
262
|
3 |
|
$html = $this->node->parentNode->getElementsByTagName('head')[0]; |
263
|
|
|
|
264
|
3 |
|
if ($this->node->parentNode->ownerDocument) { |
265
|
3 |
|
$fragment = $this->node->parentNode->ownerDocument->createDocumentFragment(); |
266
|
3 |
|
if ($html !== null) { |
267
|
|
|
/** @var \DOMNode $html */ |
268
|
1 |
|
while ($html->childNodes->length > 0) { |
269
|
1 |
|
$tmpNode = $html->childNodes->item(0); |
270
|
1 |
|
if ($tmpNode !== null) { |
271
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
272
|
1 |
|
$fragment->appendChild($tmpNode); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
276
|
1 |
|
$html->parentNode->replaceChild($fragment, $html); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
281
|
5 |
|
return $this; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Replace this node with text |
286
|
|
|
* |
287
|
|
|
* @param string $string |
288
|
|
|
* |
289
|
|
|
* @return SimpleHtmlDomInterface |
290
|
|
|
*/ |
291
|
1 |
View Code Duplication |
protected function replaceTextWithString($string): SimpleHtmlDomInterface |
|
|
|
|
292
|
|
|
{ |
293
|
1 |
|
if (empty($string)) { |
294
|
1 |
|
$this->node->parentNode->removeChild($this->node); |
295
|
|
|
|
296
|
1 |
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
1 |
|
$ownerDocument = $this->node->ownerDocument; |
300
|
1 |
|
if ($ownerDocument) { |
301
|
1 |
|
$newElement = $ownerDocument->createTextNode($string); |
302
|
1 |
|
$newNode = $ownerDocument->importNode($newElement, true); |
303
|
1 |
|
$this->node->parentNode->replaceChild($newNode, $this->node); |
304
|
1 |
|
$this->node = $newNode; |
305
|
|
|
} |
306
|
|
|
|
307
|
1 |
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Set attribute value. |
312
|
|
|
* |
313
|
|
|
* @param string $name <p>The name of the html-attribute.</p> |
314
|
|
|
* @param string|null $value <p>Set to NULL or empty string, to remove the attribute.</p> |
315
|
|
|
* @param bool $strict </p> |
316
|
|
|
* $value must be NULL, to remove the attribute, |
317
|
|
|
* so that you can set an empty string as attribute-value e.g. autofocus="" |
318
|
|
|
* </p> |
319
|
|
|
* |
320
|
|
|
* @return SimpleHtmlDomInterface |
321
|
|
|
*/ |
322
|
15 |
View Code Duplication |
public function setAttribute(string $name, $value = null, bool $strict = false): SimpleHtmlDomInterface |
|
|
|
|
323
|
|
|
{ |
324
|
|
|
if ( |
325
|
15 |
|
($strict && $value === null) |
326
|
|
|
|| |
327
|
15 |
|
(!$strict && empty($value)) |
328
|
|
|
) { |
329
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
330
|
2 |
|
$this->removeAttribute($name); |
331
|
15 |
|
} elseif (\method_exists($this->node, 'setAttribute')) { |
332
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
333
|
15 |
|
$this->node->setAttribute($name, $value); |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
15 |
|
return $this; |
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Get dom node's plain text. |
341
|
|
|
* |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
28 |
|
public function text(): string |
345
|
|
|
{ |
346
|
28 |
|
return $this->getHtmlDomParser()->fixHtmlOutput($this->node->textContent); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Change the name of a tag in a "DOMNode". |
351
|
|
|
* |
352
|
|
|
* @param \DOMNode $node |
353
|
|
|
* @param string $name |
354
|
|
|
* |
355
|
|
|
* @return \DOMElement|false |
356
|
|
|
* <p>DOMElement a new instance of class DOMElement or false |
357
|
|
|
* if an error occured.</p> |
358
|
|
|
*/ |
359
|
10 |
View Code Duplication |
protected function changeElementName(\DOMNode $node, string $name) |
|
|
|
|
360
|
|
|
{ |
361
|
10 |
|
$ownerDocument = $node->ownerDocument; |
362
|
10 |
|
if (!$ownerDocument) { |
363
|
|
|
return false; |
364
|
|
|
} |
365
|
|
|
|
366
|
10 |
|
$newNode = $ownerDocument->createElement($name); |
367
|
|
|
|
368
|
10 |
|
foreach ($node->childNodes as $child) { |
369
|
10 |
|
$child = $ownerDocument->importNode($child, true); |
370
|
10 |
|
$newNode->appendChild($child); |
371
|
|
|
} |
372
|
|
|
|
373
|
10 |
|
foreach ($node->attributes ?? [] as $attrName => $attrNode) { |
374
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
375
|
|
|
$newNode->setAttribute($attrName, $attrNode); |
376
|
|
|
} |
377
|
|
|
|
378
|
10 |
|
if ($newNode->ownerDocument) { |
379
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
380
|
10 |
|
$newNode->ownerDocument->replaceChild($newNode, $node); |
381
|
|
|
} |
382
|
|
|
|
383
|
10 |
|
return $newNode; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Returns children of node. |
388
|
|
|
* |
389
|
|
|
* @param int $idx |
390
|
|
|
* |
391
|
|
|
* @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface|null |
392
|
|
|
*/ |
393
|
2 |
View Code Duplication |
public function childNodes(int $idx = -1) |
|
|
|
|
394
|
|
|
{ |
395
|
2 |
|
$nodeList = $this->getIterator(); |
396
|
|
|
|
397
|
2 |
|
if ($idx === -1) { |
398
|
2 |
|
return $nodeList; |
399
|
|
|
} |
400
|
|
|
|
401
|
2 |
|
return $nodeList[$idx] ?? null; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Find nodes with a CSS selector. |
406
|
|
|
* |
407
|
|
|
* @param string $selector |
408
|
|
|
* |
409
|
|
|
* @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
410
|
|
|
*/ |
411
|
1 |
|
public function findMulti(string $selector): SimpleHtmlDomNodeInterface |
412
|
|
|
{ |
413
|
1 |
|
return $this->getHtmlDomParser()->findMulti($selector); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Find nodes with a CSS selector or false, if no element is found. |
418
|
|
|
* |
419
|
|
|
* @param string $selector |
420
|
|
|
* |
421
|
|
|
* @return false|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
422
|
|
|
*/ |
423
|
1 |
|
public function findMultiOrFalse(string $selector) |
424
|
|
|
{ |
425
|
1 |
|
return $this->getHtmlDomParser()->findMultiOrFalse($selector); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Find one node with a CSS selector. |
430
|
|
|
* |
431
|
|
|
* @param string $selector |
432
|
|
|
* |
433
|
|
|
* @return SimpleHtmlDomInterface |
434
|
|
|
*/ |
435
|
3 |
|
public function findOne(string $selector): SimpleHtmlDomInterface |
436
|
|
|
{ |
437
|
3 |
|
return $this->getHtmlDomParser()->findOne($selector); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Find one node with a CSS selector or false, if no element is found. |
442
|
|
|
* |
443
|
|
|
* @param string $selector |
444
|
|
|
* |
445
|
|
|
* @return false|SimpleHtmlDomInterface |
446
|
|
|
*/ |
447
|
1 |
|
public function findOneOrFalse(string $selector) |
448
|
|
|
{ |
449
|
1 |
|
return $this->getHtmlDomParser()->findOneOrFalse($selector); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Returns the first child of node. |
454
|
|
|
* |
455
|
|
|
* @return SimpleHtmlDomInterface|null |
456
|
|
|
*/ |
457
|
4 |
View Code Duplication |
public function firstChild() |
|
|
|
|
458
|
|
|
{ |
459
|
|
|
/** @var \DOMNode|null $node */ |
460
|
4 |
|
$node = $this->node->firstChild; |
461
|
|
|
|
462
|
4 |
|
if ($node === null) { |
463
|
1 |
|
return null; |
464
|
|
|
} |
465
|
|
|
|
466
|
4 |
|
return new static($node); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Return elements by ".class". |
471
|
|
|
* |
472
|
|
|
* @param string $class |
473
|
|
|
* |
474
|
|
|
* @return SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
475
|
|
|
*/ |
476
|
|
|
public function getElementByClass(string $class): SimpleHtmlDomNodeInterface |
477
|
|
|
{ |
478
|
|
|
return $this->findMulti(".${class}"); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Return element by #id. |
483
|
|
|
* |
484
|
|
|
* @param string $id |
485
|
|
|
* |
486
|
|
|
* @return SimpleHtmlDomInterface |
487
|
|
|
*/ |
488
|
1 |
|
public function getElementById(string $id): SimpleHtmlDomInterface |
489
|
|
|
{ |
490
|
1 |
|
return $this->findOne("#${id}"); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Return element by tag name. |
495
|
|
|
* |
496
|
|
|
* @param string $name |
497
|
|
|
* |
498
|
|
|
* @return SimpleHtmlDomInterface |
499
|
|
|
*/ |
500
|
1 |
View Code Duplication |
public function getElementByTagName(string $name): SimpleHtmlDomInterface |
|
|
|
|
501
|
|
|
{ |
502
|
1 |
|
if ($this->node instanceof \DOMElement) { |
503
|
1 |
|
$node = $this->node->getElementsByTagName($name)->item(0); |
504
|
|
|
} else { |
505
|
|
|
$node = null; |
506
|
|
|
} |
507
|
|
|
|
508
|
1 |
|
if ($node === null) { |
509
|
|
|
return new SimpleHtmlDomBlank(); |
510
|
|
|
} |
511
|
|
|
|
512
|
1 |
|
return new static($node); |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* Returns elements by "#id". |
517
|
|
|
* |
518
|
|
|
* @param string $id |
519
|
|
|
* @param int|null $idx |
520
|
|
|
* |
521
|
|
|
* @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
522
|
|
|
*/ |
523
|
|
|
public function getElementsById(string $id, $idx = null) |
524
|
|
|
{ |
525
|
|
|
return $this->find("#${id}", $idx); |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
/** |
529
|
|
|
* Returns elements by tag name. |
530
|
|
|
* |
531
|
|
|
* @param string $name |
532
|
|
|
* @param int|null $idx |
533
|
|
|
* |
534
|
|
|
* @return SimpleHtmlDomInterface|SimpleHtmlDomInterface[]|SimpleHtmlDomNodeInterface<SimpleHtmlDomInterface> |
|
|
|
|
535
|
|
|
*/ |
536
|
1 |
View Code Duplication |
public function getElementsByTagName(string $name, $idx = null) |
|
|
|
|
537
|
|
|
{ |
538
|
1 |
|
if ($this->node instanceof \DOMElement) { |
539
|
1 |
|
$nodesList = $this->node->getElementsByTagName($name); |
540
|
|
|
} else { |
541
|
|
|
$nodesList = []; |
542
|
|
|
} |
543
|
|
|
|
544
|
1 |
|
$elements = new SimpleHtmlDomNode(); |
545
|
|
|
|
546
|
1 |
|
foreach ($nodesList as $node) { |
547
|
1 |
|
$elements[] = new static($node); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
// return all elements |
551
|
1 |
|
if ($idx === null) { |
552
|
1 |
|
if (\count($elements) === 0) { |
553
|
|
|
return new SimpleHtmlDomNodeBlank(); |
554
|
|
|
} |
555
|
|
|
|
556
|
1 |
|
return $elements; |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
// handle negative values |
560
|
|
|
if ($idx < 0) { |
561
|
|
|
$idx = \count($elements) + $idx; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
// return one element |
565
|
|
|
return $elements[$idx] ?? new SimpleHtmlDomBlank(); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Create a new "HtmlDomParser"-object from the current context. |
570
|
|
|
* |
571
|
|
|
* @return HtmlDomParser |
572
|
|
|
*/ |
573
|
98 |
|
public function getHtmlDomParser(): HtmlDomParser |
574
|
|
|
{ |
575
|
98 |
|
return new HtmlDomParser($this); |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
/** |
579
|
|
|
* @return \DOMNode |
580
|
|
|
*/ |
581
|
99 |
|
public function getNode(): \DOMNode |
582
|
|
|
{ |
583
|
99 |
|
return $this->node; |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Nodes can get partially destroyed in which they're still an |
588
|
|
|
* actual DOM node (such as \DOMElement) but almost their entire |
589
|
|
|
* body is gone, including the `nodeType` attribute. |
590
|
|
|
* |
591
|
|
|
* @return bool true if node has been destroyed |
592
|
|
|
*/ |
593
|
|
|
public function isRemoved(): bool |
594
|
|
|
{ |
595
|
|
|
return !isset($this->node->nodeType); |
596
|
|
|
} |
597
|
|
|
|
598
|
|
|
/** |
599
|
|
|
* Returns the last child of node. |
600
|
|
|
* |
601
|
|
|
* @return SimpleHtmlDomInterface|null |
602
|
|
|
*/ |
603
|
4 |
View Code Duplication |
public function lastChild() |
|
|
|
|
604
|
|
|
{ |
605
|
|
|
/** @var \DOMNode|null $node */ |
606
|
4 |
|
$node = $this->node->lastChild; |
607
|
|
|
|
608
|
4 |
|
if ($node === null) { |
609
|
1 |
|
return null; |
610
|
|
|
} |
611
|
|
|
|
612
|
4 |
|
return new static($node); |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
/** |
616
|
|
|
* Returns the next sibling of node. |
617
|
|
|
* |
618
|
|
|
* @return SimpleHtmlDomInterface|null |
619
|
|
|
*/ |
620
|
1 |
View Code Duplication |
public function nextSibling() |
|
|
|
|
621
|
|
|
{ |
622
|
|
|
/** @var \DOMNode|null $node */ |
623
|
1 |
|
$node = $this->node->nextSibling; |
624
|
|
|
|
625
|
1 |
|
if ($node === null) { |
626
|
1 |
|
return null; |
627
|
|
|
} |
628
|
|
|
|
629
|
1 |
|
return new static($node); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Returns the next sibling of node. |
634
|
|
|
* |
635
|
|
|
* @return SimpleHtmlDomInterface|null |
636
|
|
|
*/ |
637
|
1 |
View Code Duplication |
public function nextNonWhitespaceSibling() |
|
|
|
|
638
|
|
|
{ |
639
|
|
|
/** @var \DOMNode|null $node */ |
640
|
1 |
|
$node = $this->node->nextSibling; |
641
|
|
|
|
642
|
1 |
|
while ($node && !\trim($node->textContent)) { |
643
|
|
|
/** @var \DOMNode|null $node */ |
644
|
1 |
|
$node = $node->nextSibling; |
645
|
|
|
} |
646
|
|
|
|
647
|
1 |
|
if ($node === null) { |
648
|
|
|
return null; |
649
|
|
|
} |
650
|
|
|
|
651
|
1 |
|
return new static($node); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* Returns the parent of node. |
656
|
|
|
* |
657
|
|
|
* @return SimpleHtmlDomInterface |
658
|
|
|
*/ |
659
|
2 |
|
public function parentNode(): SimpleHtmlDomInterface |
660
|
|
|
{ |
661
|
2 |
|
return new static($this->node->parentNode); |
|
|
|
|
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
/** |
665
|
|
|
* Returns the previous sibling of node. |
666
|
|
|
* |
667
|
|
|
* @return SimpleHtmlDomInterface|null |
668
|
|
|
*/ |
669
|
1 |
View Code Duplication |
public function previousSibling() |
|
|
|
|
670
|
|
|
{ |
671
|
|
|
/** @var \DOMNode|null $node */ |
672
|
1 |
|
$node = $this->node->previousSibling; |
673
|
|
|
|
674
|
1 |
|
if ($node === null) { |
675
|
1 |
|
return null; |
676
|
|
|
} |
677
|
|
|
|
678
|
1 |
|
return new static($node); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
/** |
682
|
|
|
* @param string|string[]|null $value <p> |
683
|
|
|
* null === get the current input value |
684
|
|
|
* text === set a new input value |
685
|
|
|
* </p> |
686
|
|
|
* |
687
|
|
|
* @return string|string[]|null |
688
|
|
|
*/ |
689
|
1 |
View Code Duplication |
public function val($value = null) |
|
|
|
|
690
|
|
|
{ |
691
|
1 |
|
if ($value === null) { |
692
|
|
|
if ( |
693
|
1 |
|
$this->tag === 'input' |
|
|
|
|
694
|
|
|
&& |
695
|
|
|
( |
696
|
1 |
|
$this->getAttribute('type') === 'hidden' |
697
|
|
|
|| |
698
|
1 |
|
$this->getAttribute('type') === 'text' |
699
|
|
|
|| |
700
|
1 |
|
!$this->hasAttribute('type') |
701
|
|
|
) |
702
|
|
|
) { |
703
|
1 |
|
return $this->getAttribute('value'); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
if ( |
707
|
1 |
|
$this->hasAttribute('checked') |
708
|
|
|
&& |
709
|
1 |
|
\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true) |
710
|
|
|
) { |
711
|
1 |
|
return $this->getAttribute('value'); |
712
|
|
|
} |
713
|
|
|
|
714
|
1 |
|
if ($this->node->nodeName === 'select') { |
715
|
|
|
$valuesFromDom = []; |
716
|
|
|
$options = $this->getElementsByTagName('option'); |
717
|
|
|
if ($options instanceof SimpleHtmlDomNode) { |
718
|
|
|
foreach ($options as $option) { |
719
|
|
|
if ($this->hasAttribute('checked')) { |
720
|
|
|
/** @noinspection UnnecessaryCastingInspection */ |
721
|
|
|
$valuesFromDom[] = (string) $option->getAttribute('value'); |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
if (\count($valuesFromDom) === 0) { |
727
|
|
|
return null; |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
return $valuesFromDom; |
|
|
|
|
731
|
|
|
} |
732
|
|
|
|
733
|
1 |
|
if ($this->node->nodeName === 'textarea') { |
734
|
1 |
|
return $this->node->nodeValue; |
735
|
|
|
} |
736
|
|
|
} else { |
737
|
|
|
/** @noinspection NestedPositiveIfStatementsInspection */ |
738
|
1 |
|
if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) { |
739
|
1 |
|
if ($value === $this->getAttribute('value')) { |
740
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
741
|
1 |
|
$this->setAttribute('checked', 'checked'); |
742
|
|
|
} else { |
743
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
744
|
1 |
|
$this->removeAttribute('checked'); |
745
|
|
|
} |
746
|
1 |
|
} elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') { |
747
|
|
|
foreach ($this->node->getElementsByTagName('option') as $option) { |
748
|
|
|
/** @var \DOMElement $option */ |
749
|
|
|
if ($value === $option->getAttribute('value')) { |
750
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
751
|
|
|
$option->setAttribute('selected', 'selected'); |
752
|
|
|
} else { |
753
|
|
|
$option->removeAttribute('selected'); |
754
|
|
|
} |
755
|
|
|
} |
756
|
1 |
|
} elseif ($this->node->nodeName === 'input' && \is_string($value)) { |
757
|
|
|
// Set value for input elements |
758
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
759
|
1 |
|
$this->setAttribute('value', $value); |
760
|
1 |
|
} elseif ($this->node->nodeName === 'textarea' && \is_string($value)) { |
761
|
1 |
|
$this->node->nodeValue = $value; |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
|
765
|
1 |
|
return null; |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* @param HtmlDomParser $newDocument |
770
|
|
|
* @param bool $removeExtraHeadTag |
771
|
|
|
* |
772
|
|
|
* @return HtmlDomParser |
773
|
|
|
*/ |
774
|
13 |
|
protected function cleanHtmlWrapper( |
775
|
|
|
HtmlDomParser $newDocument, |
776
|
|
|
$removeExtraHeadTag = false |
777
|
|
|
): HtmlDomParser { |
778
|
|
|
if ( |
779
|
13 |
|
$newDocument->getIsDOMDocumentCreatedWithoutHtml() |
780
|
|
|
|| |
781
|
13 |
|
$newDocument->getIsDOMDocumentCreatedWithoutHtmlWrapper() |
782
|
|
|
) { |
783
|
|
|
|
784
|
|
|
// Remove doc-type node. |
785
|
13 |
|
if ($newDocument->getDocument()->doctype !== null) { |
786
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
787
|
|
|
$newDocument->getDocument()->doctype->parentNode->removeChild($newDocument->getDocument()->doctype); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
// Replace html element, preserving child nodes -> but keep the html wrapper, otherwise we got other problems ... |
791
|
|
|
// so we replace it with "<simpleHtmlDomHtml>" and delete this at the ending. |
792
|
13 |
|
$item = $newDocument->getDocument()->getElementsByTagName('html')->item(0); |
793
|
13 |
|
if ($item !== null) { |
794
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
795
|
10 |
|
$this->changeElementName($item, 'simpleHtmlDomHtml'); |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
// Remove body element, preserving child nodes. |
799
|
13 |
|
$body = $newDocument->getDocument()->getElementsByTagName('body')->item(0); |
800
|
13 |
|
if ($body instanceof \DOMElement) { |
801
|
8 |
|
$fragment = $newDocument->getDocument()->createDocumentFragment(); |
802
|
|
|
|
803
|
8 |
|
while ($body->childNodes->length > 0) { |
804
|
8 |
|
$tmpNode = $body->childNodes->item(0); |
805
|
8 |
|
if ($tmpNode !== null) { |
806
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
807
|
8 |
|
$fragment->appendChild($tmpNode); |
808
|
|
|
} |
809
|
|
|
} |
810
|
|
|
|
811
|
8 |
|
if ($body->parentNode !== null) { |
812
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
813
|
8 |
|
$body->parentNode->replaceChild($fragment, $body); |
814
|
|
|
} |
815
|
|
|
} |
816
|
|
|
} |
817
|
|
|
|
818
|
|
|
// Remove head element, preserving child nodes. |
819
|
|
View Code Duplication |
if ( |
|
|
|
|
820
|
13 |
|
$removeExtraHeadTag |
821
|
|
|
&& |
822
|
13 |
|
$this->node->parentNode instanceof \DOMElement |
823
|
|
|
&& |
824
|
13 |
|
$newDocument->getIsDOMDocumentCreatedWithoutHeadWrapper() |
825
|
|
|
) { |
826
|
3 |
|
$html = $this->node->parentNode->getElementsByTagName('head')[0] ?? null; |
827
|
|
|
|
828
|
|
|
if ( |
829
|
3 |
|
$html !== null |
830
|
|
|
&& |
831
|
3 |
|
$this->node->parentNode->ownerDocument |
832
|
|
|
) { |
833
|
|
|
$fragment = $this->node->parentNode->ownerDocument->createDocumentFragment(); |
834
|
|
|
|
835
|
|
|
/** @var \DOMNode $html */ |
836
|
|
|
while ($html->childNodes->length > 0) { |
837
|
|
|
$tmpNode = $html->childNodes->item(0); |
838
|
|
|
if ($tmpNode !== null) { |
839
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
840
|
|
|
$fragment->appendChild($tmpNode); |
841
|
|
|
} |
842
|
|
|
} |
843
|
|
|
|
844
|
|
|
/** @noinspection UnusedFunctionResultInspection */ |
845
|
|
|
$html->parentNode->replaceChild($fragment, $html); |
846
|
|
|
} |
847
|
|
|
} |
848
|
|
|
|
849
|
13 |
|
return $newDocument; |
850
|
|
|
} |
851
|
|
|
|
852
|
|
|
/** |
853
|
|
|
* Retrieve an external iterator. |
854
|
|
|
* |
855
|
|
|
* @see http://php.net/manual/en/iteratoraggregate.getiterator.php |
856
|
|
|
* |
857
|
|
|
* @return SimpleHtmlDomNode |
858
|
|
|
* <p> |
859
|
|
|
* An instance of an object implementing <b>Iterator</b> or |
860
|
|
|
* <b>Traversable</b> |
861
|
|
|
* </p> |
862
|
|
|
*/ |
863
|
3 |
View Code Duplication |
public function getIterator(): SimpleHtmlDomNodeInterface |
|
|
|
|
864
|
|
|
{ |
865
|
3 |
|
$elements = new SimpleHtmlDomNode(); |
866
|
3 |
|
if ($this->node->hasChildNodes()) { |
867
|
3 |
|
foreach ($this->node->childNodes as $node) { |
868
|
3 |
|
$elements[] = new static($node); |
869
|
|
|
} |
870
|
|
|
} |
871
|
|
|
|
872
|
3 |
|
return $elements; |
873
|
|
|
} |
874
|
|
|
|
875
|
|
|
/** |
876
|
|
|
* Get dom node's inner html. |
877
|
|
|
* |
878
|
|
|
* @param bool $multiDecodeNewHtmlEntity |
879
|
|
|
* |
880
|
|
|
* @return string |
881
|
|
|
*/ |
882
|
|
|
public function innerXml(bool $multiDecodeNewHtmlEntity = false): string |
883
|
|
|
{ |
884
|
|
|
return $this->getHtmlDomParser()->innerXml($multiDecodeNewHtmlEntity); |
885
|
|
|
} |
886
|
|
|
|
887
|
|
|
/** |
888
|
|
|
* Normalize the given input for comparision. |
889
|
|
|
* |
890
|
|
|
* @param HtmlDomParser|string $input |
891
|
|
|
* |
892
|
|
|
* @return string |
893
|
|
|
*/ |
894
|
13 |
|
private function normalizeStringForComparision($input): string |
895
|
|
|
{ |
896
|
13 |
|
if ($input instanceof HtmlDomParser) { |
897
|
13 |
|
$string = $input->outerText(); |
898
|
|
|
|
899
|
13 |
|
if ($input->getIsDOMDocumentCreatedWithoutHeadWrapper()) { |
900
|
|
|
/** @noinspection HtmlRequiredTitleElement */ |
901
|
13 |
|
$string = \str_replace(['<head>', '</head>'], '', $string); |
902
|
|
|
} |
903
|
|
|
} else { |
904
|
13 |
|
$string = (string) $input; |
905
|
|
|
} |
906
|
|
|
|
907
|
|
|
return |
908
|
13 |
|
\urlencode( |
909
|
13 |
|
\urldecode( |
910
|
13 |
|
\trim( |
911
|
13 |
|
\str_replace( |
912
|
|
|
[ |
913
|
13 |
|
' ', |
914
|
|
|
"\n", |
915
|
|
|
"\r", |
916
|
|
|
'/>', |
917
|
|
|
], |
918
|
|
|
[ |
919
|
13 |
|
'', |
920
|
|
|
'', |
921
|
|
|
'', |
922
|
|
|
'>', |
923
|
|
|
], |
924
|
13 |
|
\strtolower($string) |
925
|
|
|
) |
926
|
|
|
) |
927
|
|
|
) |
928
|
|
|
); |
929
|
|
|
} |
930
|
|
|
} |
931
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.