Skip to content

Commit dfb6fa1

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

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
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/Type/Logical/HTMLTypeTest.php

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use function Flow\Types\DSL\{type_from_array, type_html};
88
use Flow\Types\Exception\{CastingException, InvalidTypeException};
99
use Flow\Types\Value\HTMLDocument;
10-
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Attributes\{DataProvider, RequiresPhp};
1111
use PHPUnit\Framework\TestCase;
1212

1313
final class HTMLTypeTest extends TestCase
@@ -60,24 +60,39 @@ public static function assert_data_provider() : \Generator
6060
];
6161
}
6262

63-
public static function cast_data_provider() : \Generator
63+
public static function cast_data_provider_php82() : \Generator
6464
{
65-
if (PHP_VERSION_ID >= 80400) {
66-
yield 'string to HTML' => [
67-
'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
68-
'expected' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
69-
'exceptionClass' => null,
70-
];
71-
} else {
72-
yield 'string to HTML' => [
73-
'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
74-
'expected' => <<<'HTML'
65+
yield 'string to HTML' => [
66+
'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
67+
'expected' => <<<'HTML'
7568
<!DOCTYPE html>
7669
<html lang="en"><body><div><span>1</span></div></body></html>
7770
HTML,
78-
'exceptionClass' => null,
79-
];
80-
}
71+
'exceptionClass' => null,
72+
];
73+
74+
yield 'incomplete string to HTML' => [
75+
'value' => '<div><span>1</span></div>',
76+
'expected' => <<<'HTML'
77+
<div><span>1</span></div>
78+
HTML,
79+
'exceptionClass' => null,
80+
];
81+
82+
yield 'object to HTML' => [
83+
'value' => new \stdClass(),
84+
'expected' => null,
85+
'exceptionClass' => CastingException::class,
86+
];
87+
}
88+
89+
public static function cast_data_provider_php84() : \Generator
90+
{
91+
yield 'string to HTML' => [
92+
'value' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
93+
'expected' => '<!DOCTYPE html><html lang="en"><body><div><span>1</span></div></body></html>',
94+
'exceptionClass' => null,
95+
];
8196

8297
yield 'incomplete string to HTML' => [
8398
'value' => '<div><span>1</span></div>',
@@ -128,8 +143,22 @@ public function test_assert(mixed $value, ?string $exceptionClass = null) : void
128143
}
129144
}
130145

131-
#[DataProvider('cast_data_provider')]
132-
public function test_cast(mixed $value, mixed $expected, ?string $exceptionClass) : void
146+
#[RequiresPhp('< 8.4')]
147+
#[DataProvider('cast_data_provider_php82')]
148+
public function test_cast_php82(mixed $value, mixed $expected, ?string $exceptionClass) : void
149+
{
150+
if ($exceptionClass !== null) {
151+
$this->expectException($exceptionClass);
152+
type_html()->cast($value);
153+
} else {
154+
$result = type_html()->cast($value);
155+
self::assertSame($expected, $result->toString());
156+
}
157+
}
158+
159+
#[RequiresPhp('>= 8.4')]
160+
#[DataProvider('cast_data_provider_php84')]
161+
public function test_cast_php84(mixed $value, mixed $expected, ?string $exceptionClass) : void
133162
{
134163
if ($exceptionClass !== null) {
135164
$this->expectException($exceptionClass);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ 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

24-
#[RequiresPhp('<= 8.4')]
24+
#[RequiresPhp('< 8.4')]
2525
public function test_create_with_dom_document_html_on_old() : void
2626
{
2727
$doc = new \DOMDocument();
@@ -40,7 +40,7 @@ public function test_create_with_invalid_html_on_newer() : void
4040
self::assertSame('invalid', (string) $document);
4141
}
4242

43-
#[RequiresPhp('<= 8.4')]
43+
#[RequiresPhp('< 8.4')]
4444
public function test_create_with_invalid_html_on_old() : void
4545
{
4646
$document = new HTMLDocument('invalid');
@@ -57,7 +57,7 @@ public function test_create_with_proper_html_on_newer() : void
5757
self::assertSame($html, (string) $document);
5858
}
5959

60-
#[RequiresPhp('<= 8.4')]
60+
#[RequiresPhp('< 8.4')]
6161
public function test_create_with_proper_html_on_old() : void
6262
{
6363
$html = '<html><body><div><span>bar</span></div></body></html>';

0 commit comments

Comments
 (0)