-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.html
291 lines (265 loc) · 9.63 KB
/
diff.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/@vue/[email protected]/dist/reactivity.global.js"></script>
</head>
<body>
<div id="app"></div>
</body>
<script>
// 简单diff算法
const vnode = {
type: 'h1',
props: {
'class': 'box'
},
children: 'hello'
}
function createRenderer() {
const patchChildren = (n1, n2, container) => { // 更新children
if (typeof n2.children === 'string') { // 新子节点是文本子节点
if (Array.isArray(n1.children)) {
n1.children.forEach(c => unmount(c)) // 逐个卸载旧子节点
}
setElementText(container, n2.children)
} else if (Array.isArray(n2.children)) { // 新子节点是一组子节点
if (Array.isArray(n1.children)) {
// 新旧节点都是一组子节点,这里涉及核心的diff算法
// n1.children.forEach(c => unmount(c)) // 先卸载所有的旧节点
// n2.children.forEach(c => patch(null, c, container)) // 然后挂载所有的新节点
const oldChildren = n1.children
const newChildren = n2.children
// for (let i = 0; i < oldChildren.length; i++) {
// // 调用patch函数逐个更新子节点
// patch(oldChildren[i], newChildren[i])
// }
// const oldLen = oldChildren.length
// const newLen = newChildren.length
// const commonLength = Math.min(oldLen, newLen) // 找出公共长度,也就是较短的那一组子节点长度
// for (let i = 0; i < commonLength; i++) {
// patch(oldChildren[i], newChildren[i], container)
// }
// if (newLen > oldLen) { // 说明有新的子节点需要进行挂载
// for (let i = commonLength; i < newLen; i++) {
// patch(null, newChildren[i], container)
// }
// } else if (oldLen > newLen) { // 说明有旧的子节点需要进行卸载
// for (let i = commonLength; i < oldLen; i++) {
// unmount(oldChildren[i])
// }
// }
let lastIndex = 0 // 用来存储最大索引值
for (let i = 0; i < newChildren.length; i++) {
const newVNode = newChildren[i]
let find = false
for (let j = 0; j < oldChildren.length; j++) {
const oldVNode = oldChildren[j]
if (newVNode.key === oldVNode.key) {
find = true
// 在key相同的情况下,仍需要对这两个虚拟节点进行打补丁操作
patch(oldVNode, newVNode, container)
if (j < lastIndex) {
// 该节点需要进行移动
const prevVNode = newChildren[i-1]
if (prevVNode) {
const anchor = prevVNode.el.nextSibling
insert(newVNode.el, container, anchor)
}
} else {
lastIndex = j
}
break
}
}
if (!false) {
// 表示该节点是新增节点
const prevVNode = newChildren[i-1]
let anchor = null
if (prevVNode) {
anchor = prevVNode.el.nextSibling
} else {
anchor = container.firstChild
}
patch(null, newVNode, container, anchor)
}
}
for (let i = 0; i < oldChildren.length; i++) { // 寻找是否需要移除的节点
const oldVNode = oldChildren[i]
const has = newChildren.find(vnode => vnode.key === oldVNode.key)
if (!has) {
unmount(oldVNode)
}
}
} else {
// 旧节点要么是文本子节点,要么不存在
setElementText(container, '') // 先清空容器
n2.children.forEach(c => patch(null, c, container)) // 再将新的一组子节点逐个进行挂载
}
} else {
// 新子节点不存在
if (Array.isArray(n1.children)) {
n1.children.forEach(c => unmount(c)) // 旧子节点是一组,逐个进行卸载
} else if (typeof n1.children === 'string') {
setElementText(container, '') // 旧子节点是文本,直接进行清空即可
}
}
}
const patchElement = (n1, n2) => { // 打补丁(更新)
const el = n2.el = n1.el
const oldProps = n1.props
const newProps = n2.props
// 第一步:更新这个props
for (const key in newProps) {
if (newProps[key] !== oldProps[key]) {
patchProps(el, key, oldProps[key], newProps[key])
}
}
for (const key in oldProps) {
if (!(key in newProps)) {
patchProps(el, key, oldProps[key], null)
}
}
// 第二步:更新children
patchChildren(n1, n2, el)
}
const shouldSetAsProps = (el, key) => {
// 判断dom properties是否是只读,如果是只读通过setAttribute来设置属性
if (key === 'form' && el.tagName === 'INPUT') {
return false
}
return key in el
}
const mountElement = (vnode, container, anchor) => { // 第一次挂载过程
const el = vnode.el = createElement(vnode.type) // 让vnode.el引用真实dom元素
if (vnode.props) { // 处理vnode中的props
for (const key in vnode.props) {
// el.setAttribute(key, vnode.props[key]) // 直接设置有缺陷
patchProps(el, key, null, vnode.props[key])
}
}
if (typeof vnode.children === 'string') { // 处理vnode中的children
setElementText(el, vnode.children)
} else if (Array.isArray(vnode.children)) {
vnode.children.forEach(child => {
patch(null, child, el)
})
}
insert(el, container, anchor)
}
const patch = (n1, n2, container, anchor) => {
if (n1 && n1.type !== n2.type) { //如果新旧node的type不同,直接卸载旧的node
unmount(n1)
n1 = null
}
const { type } = n2
if (type === 'string') { // 普通标签元素
if (!n1) { // 挂载
mountElement(n2, container, anchor)
} else { // 打补丁(更新)
patchElement(n1, n2)
}
} else if (type === 'object') { // 组件
} else if (type === Text) { // 文本节点
if (!n1) {
const el = n2.el = createText(n2.children)
insert(el, container)
} else {
const el = n2.el = n1.el
if (n2.children !== n1.children) {
setText(el, n2.children)
}
}
} else if (type === Fragment) { // 片段
if (!n1) {
n2.children.forEach(c => patch(null, c, container))
} else {
patchChildren(n1, n2, container)
}
}
}
const unmount = (vnode) => { // 卸载操作
if (vnode.type === Fragment) {
vnode.children.forEach(c => unmount(c))
return
}
const parent = vnode.el.parent
if (parent) parent.removeChild(vnode.el)
}
const render = (vnode, container) => {
if (vnode) {
patch(container._vnode, vnode, container) // 挂载或更新
} else {
if (container._vnode) {
// container.innerHTML = '' // 卸载,这样的卸载操作有缺陷
unmount(container._vnode)
}
}
container._vnode = vnode
}
return {
render
}
}
const renderer = createRenderer(
{
createElement(tag) {
return document.createElement(tag)
},
createText(text) {
return document.createTextNode(text)
},
setText(el, text) {
el.nodeValue = text
},
setElementText(el, text) {
el.textContent = text
},
insert(el, parent, anchor = null) {
parent.insertBefore(el, anchor)
},
patchProps(el, key, preValue, nextValue) { // 用来对dom对象上的props进行更新操作
if (/^on/.test(key)) { // 对事件进行处理
let invokers = el._vei || (el._vei = {})
const invoker = invokers[key]
const name = key.slice(2).toLowerCase()
if (nextValue) {
if (!invoker) { // 添加绑定
invoker = el._vei[key] = (e) => { // 伪造事件处理函数
if (e.timestamp < invoker.attached) return;
if (Array.isArray(invoker.value)) { // 判断同一个事件绑定多个处理函数
invoker.value.forEach(fn => fn(e))
} else {
invoker.value(e)
}
}
invoker.value = nextValue
invoker.attached = performance.now()
el.addEventListener(name, invoker)
} else { // 更新,直接更新invoker的value值即可
invoker.value = nextValue
}
} else if (invoker) { // 卸载
el.removeEventListener(name, invoker)
}
} else if (key === 'class') { // 对class做特殊处理, className的性能比setAttribute好得多
el.className = nextValue || ''
} else if (shouldSetAsProps(el, key)) {
const type = typeof el[key]
if (type === 'boolean' && nextValue === '') {
el[key] = true
} else {
el[key] = nextValue
}
} else {
el.setAttribute(key, nextValue)
}
}
}
)
renderer.render(vnode, document.getElementById('app'))
</script>
</html>