-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.go
250 lines (201 loc) · 4.84 KB
/
diff.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
package diff
import (
"golang.org/x/net/html"
"strconv"
)
type Type int
func (dt Type) toString() string {
return strconv.Itoa(int(dt))
}
const (
Append Type = iota
Remove
SetInnerHTML
SetAttr
RemoveAttr
Replace
Move
)
type changeInstruction struct {
changeType Type
element *html.Node
content string
attr attrChange
index int
}
// attrChange todo
type attrChange struct {
name string
value string
}
type diff struct {
actual *html.Node
instructions []changeInstruction
quantity int
doneElements []*html.Node
}
func newDiff(actual *html.Node) *diff {
return &diff{
actual: actual,
instructions: make([]changeInstruction, 0),
}
}
func (d *diff) instructionsByType(t Type) []changeInstruction {
s := make([]changeInstruction, 0)
for _, i := range d.instructions {
if i.changeType == t {
s = append(s, i)
}
}
return s
}
func (d *diff) checkpoint() {
d.quantity = len(d.instructions)
}
// Has changed since last checkpoint
func (d *diff) hasChanged() bool {
return len(d.instructions) != d.quantity
}
func (d *diff) propose(proposed *html.Node) {
d.clearMarked()
d.diffNode(d.actual, proposed)
}
func (d *diff) diffNode(actual, proposed *html.Node) {
if actual == nil || proposed == nil {
d.diffWalk(actual, proposed)
return
}
uidActual, actualOk := getLiveUidAttributeValue(actual)
uidProposed, proposedOk := getLiveUidAttributeValue(proposed)
if actualOk && proposedOk && uidActual != uidProposed {
content, _ := renderNodeToString(proposed)
d.instructions = append(d.instructions, changeInstruction{
changeType: Replace,
element: actual,
content: content,
})
return
}
d.diffNodeAttributes(actual, proposed)
d.diffWalk(actual.FirstChild, proposed.FirstChild)
d.markNodeDone(proposed)
}
func (d *diff) clearMarked() {
d.doneElements = make([]*html.Node, 0)
}
func (d *diff) markNodeDone(node *html.Node) {
d.doneElements = append(d.doneElements, node)
}
func (d *diff) isMarked(node *html.Node) bool {
for _, n := range d.doneElements {
if n == node {
return true
}
}
return false
}
func (d *diff) diffWalk(actual, proposed *html.Node) {
if actual == nil && proposed == nil {
return
}
if nodeIsText(actual) || nodeIsText(proposed) {
d.checkpoint()
d.diffTextNode(actual, proposed)
if d.hasChanged() {
return
}
}
if actual != nil && proposed != nil {
d.diffNode(actual, proposed)
} else if actual == nil && nodeIsElement(proposed) {
nodeContent, _ := renderNodeToString(proposed)
d.instructions = append(d.instructions, changeInstruction{
changeType: Append,
element: proposed.Parent,
content: nodeContent,
})
d.markNodeDone(proposed)
} else if proposed == nil && nodeIsElement(actual) {
d.instructions = append(d.instructions, changeInstruction{
changeType: Remove,
element: actual,
})
d.markNodeDone(actual)
}
nextActual := nextRelevantElement(actual)
nextProposed := nextRelevantElement(proposed)
if nextActual != nil || nextProposed != nil {
d.diffWalk(nextActual, nextProposed)
}
}
func (d *diff) forceRenderElementContent(proposed *html.Node) {
childrenHTML, _ := renderInnerHTML(proposed)
d.instructions = append(d.instructions, changeInstruction{
changeType: SetInnerHTML,
content: childrenHTML,
element: proposed,
})
}
// diffNodeAttributes compares the attributes in el to the attributes in otherEl
// and adds the necessary patches to make the attributes in el match those in
// otherEl
func (d *diff) diffNodeAttributes(actual, proposed *html.Node) {
actualAttrs := AttrMapFromNode(actual)
proposedAttrs := AttrMapFromNode(proposed)
// Now iterate through the attributes in otherEl
for name, otherValue := range proposedAttrs {
value, found := actualAttrs[name]
if !found || value != otherValue {
d.instructions = append(d.instructions, changeInstruction{
changeType: SetAttr,
element: actual,
attr: attrChange{
name: name,
value: otherValue,
},
})
}
}
for attrName := range actualAttrs {
if _, found := proposedAttrs[attrName]; !found {
d.instructions = append(d.instructions, changeInstruction{
changeType: RemoveAttr,
element: actual,
attr: attrChange{
name: attrName,
},
})
}
}
}
func (d *diff) diffTextNode(actual, proposed *html.Node) {
// Any node is text
if !nodeIsText(proposed) && !nodeIsText(actual) {
return
}
proposedIsRelevant := nodeRelevant(proposed)
actualIsRelevant := nodeRelevant(actual)
if !proposedIsRelevant && !actualIsRelevant {
return
}
// XOR
if proposedIsRelevant != actualIsRelevant {
goto renderEntireNode
}
if proposed.Data != actual.Data {
goto renderEntireNode
}
return
renderEntireNode:
{
node := proposed
if node == nil {
node = actual
}
if node == nil {
return
}
d.forceRenderElementContent(node.Parent)
d.markNodeDone(node.Parent)
}
}