/
tree.js
73 lines (69 loc) · 1.64 KB
/
tree.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
/**
* Tree 类,元素节点生成 dom-tree
*/
class Tree {
constructor(tagName, props, children){
this.tagName = tagName,
this.props = props || {}
this.children = children || []
this.count = 0 // 子节点数量
this.key = props ? props.key : void 999
if(Array.isArray(this.props)) {
this.children = this.props
this.props = {}
}
this.each(this.children)
}
// each child
each(children) {
children.forEach((child, i) => {
if (child instanceof Tree) {
this.count += child.count
} else {
children[i] = '' + child
}
this.count++
})
}
setAttr(node, key, value) {
switch (key) {
case 'style':
node.style.cssText = value
break
case 'value':
let tagName = node.tagName || ''
tagName = tagName.toLowerCase()
if (
tagName === 'input' || tagName === 'textarea'
) {
node.value = value
} else {
// if it is not a input or textarea, use `setAttribute` to set
node.setAttribute(key, value)
}
break
default:
node.setAttribute(key, value)
break
}
}
// 生成 virtual-dom
render() {
let ele = document.createElement(this.tagName)
for( let key in this.props) {
this.setAttr(ele, key, this.props[key])
}
let children = this.children.map((child) => {
if (child instanceof Tree) {
ele.appendChild(this.render.call(child))
} else {
ele.appendChild(document.createTextNode(child))
}
})
return ele
}
}
function t() {
return new Tree(...arguments)
}
export default t