-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathvdom.html
167 lines (146 loc) · 4.31 KB
/
vdom.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Renderer Demo</title>
<!-- <script src="https://unpkg.com/vue@next"></script> -->
<style>
.red {
color: red
}
</style>
</head>
<body>
<div id="app"></div>
<script>
const ShapeFlags = {
// html 或 svg 标签
ELEMENT: 1,
// 函数式组件
FUNCTIONAL_COMPONENT: 1 << 1,
// 普通有状态组件
STATEFUL_COMPONENT: 1 << 2,
// 子节点是纯文本
TEXT_CHILDREN: 1 << 3,
// 子节点是数组
ARRAY_CHILDREN: 1 << 4,
// 子节点是 slots
SLOTS_CHILDREN: 1 << 5,
// Portal
PORTAL: 1 << 6,
// Suspense
SUSPENSE: 1 << 7,
// 需要被keepAlive的有状态组件
COMPONENT_SHOULD_KEEP_ALIVE: 1 << 8,
// 已经被keepAlive的有状态组件
COMPONENT_KEPT_ALIVE: 1 << 9,
// 有状态组件和函数式组件都是“组件”,用 COMPONENT 表示
// COMPONENT: ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT
}
ShapeFlags.COMPONENT = ShapeFlags.STATEFUL_COMPONENT | ShapeFlags.FUNCTIONAL_COMPONENT
function h(tag, props = null, children = null) {
let shapeFlag = null
// 这里为了简化,直接这样判断
if (typeof tag === 'string') {
shapeFlag = ShapeFlags.ELEMENT
} else if (typeof tag === 'object') {
shapeFlag = ShapeFlags.STATEFUL_COMPONENT
} else if (typeof tag === 'function') {
shapeFlag = ShapeFlags.FUNCTIONAL_COMPONENT
}
const vnode = {
_isVNode: true,
el: null,
shapeFlag,
tag,
props,
children
}
normalizeChildren(vnode, vnode.children)
return vnode
}
function normalizeChildren(vnode, children) {
let type = 0
if (children == null) {
children = null
} else if (Array.isArray(children)) {
type = ShapeFlags.ARRAY_CHILDREN
} else if (typeof children === 'object') {
type = ShapeFlags.SLOTS_CHILDREN
} else if (typeof children === 'string') {
children = String(children)
type = ShapeFlags.TEXT_CHILDREN
}
vnode.shapeFlag |= type
}
function mount(vnode, container) {
if (vnode.tag === null) {
mountTextElement(vnode, container)
} else if (vnode.shapeFlag & ShapeFlags.ELEMENT) {
mountElement(vnode, container)
} else if (vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
mountStatefulComponent(vnode, container)
} else if (vnode.shapeFlag & ShapeFlags.FUNCTIONAL_COMPONENT) {
mountFunctionalComponent(vnode, container)
}
}
function mountTextElement(vnode, container) {
const el = document.createTextNode(vnode.children)
vnode.el = el
container.appendChild(el)
}
function mountElement(vnode, container) {
const el = document.createElement(vnode.tag)
// props
if (vnode.props) {
for (const key in vnode.props) {
const value = vnode.props[key]
el.setAttribute(key, value)
}
}
// children
if (vnode.children) {
if (typeof vnode.children === 'string') {
el.textContent = vnode.children
} else {
vnode.children.forEach(child => {
mount(child, el)
})
}
}
vnode.el = el
container.appendChild(el)
}
function mountStatefulComponent(vnode, container) {
const instance = vnode.tag
instance.$vnode = instance.render()
mount(instance.$vnode, container)
instance.$el = vnode.el = instance.$vnode.el
}
function mountFunctionalComponent(vnode, container) {
const $vnode = vnode.tag()
mount($vnode, container)
vnode.el = $vnode.el
}
const MyComponent = {
render() {
return h('div', null, 'stateful component')
}
}
function MyFunctionalComponent() {
return h('div', null, 'function component')
}
const vdom = h('div', {
class: 'red'
}, [
h('p', null, 'text children'),
h('p', null, null),
h(MyComponent),
h(MyFunctionalComponent)
])
console.log(vdom);
mount(vdom, document.querySelector("#app"))
</script>
</body>
</html>