generated from 8fold/github-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement.php
166 lines (133 loc) · 4.27 KB
/
Element.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
namespace Eightfold\HTMLBuilder;
use Stringable;
use Eightfold\XMLBuilder\Element as XMLElement;
class Element extends XMLElement implements Stringable
{
/**
* The following attributes will be placed in this order if used in an
* element. To change the order, extend the Element class and override
* this constant.
*/
public const ORDERED = [
"is",
"role",
"id",
"class",
"style",
"type",
"media",
"tabindex",
"accesskey",
"width",
"height",
"lang",
"srclang",
"hreflang",
"dir",
"translate",
"src",
"rel",
"href",
"target",
"itemtype",
"itemref",
"itemprop",
"title",
"name",
"http-equiv",
"charset",
"alt",
"value",
"content",
"manifest",
"contenteditable",
"spellcheck",
"start"
];
protected function omitEndTagClosingString(): string
{
return '>';
}
protected function propertiesString(): string
{
if (count($this->properties()) === 0) {
return '';
}
$orderedAttributes = array_fill_keys(static::ORDERED, "");
$otherAttributes = [];
$booleanAttributes = [];
$build = [];
foreach ($this->properties() as $property) {
if (strlen($property) === 0) {
continue;
}
list($attr, $content) = explode(' ', $property, 2);
if (strlen($content) > 0) {
if (array_key_exists($attr, $orderedAttributes)) {
$orderedAttributes[$attr] = $content;
} elseif ($attr === $content) {
$booleanAttributes[$attr] = $content;
} else {
$otherAttributes[$attr] = $content;
}
}
// Logic from previous implementation using php-html-spec to
// query and validate.
//
// if (array_key_exists($attr, $orderedAttributes)) {
// $orderedAttributes[$attr] = $content;
// } elseif ($index->hasComponentNamed($attr) and
// $a = $index->componentNamed($attr)
// ) {
// if ($a->isEvent()) {
// $eventAttributes[$attr] = $content;
// } elseif ($a->isData()) {
// $dataAttributes[$attr] = $content;
// } elseif ($a->isGlobal()) {
// $globalAttributes[$attr] = $content;
// } elseif ($a->isOther()) {
// $otherAttributes[$attr] = $content;
// } elseif ($a->isBoolean()) {
// $booleanAttributes[$attr] = $content;
// }
// } else {
// $otherAttributes[$attr] = $content;
// }
}
$orderedAttributes = array_filter($orderedAttributes, fn($c) => strlen($c) > 0);
// $eventAttributes = array_filter($eventAttributes);
// ksort($eventAttributes);
// $dataAttributes = array_filter($dataAttributes);
// ksort($eventAttributes);
// $globalAttributes = array_filter($globalAttributes);
// ksort($globalAttributes);
// $otherAttributes = array_filter($otherAttributes, fn($c) => strlen($c) > 0);
ksort($otherAttributes);
// $booleanAttributes = array_filter($booleanAttributes);
ksort($booleanAttributes);
// $merged = array_merge(
// $orderedAttributes,
// // $eventAttributes,
// // $dataAttributes,
// // $globalAttributes,
// $otherAttributes,
// $booleanAttributes
// );
$b = [];
foreach ($orderedAttributes as $prop => $content) {
$b[] = $prop . '="' . $content . '"';
}
foreach ($otherAttributes as $prop => $content) {
$b[] = $prop . '="' . $content . '"';
}
foreach ($booleanAttributes as $prop => $content) {
$b[] = $prop;
}
if (count($b) === 0) {
return '';
}
return ' ' . implode(' ', $b);
}
}