@@ -69,6 +69,7 @@ public function __construct($node = null, string $uri = null, string $baseHref =
69
69
{
70
70
$ this ->uri = $ uri ;
71
71
$ this ->baseHref = $ baseHref ?: $ uri ;
72
+ $ this ->html5Parser = class_exists (HTML5 ::class) ? new HTML5 (['disable_html_ns ' => true ]) : null ;
72
73
73
74
$ this ->add ($ node );
74
75
}
@@ -190,13 +191,7 @@ public function addContent($content, $type = null)
190
191
public function addHtmlContent ($ content , $ charset = 'UTF-8 ' )
191
192
{
192
193
// Use HTML5 parser if the content is HTML5 and the library is available
193
- if (!$ this ->html5Parser
194
- && class_exists (HTML5 ::class)
195
- && '<!doctype html> ' === strtolower (substr (ltrim ($ content ), 0 , 15 ))) {
196
- $ this ->html5Parser = new HTML5 (['disable_html_ns ' => true ]);
197
- }
198
-
199
- $ dom = null !== $ this ->html5Parser ? $ this ->parseHtml5 ($ content , $ charset ) : $ this ->parseXhtml ($ content , $ charset );
194
+ $ dom = null !== $ this ->html5Parser && strspn ($ content , " \t\r\n" ) === stripos ($ content , '<!doctype html> ' ) ? $ this ->parseHtml5 ($ content , $ charset ) : $ this ->parseXhtml ($ content , $ charset );
200
195
$ this ->addDocument ($ dom );
201
196
202
197
$ base = $ this ->filterRelativeXPath ('descendant-or-self::base ' )->extract (['href ' ]);
@@ -599,18 +594,16 @@ public function html(/* $default = null */)
599
594
throw new \InvalidArgumentException ('The current node list is empty. ' );
600
595
}
601
596
602
- if (null !== $ this ->html5Parser ) {
603
- $ html = '' ;
604
- foreach ($ this ->getNode (0 )->childNodes as $ child ) {
605
- $ html .= $ this ->html5Parser ->saveHTML ($ child );
606
- }
597
+ $ node = $ this ->getNode (0 );
598
+ $ owner = $ node ->ownerDocument ;
607
599
608
- return $ html ;
600
+ if (null !== $ this ->html5Parser && '<!DOCTYPE html> ' === $ owner ->saveXML ($ owner ->childNodes [0 ])) {
601
+ $ owner = $ this ->html5Parser ;
609
602
}
610
603
611
604
$ html = '' ;
612
- foreach ($ this -> getNode ( 0 ) ->childNodes as $ child ) {
613
- $ html .= $ child -> ownerDocument ->saveHTML ($ child );
605
+ foreach ($ node ->childNodes as $ child ) {
606
+ $ html .= $ owner ->saveHTML ($ child );
614
607
}
615
608
616
609
return $ html ;
0 commit comments