-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tagger.js
305 lines (287 loc) 路 8.44 KB
/
tagger.js
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
import createContent from '@ungap/create-content';
import domdiff from 'domdiff';
import domtagger from 'domtagger';
import hyperStyle from 'hyperhtml-style';
import {wireType, isArray} from './shared.js';
// returns nodes from wires and components
const asNode = (item, i) => item.nodeType === wireType ?
(
(1 / i) < 0 ?
(i ? item.remove(true) : item.lastChild) :
(i ? item.valueOf(true) : item.firstChild)
) :
item
;
// returns true if domdiff can handle the value
const canDiff = value => 'ELEMENT_NODE' in value;
// generic attributes helpers
const hyperAttribute = (node, original) => {
let oldValue;
let owner = false;
const attribute = original.cloneNode(true);
return newValue => {
if (oldValue !== newValue) {
oldValue = newValue;
if (attribute.value !== newValue) {
if (newValue == null) {
if (owner) {
owner = false;
node.removeAttributeNode(attribute);
}
attribute.value = newValue;
} else {
attribute.value = newValue;
if (!owner) {
owner = true;
node.setAttributeNode(attribute);
}
}
}
}
};
};
// events attributes helpers
const hyperEvent = (node, name) => {
let oldValue;
let type = name.slice(2);
if (name.toLowerCase() in node)
type = type.toLowerCase();
return newValue => {
if (oldValue !== newValue) {
if (oldValue)
node.removeEventListener(type, oldValue, false);
oldValue = newValue;
if (newValue)
node.addEventListener(type, newValue, false);
}
};
};
// special attributes helpers
const hyperProperty = (node, name) => {
let oldValue;
return newValue => {
if (oldValue !== newValue) {
oldValue = newValue;
if (node[name] !== newValue) {
if (newValue == null) {
// cleanup before dropping the attribute to fix IE/Edge gotcha
node[name] = '';
node.removeAttribute(name);
} else
node[name] = newValue;
}
}
};
};
// special hooks helpers
const hyperRef = node => {
return ref => {
ref.current = node;
};
};
const hyperSetter = (node, name, svg) => svg ?
value => {
try {
node[name] = value;
}
catch (nope) {
node.setAttribute(name, value);
}
} :
value => {
node[name] = value;
};
// list of attributes that should not be directly assigned
const readOnly = /^(?:form|list)$/i;
// reused every slice time
const slice = [].slice;
// simplifies text node creation
const text = (node, text) => node.ownerDocument.createTextNode(text);
export function Tagger(type) {
this.type = type;
return domtagger(this);
};
Tagger.prototype = {
// there are four kind of attributes, and related behavior:
// * events, with a name starting with `on`, to add/remove event listeners
// * special, with a name present in their inherited prototype, accessed directly
// * regular, accessed through get/setAttribute standard DOM methods
// * style, the only regular attribute that also accepts an object as value
// so that you can style=${{width: 120}}. In this case, the behavior has been
// fully inspired by Preact library and its simplicity.
attribute(node, name, original) {
const isSVG = this.type === 'svg';
switch (name) {
case 'class':
if (isSVG)
return hyperAttribute(node, original);
name = 'className';
case 'data':
case 'props':
return hyperProperty(node, name);
case 'style':
return hyperStyle(node, original, isSVG);
case 'ref':
return hyperRef(node);
default:
if (name.slice(0, 1) === '.')
return hyperSetter(node, name.slice(1), isSVG);
if (name.slice(0, 2) === 'on')
return hyperEvent(node, name);
if (name in node && !(isSVG || readOnly.test(name)))
return hyperProperty(node, name);
return hyperAttribute(node, original);
}
},
// in a hyper(node)`<div>${content}</div>` case
// everything could happen:
// * it's a JS primitive, stored as text
// * it's null or undefined, the node should be cleaned
// * it's a promise, update the content once resolved
// * it's an explicit intent, perform the desired operation
// * it's an Array, resolve all values if Promises and/or
// update the node with the resulting list of content
any(node, childNodes) {
const diffOptions = {node: asNode, before: node};
const {type} = this;
let fastPath = false;
let oldValue;
const anyContent = value => {
switch (typeof value) {
case 'string':
case 'number':
case 'boolean':
if (fastPath) {
if (oldValue !== value) {
oldValue = value;
childNodes[0].textContent = value;
}
} else {
fastPath = true;
oldValue = value;
childNodes = domdiff(
node.parentNode,
childNodes,
[text(node, value)],
diffOptions
);
}
break;
case 'function':
anyContent(value(node));
break;
case 'object':
case 'undefined':
if (value == null) {
fastPath = false;
childNodes = domdiff(
node.parentNode,
childNodes,
[],
diffOptions
);
break;
}
default:
fastPath = false;
oldValue = value;
if (isArray(value)) {
if (value.length === 0) {
if (childNodes.length) {
childNodes = domdiff(
node.parentNode,
childNodes,
[],
diffOptions
);
}
} else {
switch (typeof value[0]) {
case 'string':
case 'number':
case 'boolean':
anyContent(String(value));
break;
case 'function':
anyContent(value.map(invoke, node));
break;
case 'object':
if (isArray(value[0])) {
value = value.concat.apply([], value);
}
default:
childNodes = domdiff(
node.parentNode,
childNodes,
value,
diffOptions
);
break;
}
}
} else if (canDiff(value)) {
childNodes = domdiff(
node.parentNode,
childNodes,
value.nodeType === 11 ?
slice.call(value.childNodes) :
[value],
diffOptions
);
} else if ('text' in value) {
anyContent(String(value.text));
} else if ('any' in value) {
anyContent(value.any);
} else if ('html' in value) {
childNodes = domdiff(
node.parentNode,
childNodes,
slice.call(
createContent(
[].concat(value.html).join(''),
type
).childNodes
),
diffOptions
);
} else if ('length' in value) {
anyContent(slice.call(value));
}
break;
}
};
return anyContent;
},
// style or textareas don't accept HTML as content
// it's pointless to transform or analyze anything
// different from text there but it's worth checking
// for possible defined intents.
text(node) {
let oldValue;
const textContent = value => {
if (oldValue !== value) {
oldValue = value;
const type = typeof value;
if (type === 'object' && value) {
if ('text' in value) {
textContent(String(value.text));
} else if ('any' in value) {
textContent(value.any);
} else if ('html' in value) {
textContent([].concat(value.html).join(''));
} else if ('length' in value) {
textContent(slice.call(value).join(''));
}
} else if (type === 'function') {
textContent(value(node));
} else {
node.textContent = value == null ? '' : value;
}
}
};
return textContent;
}
};
function invoke(callback) {
return callback(this);
}