-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
96 lines (87 loc) · 2.19 KB
/
Copy pathbuild.js
File metadata and controls
96 lines (87 loc) · 2.19 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
define(['./create'], function (create) {
'use strict';
var textTypes = {string: 1, number: 1, boolean: 1};
function build (vdom, parent, options) {
var doc = options && options.document || document,
stack = [parent, vdom, null], node;
if (parent) {
if (parent.nodeType === 9) {
doc = parent;
parent = null;
} else {
doc = parent.ownerDocument || doc;
}
}
while (stack.length) {
var ns = stack.pop(), element = stack.pop();
parent = stack.pop();
node = null;
// deref element
while (typeof element == 'function') {
element = element(options);
}
if (!(element instanceof Array)) {
// make a specialty node
if (textTypes[typeof element] || element instanceof Date || element instanceof RegExp) {
// text
node = doc.createTextNode(element.toString());
} else if (!element) {
// skip
} else if (typeof element.appendChild == 'function') {
// node
node = element;
} else if (parent && typeof element == 'object') {
// attributes
create.setAttrs(parent, element, options);
}
// add it to a parent
if (node && parent) {
parent.appendChild(node);
}
continue;
}
// array: element or children
var tag = element[0];
// deref tag
while (typeof tag == 'function') {
tag = tag(options);
}
// make a node
if (typeof tag == 'string') {
// tag
node = create(tag, null, doc, ns, options);
} else if (tag && typeof tag.appendChild == 'function') {
// node
node = tag;
tag = node.tagName;
} else if (tag instanceof Array) {
// children
if (element.length > 1 && !parent) {
parent = doc.createDocumentFragment();
}
node = parent;
}
var from = 0, i;
if (node && node !== parent) {
// redefine a default namespace for children
switch (tag.toLowerCase()) {
case 'svg':
ns = 'svg';
break;
case 'foreignobject':
ns = null;
break;
}
// add children
stack.push(parent, node, ns);
from = 1;
}
// add children to the stack in the reverse order
for(i = element.length; i > from;) {
stack.push(node, element[--i], ns);
}
}
return parent || node;
}
return build;
});