Skip to content

Commit c8ce9c1

Browse files
committed
Fixed wrong HTML detection on PHP 8.4
1 parent 49c5e86 commit c8ce9c1

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/lib/types/src/Flow/Types/Type/Native/String/StringTypeChecker.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ public function isHTML() : bool
119119
return false;
120120
}
121121

122-
if (\class_exists('\Dom\HTMLDocument', false)) {
123-
$options = \LIBXML_HTML_NOIMPLIED;
122+
if (\preg_match('/(<!doctype(.+?)>)?<html(.+?)>(.+?)<\/html>/im', $this->string) === 1) {
123+
if (\class_exists('\Dom\HTMLDocument', false)) {
124+
$options = \LIBXML_HTML_NOIMPLIED;
124125

125-
if (defined('Dom\HTML_NO_DEFAULT_NS')) {
126-
$options |= constant('\Dom\HTML_NO_DEFAULT_NS');
126+
if (defined('Dom\HTML_NO_DEFAULT_NS')) {
127+
$options |= constant('\Dom\HTML_NO_DEFAULT_NS');
128+
}
129+
130+
$doc = @HTMLDocument::createFromString($this->string, $options);
131+
132+
return $doc->saveHtml() === $this->string;
127133
}
128134

129-
HTMLDocument::createFromString($this->string, $options);
130-
} elseif (\preg_match('/(<!doctype(.+?)>)?<html(.+?)>(.+?)<\/html>/im', $this->string) === 1) {
131135
try {
132136
\libxml_use_internal_errors(true);
133137

src/lib/types/tests/Flow/Types/Tests/Unit/Value/HTMLDocumentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ final class HTMLDocumentTest extends TestCase
1414
#[RequiresPhp('>= 8.4')]
1515
public function test_create_with_dom_document_html_on_newer() : void
1616
{
17-
$doc = \Dom\HTMLDocument::createFromString('<html><body><div><span>bar</span></div></body></html>');
17+
$doc = \Dom\HTMLDocument::createFromString('<html><body><div><span>bar</span></div></body></html>', \LIBXML_HTML_NOIMPLIED);
1818

1919
$document = new HTMLDocument($doc);
2020

21-
self::assertSame('<html><head></head><body><div><span>bar</span></div></body></html>', (string) $document);
21+
self::assertSame('<html><body><div><span>bar</span></div></body></html>', (string) $document);
2222
}
2323

2424
#[RequiresPhp('<= 8.4')]

0 commit comments

Comments
 (0)