@@ -87,9 +87,10 @@ public function __construct(string $version = '1.0', string $encoding = '')
87
87
* Load HTML from a string.
88
88
*
89
89
* @param string $source The HTML code.
90
- * @param int $options Additional Libxml parameters.
90
+ * @param integer $options Additional Libxml parameters.
91
91
* @return boolean TRUE on success or FALSE on failure.
92
92
*/
93
+ #[\ReturnTypeWillChange] // While supporting PHP 7
93
94
public function loadHTML ($ source , $ options = 0 )
94
95
{
95
96
// Enables libxml errors handling
@@ -131,12 +132,7 @@ public function loadHTML($source, $options = 0)
131
132
132
133
// Add body tag if missing
133
134
if ($ autoAddHtmlAndBodyTags && $ source !== '' && preg_match ('/\<!DOCTYPE.*?\>/ ' , $ source ) === 0 && preg_match ('/\<html.*?\>/ ' , $ source ) === 0 && preg_match ('/\<body.*?\>/ ' , $ source ) === 0 && preg_match ('/\<head.*?\>/ ' , $ source ) === 0 ) {
134
- $ source = '<body> ' . $ source . '</body> ' ;
135
- }
136
-
137
- // Add DOCTYPE if missing
138
- if ($ autoAddDoctype && strtoupper (substr ($ source , 0 , 9 )) !== '<!DOCTYPE ' ) {
139
- $ source = "<!DOCTYPE html> \n" . $ source ;
135
+ $ source = '<html><body> ' . $ source . '</body></html> ' ;
140
136
}
141
137
142
138
// Adds temporary head tag
@@ -154,7 +150,7 @@ public function loadHTML($source, $options = 0)
154
150
if (isset ($ matches [0 ])) { // has html tag
155
151
$ source = str_replace ($ matches [0 ], $ matches [0 ] . '<head> ' . $ charsetTag . '</head> ' , $ source );
156
152
} else {
157
- $ source = '<head> ' . $ charsetTag . '</head> ' . $ source ;
153
+ $ source = '<html>< head> ' . $ charsetTag . '</head></html > ' . $ source ;
158
154
$ removeHtmlTag = true ;
159
155
}
160
156
$ removeHeadTag = true ;
@@ -164,6 +160,11 @@ public function loadHTML($source, $options = 0)
164
160
$ source = preg_replace ('/&([a-zA-Z]*);/ ' , 'html5-dom-document-internal-entity1-$1-end ' , $ source );
165
161
$ source = preg_replace ('/&#([0-9]*);/ ' , 'html5-dom-document-internal-entity2-$1-end ' , $ source );
166
162
163
+ // Add DOCTYPE if missing
164
+ if ($ autoAddDoctype && strtoupper (substr ($ source , 0 , 9 )) !== '<!DOCTYPE ' ) {
165
+ $ source = "<!DOCTYPE html> \n" . $ source ;
166
+ }
167
+
167
168
$ result = parent ::loadHTML ('<?xml encoding="utf-8" ?> ' . $ source , $ options );
168
169
if ($ internalErrorsOptionValue === false ) {
169
170
libxml_use_internal_errors (false );
@@ -226,10 +227,12 @@ public function loadHTML($source, $options = 0)
226
227
227
228
/**
228
229
* Load HTML from a file.
229
- *
230
+ *
230
231
* @param string $filename The path to the HTML file.
231
- * @param int $options Additional Libxml parameters.
232
+ * @param integer $options Additional Libxml parameters.
233
+ * @return boolean
232
234
*/
235
+ #[\ReturnTypeWillChange] // While supporting PHP 7
233
236
public function loadHTMLFile ($ filename , $ options = 0 )
234
237
{
235
238
return $ this ->loadHTML (file_get_contents ($ filename ), $ options );
@@ -342,6 +345,7 @@ public function saveHTML(\DOMNode $node = null): string
342
345
}
343
346
$ html = trim ($ html );
344
347
} else {
348
+ //$this->modify(self::OPTIMIZE_HEAD);
345
349
$ removeHtmlElement = false ;
346
350
$ removeHeadElement = false ;
347
351
$ headElement = $ this ->getElementsByTagName ('head ' )->item (0 );
@@ -611,7 +615,7 @@ public function insertHTMLMulti(array $sources)
611
615
/**
612
616
* Applies the modifications specified to the DOM document.
613
617
*
614
- * @param int $modifications The modifications to apply. Available values:
618
+ * @param integer $modifications The modifications to apply. Available values:
615
619
* - HTML5DOMDocument::FIX_MULTIPLE_TITLES - removes all but the last title elements.
616
620
* - HTML5DOMDocument::FIX_DUPLICATE_METATAGS - removes all but the last metatags with matching name or property attributes.
617
621
* - HTML5DOMDocument::FIX_MULTIPLE_HEADS - merges multiple head elements.
@@ -709,6 +713,9 @@ public function modify($modifications = 0)
709
713
$ stylesToRemove = [];
710
714
$ list = [];
711
715
foreach ($ styles as $ style ) {
716
+ if ($ style ->parentNode !== $ headElement ) {
717
+ continue ;
718
+ }
712
719
$ innerHTML = trim ($ style ->innerHTML );
713
720
if (array_search ($ innerHTML , $ list ) === false ) {
714
721
$ list [] = $ innerHTML ;
@@ -753,6 +760,19 @@ public function modify($modifications = 0)
753
760
$ headElement ->insertBefore ($ charsetMetaTag , $ headElement ->firstChild );
754
761
}
755
762
}
763
+ $ scriptTags = $ headElement ->getElementsByTagName ('script ' );
764
+ if ($ scriptTags ->length > 0 ) {
765
+ $ nodesToMove = [];
766
+ foreach ($ scriptTags as $ scriptTag ) {
767
+ if ($ scriptTag ->parentNode !== $ headElement ) {
768
+ continue ;
769
+ }
770
+ $ nodesToMove [] = $ scriptTag ;
771
+ }
772
+ foreach ($ nodesToMove as $ nodeToMove ) {
773
+ $ headElement ->appendChild ($ nodeToMove );
774
+ }
775
+ }
756
776
}
757
777
}
758
778
0 commit comments