-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.js
62 lines (58 loc) · 1.97 KB
/
utils.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
function assert(condition) {
if (!condition) {
console.error('assertion failed');
console.trace();
//var keepGoing = 0;
debugger;
//if (!keepGoing)
// throw new Error(message);
}
}
function F( /* varargs... */) {
var fragment = document.createDocumentFragment();
for (var index = 0; index < arguments.length; index += 1) {
if (arguments[index] instanceof Array) {
fragment.appendChild(F.apply(this, arguments[index]));
} else if (typeof arguments[index] == 'string') {
fragment.appendChild(document.createTextNode(arguments[index]));
} else {
assert(arguments[index] instanceof Node);
fragment.appendChild(arguments[index]);
}
}
return fragment;
}
function E(name, /* optional */ attributes /*, varargs... */) {
var element = document.createElement(name);
var index = 1;
if ((arguments.length > 1) && (typeof attributes != 'string') &&
(!(attributes instanceof Node)) && (!(attributes instanceof Array))) {
for (var attName in attributes) {
if (typeof attributes[attName] == 'boolean') {
if (attributes[attName])
element.setAttribute(attName, '');
} else if (typeof attributes[attName] == 'function') {
element[attName] = attributes[attName];
} else {
element.setAttribute(attName, attributes[attName]);
}
}
index = 2;
}
for (; index < arguments.length; index += 1) {
if (arguments[index] instanceof Array) {
element.appendChild(F.apply(this, arguments[index]));
} else if (typeof arguments[index] == 'string') {
element.appendChild(document.createTextNode(arguments[index]));
} else {
assert(arguments[index] instanceof Node);
element.appendChild(arguments[index]);
}
}
return element;
}
function replaceChildren(parent, children) { // don't forget children is an **array**!
parent.textContent = '';
for (var index = 0; index < children.length; index += 1)
parent.appendChild(children[index]);
}