1010use PHPHtmlParser \Dom \Collection ;
1111use PHPHtmlParser \Dom \HtmlNode ;
1212use PHPHtmlParser \Dom \TextNode ;
13+ use PHPHtmlParser \DTO \TagDTO ;
1314use PHPHtmlParser \Enum \StringToken ;
1415use PHPHtmlParser \Exceptions \ChildNotFoundException ;
1516use PHPHtmlParser \Exceptions \CircularException ;
@@ -84,46 +85,17 @@ class Dom
8485 */
8586 private $ options ;
8687
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-
11988 /**
12089 * Returns the inner html of the root node.
12190 *
12291 * @throws ChildNotFoundException
12392 * @throws UnknownChildTypeException
93+ * @throws NotLoadedException
12494 */
12595 public function __toString (): string
12696 {
97+ $ this ->isLoaded ();
98+
12799 return $ this ->root ->innerHtml ();
128100 }
129101
@@ -132,10 +104,14 @@ public function __toString(): string
132104 *
133105 * @param string $name
134106 *
107+ * @throws NotLoadedException
108+ *
135109 * @return mixed
136110 */
137111 public function __get ($ name )
138112 {
113+ $ this ->isLoaded ();
114+
139115 return $ this ->root ->$ name ;
140116 }
141117
@@ -242,100 +218,6 @@ public function find(string $selector, int $nth = null)
242218 return $ this ->root ->find ($ selector , $ nth );
243219 }
244220
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-
339221 /**
340222 * Simple wrapper function that returns the first child.
341223 *
@@ -574,18 +456,18 @@ private function parse(): void
574456 $ str = $ this ->content ->copyUntil ('< ' );
575457 }
576458 if ($ str == '' ) {
577- $ info = $ this ->parseTag ();
578- if (!$ info [ ' status ' ] ) {
459+ $ tagDTO = $ this ->parseTag ();
460+ if (!$ tagDTO -> isStatus () ) {
579461 // we are done here
580462 $ activeNode = null ;
581463 continue ;
582464 }
583465
584466 // check if it was a closing tag
585- if ($ info [ ' closing ' ] ) {
467+ if ($ tagDTO -> isClosing () ) {
586468 $ foundOpeningTag = true ;
587469 $ originalNode = $ activeNode ;
588- while ($ activeNode ->getTag ()->name () != $ info [ ' tag ' ] ) {
470+ while ($ activeNode ->getTag ()->name () != $ tagDTO -> getTag () ) {
589471 $ activeNode = $ activeNode ->getParent ();
590472 if ($ activeNode === null ) {
591473 // we could not find opening tag
@@ -600,12 +482,12 @@ private function parse(): void
600482 continue ;
601483 }
602484
603- if (! isset ( $ info [ ' node ' ]) ) {
485+ if ($ tagDTO -> getNode () === null ) {
604486 continue ;
605487 }
606488
607489 /** @var AbstractNode $node */
608- $ node = $ info [ ' node ' ] ;
490+ $ node = $ tagDTO -> getNode () ;
609491 $ activeNode ->addChild ($ node );
610492
611493 // check if node is self closing
@@ -628,7 +510,7 @@ private function parse(): void
628510 *
629511 * @throws StrictException
630512 */
631- private function parseTag (): array
513+ private function parseTag (): TagDTO
632514 {
633515 $ return = [
634516 'status ' => false ,
@@ -637,15 +519,15 @@ private function parseTag(): array
637519 ];
638520 if ($ this ->content ->char () != '< ' ) {
639521 // we are not at the beginning of a tag
640- return $ return ;
522+ return new TagDTO () ;
641523 }
642524
643525 // check if this is a closing tag
644526 try {
645527 $ this ->content ->fastForward (1 );
646528 } catch (ContentLengthException $ exception ) {
647529 // we are at the end of the file
648- return $ return ;
530+ return new TagDTO () ;
649531 }
650532 if ($ this ->content ->char () == '/ ' ) {
651533 // end tag
@@ -657,22 +539,22 @@ private function parseTag(): array
657539
658540 // check if this closing tag counts
659541 $ tag = \strtolower ($ tag );
660- if (\in_array ($ tag , $ this ->selfClosing , true )) {
542+ if (\in_array ($ tag , $ this ->options -> getSelfClosing () , true )) {
661543 $ return ['status ' ] = true ;
662544
663- return $ return ;
545+ return new TagDTO ( $ return) ;
664546 }
665547 $ return ['status ' ] = true ;
666548 $ return ['closing ' ] = true ;
667549 $ return ['tag ' ] = \strtolower ($ tag );
668550
669- return $ return ;
551+ return new TagDTO ( $ return) ;
670552 }
671553
672554 $ tag = \strtolower ($ this ->content ->copyByToken (StringToken::SLASH (), true ));
673555 if (\trim ($ tag ) == '' ) {
674556 // no tag found, invalid < found
675- return $ return ;
557+ return new TagDTO () ;
676558 }
677559 $ node = new HtmlNode ($ tag );
678560 $ node ->setHtmlSpecialCharsDecode ($ this ->options ->isHtmlSpecialCharsDecode ());
@@ -754,7 +636,7 @@ private function parseTag(): array
754636 // self closing tag
755637 $ node ->getTag ()->selfClosing ();
756638 $ this ->content ->fastForward (1 );
757- } elseif (\in_array ($ tag , $ this ->selfClosing , true )) {
639+ } elseif (\in_array ($ tag , $ this ->options -> getSelfClosing () , true )) {
758640 // Should be a self closing tag, check if we are strict
759641 if ($ this ->options ->isStrict ()) {
760642 $ character = $ this ->content ->getPosition ();
@@ -765,7 +647,7 @@ private function parseTag(): array
765647 $ node ->getTag ()->selfClosing ();
766648
767649 // Should this tag use a trailing slash?
768- if (\in_array ($ tag , $ this ->noSlash , true )) {
650+ if (\in_array ($ tag , $ this ->options -> getNoSlash () , true )) {
769651 $ node ->getTag ()->noTrailingSlash ();
770652 }
771653 }
@@ -777,7 +659,7 @@ private function parseTag(): array
777659 $ return ['status ' ] = true ;
778660 $ return ['node ' ] = $ node ;
779661
780- return $ return ;
662+ return new TagDTO ( $ return) ;
781663 }
782664
783665 /**
0 commit comments