@@ -135,56 +135,61 @@ public function compile(array $varsToPass = []) {
135135 */
136136 public static function fromHTMLText (string $ text , bool $ asHTMLDocObj = true ) {
137137 $ nodesArr = self ::htmlAsArray ($ text );
138- $ TN = 'tag-name ' ;
139- $ retVal = [];
140-
138+
141139 if (count ($ nodesArr ) >= 1 ) {
140+ $ TN = 'tag-name ' ;
141+ $ retVal = [];
142+
142143 if ($ asHTMLDocObj && ($ nodesArr [0 ][$ TN ] == 'html ' || $ nodesArr [0 ][$ TN ] == '!DOCTYPE ' )) {
143- $ retVal = new HTMLDoc ();
144- $ retVal ->getHeadNode ()->removeAllChildNodes ();
145- $ retVal ->getBody ()->removeAttributes ();
146-
147- for ($ x = 0 ; $ x < count ($ nodesArr ) ; $ x ++) {
148- if ($ nodesArr [$ x ][$ TN ] == 'html ' ) {
149- $ htmlNode = self ::fromHTMLTextHelper00 ($ nodesArr [$ x ]);
150-
151- for ($ y = 0 ; $ y < $ htmlNode ->childrenCount () ; $ y ++) {
152- $ child = $ htmlNode ->children ()->get ($ y );
153-
154- if ($ child ->getNodeName () == 'head ' ) {
155- $ retVal ->setHeadNode ($ child );
156- } else {
157- if ($ child ->getNodeName () == 'body ' ) {
158- for ($ z = 0 ; $ z < $ child ->childrenCount () ; $ z ++) {
159- $ node = $ child ->children ()->get ($ z );
160- $ retVal ->addChild ($ node );
161- }
162- }
163- }
164- }
165- } else {
166- if ($ nodesArr [$ x ][$ TN ] == 'head ' ) {
167- $ headNode = self ::fromHTMLTextHelper00 ($ nodesArr [$ x ]);
168- $ retVal ->setHeadNode ($ headNode );
169- }
170- }
171- }
144+ $ retVal = self ::parseHTMLDoc ($ nodesArr );
172145 } else {
173- if (count ($ nodesArr ) != 1 ) {
174- foreach ($ nodesArr as $ node ) {
175- $ asHtmlNode = self ::fromHTMLTextHelper00 ($ node );
176- $ retVal [] = $ asHtmlNode ;
177- }
178- } else if (count ($ nodesArr ) == 1 ) {
179- return self ::fromHTMLTextHelper00 ($ nodesArr [0 ]);
180- }
146+ $ retVal = self ::parseHTMLNode ($ nodesArr );
181147 }
182148
183149 return $ retVal ;
184150 }
185151
186152 return null ;
187153 }
154+ private static function parseHTMLNode ($ nodesArr ) {
155+ if (count ($ nodesArr ) != 1 ) {
156+ $ retVal = [];
157+ foreach ($ nodesArr as $ node ) {
158+ $ asHtmlNode = self ::fromHTMLTextHelper00 ($ node );
159+ $ retVal [] = $ asHtmlNode ;
160+ }
161+ return $ retVal ;
162+ } else {
163+ return self ::fromHTMLTextHelper00 ($ nodesArr [0 ]);
164+ }
165+ }
166+ private static function parseHTMLDoc ($ children ) : HTMLDoc {
167+ $ retVal = new HTMLDoc ();
168+ $ retVal ->getHeadNode ()->removeAllChildNodes ();
169+ $ retVal ->getBody ()->removeAttributes ();
170+ $ TN = 'tag-name ' ;
171+
172+ for ($ x = 0 ; $ x < count ($ children ) ; $ x ++) {
173+ if ($ children [$ x ][$ TN ] == 'html ' ) {
174+ $ htmlNode = self ::fromHTMLTextHelper00 ($ children [$ x ]);
175+
176+ for ($ y = 0 ; $ y < $ htmlNode ->childrenCount () ; $ y ++) {
177+ $ child = $ htmlNode ->children ()->get ($ y );
178+
179+ if ($ child ->getNodeName () == 'head ' ) {
180+ $ retVal ->setHeadNode ($ child );
181+ } else if ($ child ->getNodeName () == 'body ' ) {
182+ for ($ z = 0 ; $ z < $ child ->childrenCount () ; $ z ++) {
183+ $ node = $ child ->children ()->get ($ z );
184+ $ retVal ->addChild ($ node );
185+ }
186+
187+ }
188+ }
189+ }
190+ }
191+ return $ retVal ;
192+ }
188193 /**
189194 * Returns an array that contains directories names of the calling files.
190195 *
0 commit comments