Skip to content

Commit

Permalink
Add support for PHP 8.3.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Nov 2, 2023
1 parent f2b5cc9 commit 2d0e3b6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
],
"require": {
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*",
"php": "7.0.*|7.1.*|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*",
"ext-dom": "*"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="">
<testsuite name="Tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
Expand Down
42 changes: 31 additions & 11 deletions src/HTML5DOMDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ public function __construct(string $version = '1.0', string $encoding = '')
* Load HTML from a string.
*
* @param string $source The HTML code.
* @param int $options Additional Libxml parameters.
* @param integer $options Additional Libxml parameters.
* @return boolean TRUE on success or FALSE on failure.
*/
#[\ReturnTypeWillChange] // While supporting PHP 7
public function loadHTML($source, $options = 0)
{
// Enables libxml errors handling
Expand Down Expand Up @@ -131,12 +132,7 @@ public function loadHTML($source, $options = 0)

// Add body tag if missing
if ($autoAddHtmlAndBodyTags && $source !== '' && preg_match('/\<!DOCTYPE.*?\>/', $source) === 0 && preg_match('/\<html.*?\>/', $source) === 0 && preg_match('/\<body.*?\>/', $source) === 0 && preg_match('/\<head.*?\>/', $source) === 0) {
$source = '<body>' . $source . '</body>';
}

// Add DOCTYPE if missing
if ($autoAddDoctype && strtoupper(substr($source, 0, 9)) !== '<!DOCTYPE') {
$source = "<!DOCTYPE html>\n" . $source;
$source = '<html><body>' . $source . '</body></html>';
}

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

// Add DOCTYPE if missing
if ($autoAddDoctype && strtoupper(substr($source, 0, 9)) !== '<!DOCTYPE') {
$source = "<!DOCTYPE html>\n" . $source;
}

$result = parent::loadHTML('<?xml encoding="utf-8" ?>' . $source, $options);
if ($internalErrorsOptionValue === false) {
libxml_use_internal_errors(false);
Expand Down Expand Up @@ -226,10 +227,12 @@ public function loadHTML($source, $options = 0)

/**
* Load HTML from a file.
*
*
* @param string $filename The path to the HTML file.
* @param int $options Additional Libxml parameters.
* @param integer $options Additional Libxml parameters.
* @return boolean
*/
#[\ReturnTypeWillChange] // While supporting PHP 7
public function loadHTMLFile($filename, $options = 0)
{
return $this->loadHTML(file_get_contents($filename), $options);
Expand Down Expand Up @@ -342,6 +345,7 @@ public function saveHTML(\DOMNode $node = null): string
}
$html = trim($html);
} else {
//$this->modify(self::OPTIMIZE_HEAD);
$removeHtmlElement = false;
$removeHeadElement = false;
$headElement = $this->getElementsByTagName('head')->item(0);
Expand Down Expand Up @@ -611,7 +615,7 @@ public function insertHTMLMulti(array $sources)
/**
* Applies the modifications specified to the DOM document.
*
* @param int $modifications The modifications to apply. Available values:
* @param integer $modifications The modifications to apply. Available values:
* - HTML5DOMDocument::FIX_MULTIPLE_TITLES - removes all but the last title elements.
* - HTML5DOMDocument::FIX_DUPLICATE_METATAGS - removes all but the last metatags with matching name or property attributes.
* - HTML5DOMDocument::FIX_MULTIPLE_HEADS - merges multiple head elements.
Expand Down Expand Up @@ -709,6 +713,9 @@ public function modify($modifications = 0)
$stylesToRemove = [];
$list = [];
foreach ($styles as $style) {
if ($style->parentNode !== $headElement) {
continue;
}
$innerHTML = trim($style->innerHTML);
if (array_search($innerHTML, $list) === false) {
$list[] = $innerHTML;
Expand Down Expand Up @@ -753,6 +760,19 @@ public function modify($modifications = 0)
$headElement->insertBefore($charsetMetaTag, $headElement->firstChild);
}
}
$scriptTags = $headElement->getElementsByTagName('script');
if ($scriptTags->length > 0) {
$nodesToMove = [];
foreach ($scriptTags as $scriptTag) {
if ($scriptTag->parentNode !== $headElement) {
continue;
}
$nodesToMove[] = $scriptTag;
}
foreach ($nodesToMove as $nodeToMove) {
$headElement->appendChild($nodeToMove);
}
}
}
}

Expand Down
9 changes: 4 additions & 5 deletions tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function testHtmlEntities()
/**
*
*/
public function testInserHTML()
public function testInsertHTML()
{
// insert beforeBodyEnd
$source = '<!DOCTYPE html><html><body>'
Expand Down Expand Up @@ -275,7 +275,7 @@ public function testInserHTML()
// Empty content
$dom = new HTML5DOMDocument();
$dom->insertHTML('');
$expectedSource = '<!DOCTYPE html><html></html>';
$expectedSource = '<!DOCTYPE html>';
$this->assertEquals($expectedSource, $this->removeNewLines($dom->saveHTML()));

// Html tag with attribute
Expand Down Expand Up @@ -329,7 +329,7 @@ public function testEmpty()
$source = '<!DOCTYPE html>';
$testSource($source, $source);

$testSource('', '<!DOCTYPE html><html></html>');
$testSource('', '<!DOCTYPE html>');
}

/**
Expand Down Expand Up @@ -1277,7 +1277,6 @@ public function testLIBXML_HTML_NODEFDTD()
*/
public function testLIBXML_HTML_NOIMPLIED()
{

$content = '<div>hello</div>';
$dom = new HTML5DOMDocument();
$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED);
Expand Down Expand Up @@ -1446,7 +1445,7 @@ public function testScriptsCDATA()
*
* @return array
*/
public function propertyGetterTestDataProvider()
static public function propertyGetterTestDataProvider()
{
return [
[
Expand Down

0 comments on commit 2d0e3b6

Please sign in to comment.