forked from albrow/vdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
299 lines (270 loc) · 8.97 KB
/
tree.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
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
292
293
294
295
296
297
298
299
package vdom
import (
"fmt"
"html"
"reflect"
)
// A Tree is a virtual, in-memory representation of a DOM tree
type Tree struct {
// Children is the first-level child nodes for the tree
Children []Node
reader *IndexedByteReader
src []byte
}
// HTML returns the html of this tree and recursively its children
// as a slice of bytes.
func (t *Tree) HTML() []byte {
escaped := string(t.src)
return []byte(html.UnescapeString(escaped))
}
// A Node is an element inside a tree.
type Node interface {
// Parent returns the parent node or nil if there is none
Parent() *Element
// Children returns a slice of child nodes or nil if there
// are none
Children() []Node
// HTML returns the unescaped html of this node and its
// children as a slice of bytes.
HTML() []byte
// Index returns the child indexes starting at the root of the
// virtual tree that can be used to get to this node. So if this
// node is the second child of its parent, and its parent is the first
// child of some root node, Index should return [0, 1]. This means we
// can get to this node via root.ChildNodes()[0].ChildNodes()[1].
Index() []int
}
// Attr is an html attribute
type Attr struct {
Name string
Value string
}
// Element is an html element, e.g., <div></div>. Name does not include the
// <, >, or / symbols.
type Element struct {
Name string
Attrs []Attr
parent *Element
children []Node
tree *Tree
srcStart int
srcEnd int
srcInnerStart int
srcInnerEnd int
autoClosed bool
index []int
}
func (e *Element) Parent() *Element {
return e.parent
}
func (e *Element) Children() []Node {
return e.children
}
func (e *Element) HTML() []byte {
if e.autoClosed {
// If the tag was autoclosed, it has no children. Just construct the html manually
result := []byte(fmt.Sprintf("<%s", e.Name))
for _, attr := range e.Attrs {
result = append(result, []byte(fmt.Sprintf(` %s="%s"`, attr.Name, attr.Value))...)
}
result = append(result, '>')
return result
} else {
escaped := string(e.tree.src[e.srcStart:e.srcEnd])
return []byte(html.UnescapeString(escaped))
}
}
// AttrMap returns this element's attributes as a map
// of attribute name to attribute value
func (e *Element) AttrMap() map[string]string {
m := map[string]string{}
for _, attr := range e.Attrs {
m[attr.Name] = attr.Value
}
return m
}
// InnerHTML returns the unescaped html inside of e. So if e
// is <ul><li>one</li><li>two</li></ul>, it will return
// <li>one</li><li>two</li>. Since Element is the only type that
// can have children, this only makes sense for the Element type.
func (e *Element) InnerHTML() []byte {
if e.autoClosed {
// If the tag was autoclosed, it has no children, and therefore no inner html.
return nil
} else {
escaped := string(e.tree.src[e.srcInnerStart:e.srcInnerEnd])
return []byte(html.UnescapeString(escaped))
}
}
// Selector returns a css selector which can be used to find
// the corresponding element in the actual DOM. The selector
// should be applied to the root of the tree, i.e. the starting
// point for the virtual tree in the actual DOM.
func (e *Element) Selector() string {
// Simply use the index field to construct a selector with nth-child.
selector := fmt.Sprintf("*:nth-child(%d)", e.index[0]+1)
for _, i := range e.index[1:] {
selector += fmt.Sprintf(" > *:nth-child(%d)", i+1)
}
return selector
}
func (e *Element) Index() []int {
return e.index
}
// Compare non-recursively compares e to other. It does not check
// the child nodes since they can be a Node with any underlying type.
// If you want to compare the parent and children fields, use CompareNodes.
func (e *Element) Compare(other *Element, compareAttrs bool) (bool, string) {
if e.Name != other.Name {
return false, fmt.Sprintf("e.Name was %s but other.Name was %s", e.Name, other.Name)
}
if !compareAttrs {
return true, ""
}
attrs := e.Attrs
otherAttrs := other.Attrs
if len(attrs) != len(otherAttrs) {
return false, fmt.Sprintf("n has %d attrs but other has %d attrs.", len(attrs), len(otherAttrs))
}
for i, attr := range attrs {
otherAttr := otherAttrs[i]
if attr != otherAttr {
return false, fmt.Sprintf("e.Attrs[%d] was %s but other.Attrs[%d] was %s", i, attr, i, otherAttr)
}
}
return true, ""
}
// Text is a text node inside an xml/html document, i.e. anything
// not surrounded by tags.
type Text struct {
Value []byte
parent *Element
index []int
}
func (t *Text) Parent() *Element {
return t.parent
}
func (t *Text) Children() []Node {
// A text node can't have any children
return nil
}
func (t *Text) HTML() []byte {
return t.Value
}
func (t *Text) Index() []int {
return t.index
}
// Compare non-recursively compares t to other. It does not check
// the child nodes since they can be a Node with any underlying type.
// If you want to compare the parent and children fields, use CompareNodes.
func (t *Text) Compare(other *Text) (bool, string) {
if string(t.Value) != string(other.Value) {
return false, fmt.Sprintf("t.Value was %s but other.Value was %s", string(t.Value), string(other.Value))
}
return true, ""
}
// Comment is an xml/html comment of the form <!-- value -->.
// Value does not include the <!-- and --> markers.
type Comment struct {
Value []byte
parent *Element
index []int
}
func (c *Comment) Parent() *Element {
return c.parent
}
func (c *Comment) Children() []Node {
// A commet node can't have any children
return nil
}
func (c *Comment) HTML() []byte {
// Re-add the open and close for the tag
result := []byte("<!--")
result = append(result, c.Value...)
result = append(result, []byte("-->")...)
return result
}
func (c *Comment) Index() []int {
return c.index
}
// Compare non-recursively compares c to other. It does not check
// the child nodes since they can be a Node with any underlying type.
// If you want to compare the parent and children fields, use CompareNodes.
func (c *Comment) Compare(other *Comment) (bool, string) {
if string(c.Value) != string(other.Value) {
return false, fmt.Sprintf("c.Value was %s but other.Value was %s", string(c.Value), string(other.Value))
}
return true, ""
}
// Compare recursively compares t to other. It returns false and a detailed
// message if n does not equal other. Otherwise, it returns true and an empty
// string. NOTE: Comare never checks the parent properties of t's
// children. This is so you can construct a comparable tree inside a literal.
// (You can't set the parent field inside a literal).
func (t *Tree) Compare(other *Tree, compareAttrs bool) (bool, string) {
if len(t.Children) != len(other.Children) {
return false, fmt.Sprintf("t had %d first-level children but other had %d", len(t.Children), len(other.Children))
}
for i, root := range t.Children {
otherRoot := other.Children[i]
if match, msg := CompareNodesRecursive(root, otherRoot, compareAttrs); !match {
return false, msg
}
}
return true, ""
}
// CompareNodes non-recursively compares n to other. It returns false and
// a detailed message if n does not equal other. Otherwise, it returns true and
// an empty string. NOTE: CompareNodes never checks the parent properties of n
// or n's children. This is so you can construct a comparable tree inside a literal.
// (You can't set the parent field inside a literal).
func CompareNodes(n Node, other Node, compareAttrs bool) (bool, string) {
if reflect.TypeOf(n) != reflect.TypeOf(other) {
return false, fmt.Sprintf("n has underlying type %T but the other node has underlying type %T", n, other)
}
switch n.(type) {
case *Element:
el := n.(*Element)
otherEl := other.(*Element)
if match, msg := el.Compare(otherEl, compareAttrs); !match {
return false, msg
}
case *Text:
text := n.(*Text)
otherText := other.(*Text)
if match, msg := text.Compare(otherText); !match {
return false, msg
}
case *Comment:
comment := n.(*Comment)
otherComment := other.(*Comment)
if match, msg := comment.Compare(otherComment); !match {
return false, msg
}
default:
return false, fmt.Sprintf("Don't know how to compare n of underlying type %T", n)
}
return true, ""
}
// CompareNodesRecursive recursively compares n to other. It returns false and
// a detailed message if n does not equal other. Otherwise, it returns true and
// an empty string. NOTE: CompareNodesRecursive never checks the parent properties
// of n or n's children. This is so you can construct a comparable tree inside a
// literal. (You can't set the parent field inside a literal).
func CompareNodesRecursive(n Node, other Node, compareAttrs bool) (bool, string) {
if match, msg := CompareNodes(n, other, compareAttrs); !match {
return false, msg
}
children := n.Children()
otherChildren := other.Children()
if len(children) != len(otherChildren) {
return false, fmt.Sprintf("n has %d children but other has %d children.", len(children), len(otherChildren))
}
for i, child := range children {
otherChild := otherChildren[i]
if match, msg := CompareNodesRecursive(child, otherChild, compareAttrs); !match {
return false, msg
}
}
return true, ""
}