-
Notifications
You must be signed in to change notification settings - Fork 5
/
patch.go
257 lines (230 loc) · 5.51 KB
/
patch.go
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
package domui
import (
"strings"
"syscall/js"
)
func patch(
scope Scope,
node *Node,
lastElement js.Value,
lastNode *Node,
) (
element js.Value,
err error,
) {
defer he(&err)
if lastElement.IsUndefined() {
panic("bad last element")
}
if lastNode != nil && lastNode == node {
// same node, same element
return lastElement, nil
}
replace := func(lastNode *Node) (err error) {
defer he(&err)
// replace element with newly created one
element, err = node.ToElement(scope)
ce(err)
parent := lastElement.Get("parentNode")
parent.Call("insertBefore", element, lastElement)
lastElement.Call("remove")
unsetEventSpecs(lastElement)
return nil
}
if lastNode == nil {
// not patchable
ce(replace(lastNode))
return
}
if node.Kind != lastNode.Kind {
// not patchable
ce(replace(lastNode))
return
}
switch node.Kind {
case TagNode:
if node.Text != lastNode.Text {
// not patchable
ce(replace(lastNode))
return
}
case TextNode:
element = lastElement
if node.Text != lastNode.Text {
element.Set("data", node.Text)
}
return
}
// patchable
element = lastElement
// child nodes
elementChildren := element.Get("childNodes")
childNodes := node.childNodes
lastChildNodes := lastNode.childNodes
hasFocus := false
for node := document.Get("activeElement"); !node.IsNull() && !node.IsUndefined() && !node.Equal(body); node = node.Get("parentNode") {
if node.Equal(element) {
hasFocus = true
break
}
}
for i, childNode := range childNodes {
if i < len(lastChildNodes) {
childElement := elementChildren.Index(i)
hasScrollBar := false
if childElement.InstanceOf(htmlElement) {
hasScrollBar = hasScrollBar ||
childElement.Get("scrollWidth").Int() > childElement.Get("clientWidth").Int()
hasScrollBar = hasScrollBar ||
childElement.Get("scrollHeight").Int() > childElement.Get("clientHeight").Int()
}
if !hasFocus && !hasScrollBar &&
len(lastChildNodes) < len(childNodes) {
// insert
childElement, err := childNode.ToElement(scope)
ce(err)
element.Call(
"insertBefore",
childElement,
elementChildren.Index(i),
)
lastChildNodes = append(
lastChildNodes[:i],
append([]*Node{nil}, lastChildNodes[i:]...)...,
) // insert placeholder
} else {
// replace
_, err := patch(
scope,
childNode,
childElement,
lastChildNodes[i],
)
ce(err)
}
} else {
// append
childElement, err := childNode.ToElement(scope)
ce(err)
element.Call("appendChild", childElement)
}
}
if n := len(lastChildNodes) - len(childNodes); n > 0 {
for i := 0; i < n; i++ {
lastChild := element.Get("lastChild")
lastChild.Call("remove")
unsetEventSpecs(lastChild)
}
}
// id
if node.ID != lastNode.ID {
if node.ID == "" {
element.Call("removeAttribute", "id")
element.Delete("id")
} else {
element.Set("id", node.ID)
element.Call("setAttribute", "id", node.ID)
}
}
// style
if node.Style != lastNode.Style {
element.Set("style", node.Style)
}
// styles
style := element.Get("style")
// must do removing before adding, since different attributes may affect the same style
// for example, adding `padding: 1px` then removing `padding-bottom` results to `1px 1px 0 1px` wrongly
for _, item := range lastNode.Styles {
if node.Styles != nil {
if _, ok := node.Styles.Get(item.Key); !ok {
style.Set(item.Key, nil)
}
} else {
style.Set(item.Key, nil)
}
}
for _, item := range node.Styles {
if lastNode.Styles != nil {
if v, ok := lastNode.Styles.Get(item.Key); !ok ||
v != item.Value ||
// always set animation properties
strings.HasPrefix(item.Key, "animation") {
key := item.Key
if strings.HasSuffix(key, "|reset") {
// reset
key = strings.TrimSuffix(key, "|reset")
style.Set(key, nil)
element.Get("offsetHeight") // trigger reflow
}
style.Set(key, item.Value)
}
} else {
style.Set(item.Key, item.Value)
}
}
if node.Style == "" && len(node.Styles) == 0 {
element.Call("removeAttribute", "style")
element.Delete("style")
}
// classes
list := element.Get("classList")
if len(node.Classes) > 0 {
for _, item := range node.Classes {
if lastNode.Classes != nil {
if _, ok := lastNode.Classes.Get(item.Key); !ok {
list.Call("add", item.Key)
}
} else {
list.Call("add", item.Key)
}
}
for _, item := range lastNode.Classes {
if node.Classes != nil {
if _, ok := node.Classes.Get(item.Key); !ok {
list.Call("remove", item.Key)
}
} else {
list.Call("remove", item.Key)
}
}
} else {
element.Call("removeAttribute", "class")
element.Delete("class")
}
// attrs
for _, item := range node.Attributes {
if lastNode.Attributes != nil {
if v, ok := lastNode.Attributes.Get(item.Key); !ok || v != item.Value {
element.Call("setAttribute", item.Key, item.Value)
element.Set(item.Key, item.Value)
}
} else {
element.Call("setAttribute", item.Key, item.Value)
element.Set(item.Key, item.Value)
}
}
for _, item := range lastNode.Attributes {
if node.Attributes != nil {
if _, ok := node.Attributes.Get(item.Key); !ok {
element.Call("removeAttribute", item.Key)
element.Delete(item.Key)
}
} else {
element.Call("removeAttribute", item.Key)
element.Delete(item.Key)
}
}
// events
if len(node.Events) > 0 {
var app *App
scope.Assign(&app)
setEventSpecs(app.wrapElement, element, node.Events)
} else {
unsetEventSpecs(element)
}
// focus
if node.Focus {
element.Call("focus")
}
return
}