@@ -10,47 +10,53 @@ class PageIncludeContent
1010 protected static array $ topLevelTags = ['table ' , 'ul ' , 'ol ' , 'pre ' ];
1111
1212 /**
13- * @var DOMNode[]
13+ * @param DOMNode[] $contents
14+ * @param bool $isInline
1415 */
15- protected array $ contents = [];
16-
17- protected bool $ isTopLevel = false ;
18-
1916 public function __construct (
20- string $ html ,
21- PageIncludeTag $ tag ,
17+ protected array $ contents ,
18+ protected bool $ isInline ,
2219 ) {
23- $ this ->parseHtml ($ html , $ tag );
2420 }
2521
26- protected function parseHtml (string $ html , PageIncludeTag $ tag ): void
22+ public static function fromHtmlAndTag (string $ html , PageIncludeTag $ tag ): self
2723 {
2824 if (empty ($ html )) {
29- return ;
25+ return new self ([], true ) ;
3026 }
3127
3228 $ doc = new HtmlDocument ($ html );
3329
3430 $ sectionId = $ tag ->getSectionId ();
3531 if (!$ sectionId ) {
36- $ this ->contents = [...$ doc ->getBodyChildren ()];
37- $ this ->isTopLevel = true ;
38- return ;
32+ $ contents = [...$ doc ->getBodyChildren ()];
33+ return new self ($ contents , false );
3934 }
4035
4136 $ section = $ doc ->getElementById ($ sectionId );
4237 if (!$ section ) {
43- return ;
38+ return new self ([], true ) ;
4439 }
4540
4641 $ isTopLevel = in_array (strtolower ($ section ->nodeName ), static ::$ topLevelTags );
47- $ this ->isTopLevel = $ isTopLevel ;
48- $ this ->contents = $ isTopLevel ? [$ section ] : [...$ section ->childNodes ];
42+ $ contents = $ isTopLevel ? [$ section ] : [...$ section ->childNodes ];
43+ return new self ($ contents , !$ isTopLevel );
44+ }
45+
46+ public static function fromInlineHtml (string $ html ): self
47+ {
48+ if (empty ($ html )) {
49+ return new self ([], true );
50+ }
51+
52+ $ doc = new HtmlDocument ($ html );
53+
54+ return new self ([...$ doc ->getBodyChildren ()], true );
4955 }
5056
5157 public function isInline (): bool
5258 {
53- return ! $ this ->isTopLevel ;
59+ return $ this ->isInline ;
5460 }
5561
5662 public function isEmpty (): bool
@@ -65,4 +71,15 @@ public function toDomNodes(): array
6571 {
6672 return $ this ->contents ;
6773 }
74+
75+ public function toHtml (): string
76+ {
77+ $ html = '' ;
78+
79+ foreach ($ this ->contents as $ content ) {
80+ $ html .= $ content ->ownerDocument ->saveHTML ($ content );
81+ }
82+
83+ return $ html ;
84+ }
6885}
0 commit comments