forked from WebReflection/hyperHTML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperhtml.js
More file actions
212 lines (196 loc) · 4.85 KB
/
Copy pathhyperhtml.js
File metadata and controls
212 lines (196 loc) · 4.85 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
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
var hyperHTML = (function () {'use strict';
/*! (C) 2017 Andrea Giammarchi @WebReflection (MIT) */
// DOM parsing & traversing
function attributesSeeker(node, actions) {
for (var
attribute,
attributes = node.attributes,
i = 0,
length = attributes.length;
i < length; i++
) {
attribute = attributes[i];
if (attribute.value === uidc) {
actions.push(setAttribute(node, attribute));
}
}
}
function lukeTreeWalker(node, actions) {
for (var
child, text,
childNodes = slice.call(node.childNodes),
length = childNodes.length,
i = 0; i < length; i++
) {
child = childNodes[i];
switch (child.nodeType) {
case 1:
attributesSeeker(child, actions);
lukeTreeWalker(child, actions);
break;
case 8:
if (child.textContent === uid) {
if (length === 1) {
actions.push(setAnyContent(node));
node.removeChild(child);
} else {
text = node.ownerDocument.createTextNode('');
node.replaceChild(text, child);
actions.push(setTextContent(text));
}
}
break;
}
}
}
// DOM manipulating
function setAnyContent(node) {
return function any(value) {
switch (typeof value) {
case 'string':
node.innerHTML = value;
break;
case 'number':
case 'boolean':
node.textContent = value;
break;
default:
if (Array.isArray(value)) {
if (value.length === 0) {
any(value[0]);
} else if(typeof value[0] === 'string') {
any(value.join(''));
} else {
if (!sameList(node.childNodes, value)) {
any(populateFragment(
node.ownerDocument.createDocumentFragment(),
value
));
}
}
} else {
populateNode(node, value);
}
break;
}
};
}
function setAttribute(node, attribute) {
var
name = attribute.name,
onStuff = !name.indexOf('on')
;
if (onStuff) node.removeAttribute(name);
return onStuff ?
function event(value) {
node[name] = value;
} :
function attr(value) {
attribute.value = value;
};
}
function setTextContent(node) {
return function text(value) {
node.textContent = value;
};
}
// Helpers
function populateFragment(f, nodes) {
for (var
i = 0,
length = nodes.length;
i < length; i++
) {
f.appendChild(nodes[i]);
}
return f;
}
function populateNode(parent, child) {
switch (child.nodeType) {
case 1:
var childNodes = parent.childNodes;
if (childNodes.length !== 1 || childNodes[0] !== child) {
resetAndPopulate(parent, child);
}
break;
case 11:
if (!sameList(parent.childNodes, child.childNodes)) {
resetAndPopulate(parent, child);
}
break;
case 3:
parent.textContent = child.textContent;
break;
}
}
function resetAndPopulate(parent, child) {
parent.textContent = '';
parent.appendChild(child);
}
function sameList(a, b) {
if (a === b) return true;
var
i = 0,
aLength = a.length
;
if (aLength !== b.length) return false;
while (i < aLength) {
if (a[i] !== b[i]) return false;
i++;
}
return true;
}
// Template setup
function update(statics) {
for (var
i = 1,
length = statics.length,
updates = this[EXPANDO].u;
i < length; i++
) {
updates[i - 1](arguments[i]);
}
return this;
}
function upgrade(statics) {
for (var
template,
updates = [],
html = [statics[0]],
i = 1,
length = statics.length;
i < length; i++
) {
html.push(uidc, statics[i]);
}
if (this.nodeType === 1) {
this.innerHTML = html.join('');
} else {
template = this.ownerDocument.createElement('template');
template.innerHTML = html.join('');
populateFragment(
this,
slice.call((template.content || template).childNodes)
);
}
lukeTreeWalker(this, updates);
this[EXPANDO] = {s: statics, u: updates};
return update.apply(this, arguments);
}
// local variables
var
EXPANDO = '_hyperHTML',
uid = EXPANDO + ((Math.random() * new Date) | 0),
uidc = '<!--' + uid + '-->',
slice = [].slice
;
// hyperHTML \o/
return function hyperHTML(statics) {
return EXPANDO in this &&
this[EXPANDO].s === statics ?
update.apply(this, arguments) :
upgrade.apply(this, arguments);
};
}());
// umd.KISS
try { module.exports = hyperHTML; } catch(o_O) {}