forked from Matt-Esch/virtual-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.js
165 lines (138 loc) · 4.21 KB
/
diff.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
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
var createPatch = require("./lib/patch-op")
var isArray = require("./lib/is-array")
var isVDOMNode = require("./lib/is-virtual-dom")
var isVTextNode = require("./lib/is-virtual-text")
var isWidget = require("./lib/is-widget")
module.exports = diff
function diff(a, b) {
var patch = { a: a }
walk(a, b, patch, 0)
return patch
}
function walk(a, b, patch, index) {
if (a === b) {
return b
}
var apply = patch[index]
if (isWidget(a)) {
// Update a widget
apply = appendPatch(apply, createPatch(a, b))
} else if (isWidget(b)) {
// Patch in a new widget
apply = appendPatch(apply, createPatch(a, b))
} else if (isVTextNode(a) && isVTextNode(b)) {
// Update a text node
if (a.text !== b.text) {
apply = appendPatch(apply, createPatch(a.text, b.text))
}
} else if (isVDOMNode(a) && isVDOMNode(b) && a.tagName === b.tagName) {
// Update a VDOMNode
var propsPatch = diffProps(a.properties, b.properties)
if (propsPatch) {
apply = appendPatch(apply, createPatch(a.properties, propsPatch))
}
apply = diffChildren(a, b, patch, apply, index)
} else if (a !== b) {
apply = appendPatch(apply, createPatch(a, b))
// We must detect a remove/replace of widgets here so that
// we can add patch records for any stateful widgets
if (isVDOMNode(a) && a.hasWidgets &&
(!isVDOMNode(b) || a.tagName !== b.tagName)) {
destroyWidgets(a, patch, index)
}
}
if (apply) {
patch[index] = apply
}
}
var nullProps = {}
function diffProps(a, b) {
var diff
for (var aKey in a) {
if (aKey === "style") {
var styleDiff = diffProps(a.style, b.style || nullProps)
if (styleDiff) {
diff = diff || {}
diff.style = styleDiff
}
} else {
var aValue = a[aKey]
var bValue = b[aKey]
if (typeof aValue === "function" || aValue !== bValue) {
diff = diff || {}
diff[aKey] = bValue
}
}
}
for (var bKey in b) {
if (!(bKey in a)) {
diff = diff || {}
diff[bKey] = b[bKey]
}
}
return diff
}
function diffChildren(a, b, patch, apply, index) {
var aChildren = a.children
var bChildren = b.children
var aLen = aChildren.length
var bLen = bChildren.length
var len = aLen < bLen ? aLen : bLen
for (var i = 0; i < len; i++) {
var leftNode = aChildren[i]
var rightNode = bChildren[i]
index += 1
walk(leftNode, rightNode, patch, index)
if (isVDOMNode(leftNode) && leftNode.count) {
index += leftNode.count
}
}
// Excess nodes in a need to be removed
for (; i < aLen; i++) {
var excess = aChildren[i]
index += 1
patch[index] = createPatch(excess, null)
destroyWidgets(excess, patch, index)
if (isVDOMNode(excess) && excess.count) {
index += excess.count
}
}
// Excess nodes in b need to be added
for (; i < bLen; i++) {
var addition = bChildren[i]
apply = appendPatch(apply, createPatch(null, addition))
}
return apply
}
// Patch records for all destroyed widgets must be added because we need
// a DOM node reference for the destroy function
function destroyWidgets(vNode, patch, index) {
if (isWidget(vNode)) {
if (typeof vNode.destroy === "function") {
patch[index] = createPatch(vNode, null)
}
} else if (isVDOMNode(vNode) && vNode.hasWidgets) {
var children = vNode.children
var len = children.length
for (var i = 0; i < len; i++) {
var child = children[i]
index += 1
destroyWidgets(child, patch, index)
if (isVDOMNode(child) && child.count) {
index += child.count
}
}
}
}
function appendPatch(apply, patch) {
if (apply) {
if (isArray(apply)) {
apply.push(patch)
} else {
apply = [apply, patch]
}
return apply
} else {
return patch
}
}