10
10
use PHPHtmlParser \Dom \Collection ;
11
11
use PHPHtmlParser \Dom \HtmlNode ;
12
12
use PHPHtmlParser \Dom \TextNode ;
13
+ use PHPHtmlParser \DTO \TagDTO ;
13
14
use PHPHtmlParser \Enum \StringToken ;
14
15
use PHPHtmlParser \Exceptions \ChildNotFoundException ;
15
16
use PHPHtmlParser \Exceptions \CircularException ;
@@ -84,46 +85,17 @@ class Dom
84
85
*/
85
86
private $ options ;
86
87
87
- /**
88
- * A list of tags which will always be self closing.
89
- *
90
- * @var array
91
- */
92
- private $ selfClosing = [
93
- 'area ' ,
94
- 'base ' ,
95
- 'basefont ' ,
96
- 'br ' ,
97
- 'col ' ,
98
- 'embed ' ,
99
- 'hr ' ,
100
- 'img ' ,
101
- 'input ' ,
102
- 'keygen ' ,
103
- 'link ' ,
104
- 'meta ' ,
105
- 'param ' ,
106
- 'source ' ,
107
- 'spacer ' ,
108
- 'track ' ,
109
- 'wbr ' ,
110
- ];
111
-
112
- /**
113
- * A list of tags where there should be no /> at the end (html5 style).
114
- *
115
- * @var array
116
- */
117
- private $ noSlash = [];
118
-
119
88
/**
120
89
* Returns the inner html of the root node.
121
90
*
122
91
* @throws ChildNotFoundException
123
92
* @throws UnknownChildTypeException
93
+ * @throws NotLoadedException
124
94
*/
125
95
public function __toString (): string
126
96
{
97
+ $ this ->isLoaded ();
98
+
127
99
return $ this ->root ->innerHtml ();
128
100
}
129
101
@@ -132,10 +104,14 @@ public function __toString(): string
132
104
*
133
105
* @param string $name
134
106
*
107
+ * @throws NotLoadedException
108
+ *
135
109
* @return mixed
136
110
*/
137
111
public function __get ($ name )
138
112
{
113
+ $ this ->isLoaded ();
114
+
139
115
return $ this ->root ->$ name ;
140
116
}
141
117
@@ -242,100 +218,6 @@ public function find(string $selector, int $nth = null)
242
218
return $ this ->root ->find ($ selector , $ nth );
243
219
}
244
220
245
- /**
246
- * Adds the tag (or tags in an array) to the list of tags that will always
247
- * be self closing.
248
- *
249
- * @param string|array $tag
250
- * @chainable
251
- */
252
- public function addSelfClosingTag ($ tag ): Dom
253
- {
254
- if (!\is_array ($ tag )) {
255
- $ tag = [$ tag ];
256
- }
257
- foreach ($ tag as $ value ) {
258
- $ this ->selfClosing [] = $ value ;
259
- }
260
-
261
- return $ this ;
262
- }
263
-
264
- /**
265
- * Removes the tag (or tags in an array) from the list of tags that will
266
- * always be self closing.
267
- *
268
- * @param string|array $tag
269
- * @chainable
270
- */
271
- public function removeSelfClosingTag ($ tag ): Dom
272
- {
273
- if (!\is_array ($ tag )) {
274
- $ tag = [$ tag ];
275
- }
276
- $ this ->selfClosing = \array_diff ($ this ->selfClosing , $ tag );
277
-
278
- return $ this ;
279
- }
280
-
281
- /**
282
- * Sets the list of self closing tags to empty.
283
- *
284
- * @chainable
285
- */
286
- public function clearSelfClosingTags (): Dom
287
- {
288
- $ this ->selfClosing = [];
289
-
290
- return $ this ;
291
- }
292
-
293
- /**
294
- * Adds a tag to the list of self closing tags that should not have a trailing slash.
295
- *
296
- * @param $tag
297
- * @chainable
298
- */
299
- public function addNoSlashTag ($ tag ): Dom
300
- {
301
- if (!\is_array ($ tag )) {
302
- $ tag = [$ tag ];
303
- }
304
- foreach ($ tag as $ value ) {
305
- $ this ->noSlash [] = $ value ;
306
- }
307
-
308
- return $ this ;
309
- }
310
-
311
- /**
312
- * Removes a tag from the list of no-slash tags.
313
- *
314
- * @param $tag
315
- * @chainable
316
- */
317
- public function removeNoSlashTag ($ tag ): Dom
318
- {
319
- if (!\is_array ($ tag )) {
320
- $ tag = [$ tag ];
321
- }
322
- $ this ->noSlash = \array_diff ($ this ->noSlash , $ tag );
323
-
324
- return $ this ;
325
- }
326
-
327
- /**
328
- * Empties the list of no-slash tags.
329
- *
330
- * @chainable
331
- */
332
- public function clearNoSlashTags (): Dom
333
- {
334
- $ this ->noSlash = [];
335
-
336
- return $ this ;
337
- }
338
-
339
221
/**
340
222
* Simple wrapper function that returns the first child.
341
223
*
@@ -574,18 +456,18 @@ private function parse(): void
574
456
$ str = $ this ->content ->copyUntil ('< ' );
575
457
}
576
458
if ($ str == '' ) {
577
- $ info = $ this ->parseTag ();
578
- if (!$ info [ ' status ' ] ) {
459
+ $ tagDTO = $ this ->parseTag ();
460
+ if (!$ tagDTO -> isStatus () ) {
579
461
// we are done here
580
462
$ activeNode = null ;
581
463
continue ;
582
464
}
583
465
584
466
// check if it was a closing tag
585
- if ($ info [ ' closing ' ] ) {
467
+ if ($ tagDTO -> isClosing () ) {
586
468
$ foundOpeningTag = true ;
587
469
$ originalNode = $ activeNode ;
588
- while ($ activeNode ->getTag ()->name () != $ info [ ' tag ' ] ) {
470
+ while ($ activeNode ->getTag ()->name () != $ tagDTO -> getTag () ) {
589
471
$ activeNode = $ activeNode ->getParent ();
590
472
if ($ activeNode === null ) {
591
473
// we could not find opening tag
@@ -600,12 +482,12 @@ private function parse(): void
600
482
continue ;
601
483
}
602
484
603
- if (! isset ( $ info [ ' node ' ]) ) {
485
+ if ($ tagDTO -> getNode () === null ) {
604
486
continue ;
605
487
}
606
488
607
489
/** @var AbstractNode $node */
608
- $ node = $ info [ ' node ' ] ;
490
+ $ node = $ tagDTO -> getNode () ;
609
491
$ activeNode ->addChild ($ node );
610
492
611
493
// check if node is self closing
@@ -628,7 +510,7 @@ private function parse(): void
628
510
*
629
511
* @throws StrictException
630
512
*/
631
- private function parseTag (): array
513
+ private function parseTag (): TagDTO
632
514
{
633
515
$ return = [
634
516
'status ' => false ,
@@ -637,15 +519,15 @@ private function parseTag(): array
637
519
];
638
520
if ($ this ->content ->char () != '< ' ) {
639
521
// we are not at the beginning of a tag
640
- return $ return ;
522
+ return new TagDTO () ;
641
523
}
642
524
643
525
// check if this is a closing tag
644
526
try {
645
527
$ this ->content ->fastForward (1 );
646
528
} catch (ContentLengthException $ exception ) {
647
529
// we are at the end of the file
648
- return $ return ;
530
+ return new TagDTO () ;
649
531
}
650
532
if ($ this ->content ->char () == '/ ' ) {
651
533
// end tag
@@ -657,22 +539,22 @@ private function parseTag(): array
657
539
658
540
// check if this closing tag counts
659
541
$ tag = \strtolower ($ tag );
660
- if (\in_array ($ tag , $ this ->selfClosing , true )) {
542
+ if (\in_array ($ tag , $ this ->options -> getSelfClosing () , true )) {
661
543
$ return ['status ' ] = true ;
662
544
663
- return $ return ;
545
+ return new TagDTO ( $ return) ;
664
546
}
665
547
$ return ['status ' ] = true ;
666
548
$ return ['closing ' ] = true ;
667
549
$ return ['tag ' ] = \strtolower ($ tag );
668
550
669
- return $ return ;
551
+ return new TagDTO ( $ return) ;
670
552
}
671
553
672
554
$ tag = \strtolower ($ this ->content ->copyByToken (StringToken::SLASH (), true ));
673
555
if (\trim ($ tag ) == '' ) {
674
556
// no tag found, invalid < found
675
- return $ return ;
557
+ return new TagDTO () ;
676
558
}
677
559
$ node = new HtmlNode ($ tag );
678
560
$ node ->setHtmlSpecialCharsDecode ($ this ->options ->isHtmlSpecialCharsDecode ());
@@ -754,7 +636,7 @@ private function parseTag(): array
754
636
// self closing tag
755
637
$ node ->getTag ()->selfClosing ();
756
638
$ this ->content ->fastForward (1 );
757
- } elseif (\in_array ($ tag , $ this ->selfClosing , true )) {
639
+ } elseif (\in_array ($ tag , $ this ->options -> getSelfClosing () , true )) {
758
640
// Should be a self closing tag, check if we are strict
759
641
if ($ this ->options ->isStrict ()) {
760
642
$ character = $ this ->content ->getPosition ();
@@ -765,7 +647,7 @@ private function parseTag(): array
765
647
$ node ->getTag ()->selfClosing ();
766
648
767
649
// Should this tag use a trailing slash?
768
- if (\in_array ($ tag , $ this ->noSlash , true )) {
650
+ if (\in_array ($ tag , $ this ->options -> getNoSlash () , true )) {
769
651
$ node ->getTag ()->noTrailingSlash ();
770
652
}
771
653
}
@@ -777,7 +659,7 @@ private function parseTag(): array
777
659
$ return ['status ' ] = true ;
778
660
$ return ['node ' ] = $ node ;
779
661
780
- return $ return ;
662
+ return new TagDTO ( $ return) ;
781
663
}
782
664
783
665
/**
0 commit comments