Skip to content

Commit 254aacf

Browse files
committed
Add a new HTMLEntry
1 parent 0d8ec71 commit 254aacf

3 files changed

Lines changed: 18 additions & 55 deletions

File tree

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

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Flow\Types\Type\Native\String;
66

7-
use Dom\HTMLDocument;
8-
use Flow\Types\Value\Uuid;
7+
use Flow\Types\Value\{HTMLDocument, Uuid};
98

109
final readonly class StringTypeChecker
1110
{
@@ -101,53 +100,12 @@ public function isFloat() : bool
101100
return false;
102101
}
103102

104-
// scientific notation
105-
if (\is_numeric($this->string) && (\str_contains($this->string, 'e') || \str_contains($this->string, 'E'))) {
106-
107-
}
108-
109103
return \is_numeric($this->string) && \str_contains($this->string, '.');
110104
}
111105

112106
public function isHTML() : bool
113107
{
114-
if ($this->string === '') {
115-
return false;
116-
}
117-
118-
if ('<' !== $this->string[0]) {
119-
return false;
120-
}
121-
122-
if (\preg_match('/(<!doctype(.+?)>)?<html(.+?)>(.+?)<\/html>/im', $this->string) === 1) {
123-
if (\class_exists('\Dom\HTMLDocument', false)) {
124-
$options = \LIBXML_HTML_NOIMPLIED;
125-
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 \in_array(\strtolower((string) $doc->documentElement->tagName), ['doctype', 'html', 'html5'], true);
133-
}
134-
135-
try {
136-
\libxml_use_internal_errors(true);
137-
138-
$doc = new \DOMDocument();
139-
$result = @$doc->loadHTML($this->string);
140-
141-
return (bool) $result;
142-
} catch (\Exception) {
143-
return false;
144-
} finally {
145-
\libxml_clear_errors(); // Clear any errors if needed
146-
\libxml_use_internal_errors(false); // Restore standard error handling
147-
}
148-
}
149-
150-
return false;
108+
return HTMLDocument::isValid($this->string);
151109
}
152110

153111
public function isInteger() : bool

src/lib/types/src/Flow/Types/Value/HTMLDocument.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(string|object $value)
3434
// Cut all new lines and tabs
3535
$value = trim(str_replace(["\n", "\t"], '', $value));
3636

37-
if (!$this->isValid($value)) {
37+
if (!self::isValid($value)) {
3838
throw new InvalidArgumentException('Invalid HTML document given: ' . var_export($value, true));
3939
}
4040

@@ -46,6 +46,19 @@ public static function fromString(string $value) : self
4646
return new self($value);
4747
}
4848

49+
public static function isValid(string $value) : bool
50+
{
51+
if ('' === $value) {
52+
return false;
53+
}
54+
55+
if ('<' !== $value[0]) {
56+
return false;
57+
}
58+
59+
return \preg_match(self::HTML_ALIKE_REGEX, $value) === 1;
60+
}
61+
4962
public function __toString() : string
5063
{
5164
return $this->toString();
@@ -60,13 +73,4 @@ public function toString() : string
6073
{
6174
return $this->value;
6275
}
63-
64-
private function isValid(string $value) : bool
65-
{
66-
if ('' === $value) {
67-
return false;
68-
}
69-
70-
return \preg_match(self::HTML_ALIKE_REGEX, $value) === 1;
71-
}
7276
}

src/lib/types/tests/Flow/Types/Tests/Unit/Type/Native/String/StringTypeCheckerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public function test_detecting_float() : void
5555

5656
public function test_detecting_html() : void
5757
{
58-
self::assertTrue((new StringTypeChecker('<html lang="en"><body><div><span>1</span></div></body></html>'))->isHTML());
58+
self::assertTrue((new StringTypeChecker('<!DOCTYPE html><html><head></head><body><div>baz</div></body></html>'))->isHTML());
59+
self::assertFalse((new StringTypeChecker('<html lang="en"><body><div><span>1</span></div></body></html>'))->isHTML());
5960
self::assertFalse((new StringTypeChecker('not html'))->isHTML());
6061
}
6162

0 commit comments

Comments
 (0)