-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHTMLAttributeBinder.php
321 lines (286 loc) · 8.02 KB
/
HTMLAttributeBinder.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
namespace Gt\DomTemplate;
use Gt\Dom\Attr;
use Gt\Dom\Document;
use Gt\Dom\DOMTokenList;
use Gt\Dom\DOMTokenListFactory;
use Gt\Dom\Element;
class HTMLAttributeBinder {
private ListBinder $listBinder;
private TableBinder $tableBinder;
public function setDependencies(ListBinder $listBinder, TableBinder $tableBinder):void {
$this->listBinder = $listBinder;
$this->tableBinder = $tableBinder;
}
public function bind(
?string $key,
mixed $value,
Document|Element $element
):void {
if(is_null($value)) {
return;
}
if(!is_scalar($value) && !is_iterable($value)) {
$value = new BindValue($value);
}
if($element instanceof Document) {
$element = $element->documentElement;
}
$attributesToRemove = [];
/**
* @var string $attrName
* @var Attr $attr
*/
foreach($element->attributes as $attrName => $attr) {
$attrValue = $attr->value;
if($attrName === "data-element") {
if($attr->value === $key && $value) {
$element->setAttribute("data-bound", "");
}
}
if(!str_starts_with($attrName, "data-bind")) {
continue;
}
if(!str_contains($attrName, ":")) {
$tag = $this->getHTMLTag($element);
throw new InvalidBindPropertyException("$tag Element has a data-bind attribute with missing bind property - did you mean `data-bind:text`?");
}
$modifier = null;
if(is_null($key)) {
// If there is no key specified, only bind the elements that don't have a
// specified key in their bind attribute's value.
if(strlen($attrValue) > 0) {
continue;
}
}
else {
// If a key is specified, and the bind attribute's value doesn't match,
// skip this attribute.
$trimmedAttrValue = ltrim($attrValue, ":!?");
$trimmedAttrValue = strtok($trimmedAttrValue, " ");
if($key !== $trimmedAttrValue && $trimmedAttrValue !== "@") {
continue;
}
if($attrValue !== $trimmedAttrValue) {
$modifier = $attrValue;
}
}
$bindProperty = substr(
$attrName,
strpos($attrName, ":") + 1
);
$this->setBindProperty(
$element,
$bindProperty,
$value,
$modifier
);
$element->setAttribute("data-bound", "");
if(!$attr->ownerElement->hasAttribute("data-rebind")) {
array_push($attributesToRemove, $attrName);
}
}
foreach($attributesToRemove as $attrName) {
$element->removeAttribute($attrName);
}
}
public function expandAttributes(Element $element):void {
/**
* @var string $attrName
* @var Attr $attr
*/
foreach($element->attributes as $attrName => $attr) {
$attrValue = $attr->value;
if(!str_starts_with($attrName, "data-bind:")) {
continue;
}
if($attrName === "data-bind:list") {
if($attrValue === "") {
$attrValue = $element->tagName;
if(str_contains($attrValue, "-")) {
$newAttrValue = "";
foreach(explode("-", $attrValue) as $i => $part) {
if($i > 0) {
$part = ucfirst($part);
}
$newAttrValue .= $part;
}
$attrValue = $newAttrValue;
}
$element->setAttribute($attrName, $attrValue);
}
}
if(strlen($attrValue) === 0) {
continue;
}
if($attrValue[0] === "@") {
if($attrValue === "@") {
$otherAttrName = "name";
}
else {
$otherAttrName = substr($attrValue, 1);
}
$element->setAttribute(
$attrName,
$element->getAttribute($otherAttrName)
);
}
}
}
private function getHTMLTag(Element $element):string {
return "<" . strtolower($element->tagName) . ">";
}
/**
* This function actually mutates the Element. The type of mutation is
* defined by the value of $bindProperty. The default behaviour is to
* set the an attribute on $element where the attribute key is equal to
* $bindProperty and the attribute value is equal to $bindValue, however
* there are a few values of $bindProperty that affect this behaviour:
*
* 1) "text" will set the textContent of $element. Why "text" and
* not "textContent"? Because HTML attributes can't have uppercase
* characters, and this removes ambiguity.
* 2) "html" will set the innerHTML of $element. Same as above.
* 3) "class" will add the provided value as a class (rather than
* setting the class attribute and losing existing classes). The colon
* can be added to the bindKey to toggle, as explained in point 6 below.
* 4) "table" will create the appropriate columns and rows within the
* first <table> element within the element being bound.
* 5) "attr" will bind the attribute with the same name as the bindKey.
* 6) By default, the attribute matching $bindProperty will be set,
* according to these rules:
* + If the bindKey is an alphanumeric string, the attribute will be
* set to the value of the matching bindValue.
* + If the bindKey starts with a colon character ":", the attribute
* will be treated as a Token List, and the matching token will be
* added/removed from the attribute value depending on whether the
* $bindValue is true/false.
* + If the bindKey starts with a question mark "?", the attribute
* will be toggled, depending on whether the $bindValue is
* true/false.
* + If the bindKey starts with a question mark and exclamation mark,
* "?!", the attribute will be toggled as above, but with inverse
* logic. Useful for toggling "disabled" attribute from data that
* represents "enabled" state.
*
* With colon/question mark bind values, the value of the attribute will
* match the value of $bindValue - if a different attribute value is
* required, this can be specified after a space. For example:
* data-bind:class=":isSelected selected-item" will add/remove the
* "selected-item" class depending on the $bindValue's boolean value.
* @noinspection SpellCheckingInspection
*/
private function setBindProperty(
Element $element,
string $bindProperty,
mixed $bindValue,
?string $modifier = null
):void {
switch(strtolower($bindProperty)) {
case "text":
case "innertext":
case "inner-text":
case "textcontent":
case "text-content":
$element->textContent = $bindValue;
break;
case "html":
case "innerhtml":
case "inner-html":
$element->innerHTML = $bindValue;
break;
case "class":
if($modifier) {
$this->handleModifier(
$element,
"class",
$modifier,
$bindValue
);
}
else {
$element->classList->add($bindValue);
}
break;
case "table":
$this->tableBinder->bindTableData(
$bindValue,
$element,
$element->getAttribute("data-bind:$bindProperty")
);
break;
case "value":
$element->value = $bindValue;
break;
case "list";
$this->listBinder->bindListData($bindValue, $element);
break;
case "remove":
$remove = $bindValue;
if(str_contains($modifier, "!")) {
$remove = !$remove;
}
if($remove) {
$element->remove();
}
break;
default:
if($modifier) {
$this->handleModifier(
$element,
$bindProperty,
$modifier,
$bindValue
);
}
else {
$element->setAttribute($bindProperty, $bindValue);
}
break;
}
}
private function handleModifier(
Element $element,
string $attribute,
string $modifier,
mixed $bindValue
):void {
$modifierChar = $modifier[0];
$modifierValue = substr($modifier, 1);
if(false !== $spacePos = strpos($modifierValue, " ")) {
$modifierValue = substr($modifierValue, $spacePos + 1);
}
switch($modifierChar) {
case ":":
$tokenList = $this->getTokenList($element, $attribute);
if($bindValue) {
$tokenList->add($modifierValue);
}
else {
$tokenList->remove($modifierValue);
}
break;
case "?":
if($modifierValue[0] === "!") {
$bindValue = !$bindValue;
}
if($bindValue) {
$element->setAttribute($attribute, $bindValue);
}
else {
if(!is_null($bindValue)) {
$element->removeAttribute($attribute);
}
}
}
}
private function getTokenList(
Element $node,
string $attribute
):DOMTokenList {
return DOMTokenListFactory::create(
fn() => explode(" ", $node->getAttribute($attribute) ?? ""),
fn(string...$tokens) => $node->setAttribute($attribute, implode(" ", $tokens)),
);
}
}