-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeBuilderUtils.php
More file actions
105 lines (94 loc) · 3.79 KB
/
TreeBuilderUtils.php
File metadata and controls
105 lines (94 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace JustHTML;
final class InsertionMode
{
public const INITIAL = 0;
public const BEFORE_HTML = 1;
public const BEFORE_HEAD = 2;
public const IN_HEAD = 3;
public const IN_HEAD_NOSCRIPT = 4;
public const AFTER_HEAD = 5;
public const TEXT = 6;
public const IN_BODY = 7;
public const AFTER_BODY = 8;
public const AFTER_AFTER_BODY = 9;
public const IN_TABLE = 10;
public const IN_TABLE_TEXT = 11;
public const IN_CAPTION = 12;
public const IN_COLUMN_GROUP = 13;
public const IN_TABLE_BODY = 14;
public const IN_ROW = 15;
public const IN_CELL = 16;
public const IN_FRAMESET = 17;
public const AFTER_FRAMESET = 18;
public const AFTER_AFTER_FRAMESET = 19;
public const IN_SELECT = 20;
public const IN_TEMPLATE = 21;
}
final class TreeBuilderUtils
{
public static function isAllWhitespace(string $text): bool
{
if ($text === '') {
return true;
}
return strspn($text, "\t\n\f\r ") === strlen($text);
}
/** @param array<int, string> $haystack */
public static function containsPrefix(array $haystack, string $needle): bool
{
foreach ($haystack as $prefix) {
if (strncmp($needle, $prefix, strlen($prefix)) === 0) {
return true;
}
}
return false;
}
/** @return array{0: bool, 1: string} */
public static function doctypeErrorAndQuirks(Doctype $doctype, bool $iframeSrcdoc = false): array
{
$name = $doctype->name !== null ? strtolower($doctype->name) : null;
$publicId = $doctype->publicId;
$systemId = $doctype->systemId;
$acceptable = [
["html", null, null],
["html", null, "about:legacy-compat"],
["html", "-//W3C//DTD HTML 4.0//EN", null],
["html", "-//W3C//DTD HTML 4.0//EN", "http://www.w3.org/TR/REC-html40/strict.dtd"],
["html", "-//W3C//DTD HTML 4.01//EN", null],
["html", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd"],
["html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"],
["html", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"],
];
$parseError = true;
foreach ($acceptable as $entry) {
if ($entry[0] === $name && $entry[1] === $publicId && $entry[2] === $systemId) {
$parseError = false;
break;
}
}
$publicLower = $publicId !== null ? strtolower($publicId) : null;
$systemLower = $systemId !== null ? strtolower($systemId) : null;
if ($doctype->forceQuirks) {
$quirksMode = "quirks";
} elseif ($iframeSrcdoc) {
$quirksMode = "no-quirks";
} elseif ($name !== "html") {
$quirksMode = "quirks";
} elseif ($publicLower !== null && in_array($publicLower, Constants::QUIRKY_PUBLIC_MATCHES, true)) {
$quirksMode = "quirks";
} elseif ($systemLower !== null && in_array($systemLower, Constants::QUIRKY_SYSTEM_MATCHES, true)) {
$quirksMode = "quirks";
} elseif ($publicLower !== null && self::containsPrefix(Constants::QUIRKY_PUBLIC_PREFIXES, $publicLower)) {
$quirksMode = "quirks";
} elseif ($publicLower !== null && self::containsPrefix(Constants::LIMITED_QUIRKY_PUBLIC_PREFIXES, $publicLower)) {
$quirksMode = "limited-quirks";
} elseif ($publicLower !== null && self::containsPrefix(Constants::HTML4_PUBLIC_PREFIXES, $publicLower)) {
$quirksMode = $systemLower === null ? "quirks" : "limited-quirks";
} else {
$quirksMode = "no-quirks";
}
return [$parseError, $quirksMode];
}
}