Skip to content

Commit 365e58e

Browse files
committed
Add a new HTMLType
1 parent 7e26412 commit 365e58e

2 files changed

Lines changed: 93 additions & 7 deletions

File tree

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,33 @@ final class HTMLDocument implements \Stringable
1313
public function __construct(string|object $value)
1414
{
1515
if (\is_string($value)) {
16-
if (\class_exists(\Dom\HTMLDocument::class, false)) {
17-
$document = \Dom\HTMLDocument::createFromString($value);
16+
if (\class_exists('\Dom\HTMLDocument', false)) {
17+
$options = \LIBXML_HTML_NOIMPLIED;
18+
19+
if (defined('Dom\HTML_NO_DEFAULT_NS')) {
20+
$options |= constant('\Dom\HTML_NO_DEFAULT_NS');
21+
}
22+
23+
$document = \Dom\HTMLDocument::createFromString($value, $options);
1824

1925
$this->value = $document->saveHTML();
2026
} else {
2127
$document = new \DOMDocument();
22-
$document->loadHTML($value, \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD);
28+
29+
$result = @$document->loadHTML($value, \LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD);
30+
31+
if ($result === false) {
32+
throw new InvalidArgumentException("Invalid value '{$value}'");
33+
}
2334

2435
$value = $document->saveHTML() ?: throw new InvalidArgumentException("Invalid value '{$value}'");
2536

26-
$this->value = $value;
37+
$this->value = trim($value);
2738
}
2839
} elseif ($value instanceof \DOMDocument) {
29-
$value = $value->saveHTML() ?: throw new InvalidArgumentException('Invalid value ' . var_export($value, true));
40+
$value = $value->saveHTML($value->documentElement) ?: throw new InvalidArgumentException('Invalid value ' . var_export($value, true));
3041

31-
$this->value = $value;
42+
$this->value = trim($value);
3243
} elseif (is_a($value, '\Dom\HTMLDocument', true)) {
3344
/* @phpstan-ignore-next-line */
3445
$this->value = $value->saveHtml();
@@ -44,7 +55,7 @@ public static function fromString(string $value) : self
4455

4556
public function __toString() : string
4657
{
47-
return '';
58+
return $this->toString();
4859
}
4960

5061
public function isEqual(self $type) : bool
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\Types\Tests\Unit\Value;
6+
7+
use Flow\Types\Exception\InvalidArgumentException;
8+
use Flow\Types\Value\HTMLDocument;
9+
use PHPUnit\Framework\Attributes\RequiresPhp;
10+
use PHPUnit\Framework\TestCase;
11+
12+
final class HTMLDocumentTest extends TestCase
13+
{
14+
#[RequiresPhp('>= 8.4')]
15+
public function test_create_with_dom_document_html_on_newer() : void
16+
{
17+
$doc = \Dom\HTMLDocument::createFromString('<html><body><div><span>bar</span></div></body></html>');
18+
19+
$document = new HTMLDocument($doc);
20+
21+
self::assertSame('<html><head></head><body><div><span>bar</span></div></body></html>', (string) $document);
22+
}
23+
24+
#[RequiresPhp('<= 8.4')]
25+
public function test_create_with_dom_document_html_on_old() : void
26+
{
27+
$doc = new \DOMDocument();
28+
$doc->loadHTML($html = '<html><body><div><span>bar</span></div></body></html>');
29+
30+
$document = new HTMLDocument($doc);
31+
32+
self::assertSame($html, (string) $document);
33+
}
34+
35+
#[RequiresPhp('>= 8.4')]
36+
public function test_create_with_invalid_html_on_newer() : void
37+
{
38+
$document = new HTMLDocument('invalid');
39+
40+
self::assertSame('invalid', (string) $document);
41+
}
42+
43+
#[RequiresPhp('<= 8.4')]
44+
public function test_create_with_invalid_html_on_old() : void
45+
{
46+
$document = new HTMLDocument('invalid');
47+
48+
self::assertSame('<p>invalid</p>', (string) $document);
49+
}
50+
51+
#[RequiresPhp('>= 8.4')]
52+
public function test_create_with_proper_html_on_newer() : void
53+
{
54+
$html = '<html><body><div><span>bar</span></div></body></html>';
55+
$document = new HTMLDocument($html);
56+
57+
self::assertSame($html, (string) $document);
58+
}
59+
60+
#[RequiresPhp('<= 8.4')]
61+
public function test_create_with_proper_html_on_old() : void
62+
{
63+
$html = '<html><body><div><span>bar</span></div></body></html>';
64+
$document = new HTMLDocument($html);
65+
66+
self::assertSame($html, (string) $document);
67+
}
68+
69+
public function test_with_random_object() : void
70+
{
71+
$this->expectException(InvalidArgumentException::class);
72+
73+
new HTMLDocument(new \stdClass());
74+
}
75+
}

0 commit comments

Comments
 (0)