@@ -80,7 +80,7 @@ private function retrieveTemplate(string $path): string
8080 return file_get_contents ($ searchPath );
8181 }
8282
83- private function parseDom (string $ template ): NodeList
83+ private function parseDom (string $ template ): HTMLDocument | NodeList
8484 {
8585 $ template = str ($ template )
8686
@@ -104,6 +104,12 @@ private function parseDom(string $template): NodeList
104104 },
105105 );
106106
107+ if ($ template ->startsWith (['<html ' , '<!DOCTYPE ' , '<!doctype ' ])) {
108+ // If we're rendering a full HTML document, we'll parse it as is
109+ return HTMLDocument::createFromString ($ template ->toString (), LIBXML_NOERROR | HTML_NO_DEFAULT_NS );
110+ }
111+
112+ // If we're rendering an HTML snippet, we'll wrap it in a div, and return the resulting nodelist
107113 $ dom = HTMLDocument::createFromString ("<div id='tempest_render'> {$ template }</div> " , LIBXML_NOERROR | HTML_NO_DEFAULT_NS );
108114
109115 return $ dom ->getElementById ('tempest_render ' )->childNodes ;
@@ -112,11 +118,15 @@ private function parseDom(string $template): NodeList
112118 /**
113119 * @return Element[]
114120 */
115- private function mapToElements (NodeList $ nodeList ): array
121+ private function mapToElements (HTMLDocument | NodeList $ nodes ): array
116122 {
117123 $ elements = [];
118124
119- foreach ($ nodeList as $ node ) {
125+ if ($ nodes instanceof HTMLDocument) {
126+ $ nodes = $ nodes ->childNodes ;
127+ }
128+
129+ foreach ($ nodes as $ node ) {
120130 $ element = $ this ->elementFactory ->make ($ node );
121131
122132 if ($ element === null ) {
0 commit comments