Skip to content

Commit 2d0e3b6

Browse files
committed
Add support for PHP 8.3.
1 parent f2b5cc9 commit 2d0e3b6

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*",
13+
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*",
1414
"ext-dom": "*"
1515
},
1616
"require-dev": {

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<phpunit bootstrap="vendor/autoload.php">
22
<testsuites>
3-
<testsuite name="">
3+
<testsuite name="Tests">
44
<directory>./tests/</directory>
55
</testsuite>
66
</testsuites>

src/HTML5DOMDocument.php

+31-11
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public function __construct(string $version = '1.0', string $encoding = '')
8787
* Load HTML from a string.
8888
*
8989
* @param string $source The HTML code.
90-
* @param int $options Additional Libxml parameters.
90+
* @param integer $options Additional Libxml parameters.
9191
* @return boolean TRUE on success or FALSE on failure.
9292
*/
93+
#[\ReturnTypeWillChange] // While supporting PHP 7
9394
public function loadHTML($source, $options = 0)
9495
{
9596
// Enables libxml errors handling
@@ -131,12 +132,7 @@ public function loadHTML($source, $options = 0)
131132

132133
// Add body tag if missing
133134
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>';
140136
}
141137

142138
// Adds temporary head tag
@@ -154,7 +150,7 @@ public function loadHTML($source, $options = 0)
154150
if (isset($matches[0])) { // has html tag
155151
$source = str_replace($matches[0], $matches[0] . '<head>' . $charsetTag . '</head>', $source);
156152
} else {
157-
$source = '<head>' . $charsetTag . '</head>' . $source;
153+
$source = '<html><head>' . $charsetTag . '</head></html>' . $source;
158154
$removeHtmlTag = true;
159155
}
160156
$removeHeadTag = true;
@@ -164,6 +160,11 @@ public function loadHTML($source, $options = 0)
164160
$source = preg_replace('/&([a-zA-Z]*);/', 'html5-dom-document-internal-entity1-$1-end', $source);
165161
$source = preg_replace('/&#([0-9]*);/', 'html5-dom-document-internal-entity2-$1-end', $source);
166162

163+
// Add DOCTYPE if missing
164+
if ($autoAddDoctype && strtoupper(substr($source, 0, 9)) !== '<!DOCTYPE') {
165+
$source = "<!DOCTYPE html>\n" . $source;
166+
}
167+
167168
$result = parent::loadHTML('<?xml encoding="utf-8" ?>' . $source, $options);
168169
if ($internalErrorsOptionValue === false) {
169170
libxml_use_internal_errors(false);
@@ -226,10 +227,12 @@ public function loadHTML($source, $options = 0)
226227

227228
/**
228229
* Load HTML from a file.
229-
*
230+
*
230231
* @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
232234
*/
235+
#[\ReturnTypeWillChange] // While supporting PHP 7
233236
public function loadHTMLFile($filename, $options = 0)
234237
{
235238
return $this->loadHTML(file_get_contents($filename), $options);
@@ -342,6 +345,7 @@ public function saveHTML(\DOMNode $node = null): string
342345
}
343346
$html = trim($html);
344347
} else {
348+
//$this->modify(self::OPTIMIZE_HEAD);
345349
$removeHtmlElement = false;
346350
$removeHeadElement = false;
347351
$headElement = $this->getElementsByTagName('head')->item(0);
@@ -611,7 +615,7 @@ public function insertHTMLMulti(array $sources)
611615
/**
612616
* Applies the modifications specified to the DOM document.
613617
*
614-
* @param int $modifications The modifications to apply. Available values:
618+
* @param integer $modifications The modifications to apply. Available values:
615619
* - HTML5DOMDocument::FIX_MULTIPLE_TITLES - removes all but the last title elements.
616620
* - HTML5DOMDocument::FIX_DUPLICATE_METATAGS - removes all but the last metatags with matching name or property attributes.
617621
* - HTML5DOMDocument::FIX_MULTIPLE_HEADS - merges multiple head elements.
@@ -709,6 +713,9 @@ public function modify($modifications = 0)
709713
$stylesToRemove = [];
710714
$list = [];
711715
foreach ($styles as $style) {
716+
if ($style->parentNode !== $headElement) {
717+
continue;
718+
}
712719
$innerHTML = trim($style->innerHTML);
713720
if (array_search($innerHTML, $list) === false) {
714721
$list[] = $innerHTML;
@@ -753,6 +760,19 @@ public function modify($modifications = 0)
753760
$headElement->insertBefore($charsetMetaTag, $headElement->firstChild);
754761
}
755762
}
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+
}
756776
}
757777
}
758778

tests/Test.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function testHtmlEntities()
125125
/**
126126
*
127127
*/
128-
public function testInserHTML()
128+
public function testInsertHTML()
129129
{
130130
// insert beforeBodyEnd
131131
$source = '<!DOCTYPE html><html><body>'
@@ -275,7 +275,7 @@ public function testInserHTML()
275275
// Empty content
276276
$dom = new HTML5DOMDocument();
277277
$dom->insertHTML('');
278-
$expectedSource = '<!DOCTYPE html><html></html>';
278+
$expectedSource = '<!DOCTYPE html>';
279279
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));
280280

281281
// Html tag with attribute
@@ -329,7 +329,7 @@ public function testEmpty()
329329
$source = '<!DOCTYPE html>';
330330
$testSource($source, $source);
331331

332-
$testSource('', '<!DOCTYPE html><html></html>');
332+
$testSource('', '<!DOCTYPE html>');
333333
}
334334

335335
/**
@@ -1277,7 +1277,6 @@ public function testLIBXML_HTML_NODEFDTD()
12771277
*/
12781278
public function testLIBXML_HTML_NOIMPLIED()
12791279
{
1280-
12811280
$content = '<div>hello</div>';
12821281
$dom = new HTML5DOMDocument();
12831282
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
@@ -1446,7 +1445,7 @@ public function testScriptsCDATA()
14461445
*
14471446
* @return array
14481447
*/
1449-
public function propertyGetterTestDataProvider()
1448+
static public function propertyGetterTestDataProvider()
14501449
{
14511450
return [
14521451
[

0 commit comments

Comments
 (0)