-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdom.go
More file actions
128 lines (109 loc) · 2.59 KB
/
dom.go
File metadata and controls
128 lines (109 loc) · 2.59 KB
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
// By Matt Hall 2018
// This code represents a DOM tree
package main
/**
* Node
*/
// Node type
type Node interface {
SetData(string)
ToString() string
NumberOfChildren() int
Value(string)
Add(Node)
Child(int) Node
}
// NodeType const enum is better than just using strings
type NodeType uint8
const (
// TextNode is for text
TextNode NodeType = iota
// ElementNode is for elements
ElementNode
)
// Element composition upon Text
type Element struct {
value string
nodeType NodeType
children []Element
attributes map[string]string
}
// SetData of the TextNode
func (node *Element) SetData(value string) {
node.value = value
}
// Child returns the child at index i
func (node *Element) Child(i int) Element {
return node.children[i]
}
// Add a node(s) to the tree
func (node *Element) Add(child Element) {
node.children = append(node.children, child)
}
// NumberOfChildren returns the length of array children in struct ElementNode
func (node *Element) NumberOfChildren() int {
if node.nodeType == TextNode {
return 0
}
return len(node.children)
}
// ToString that the node is an ElementNode
func (node *Element) ToString(indentation ...int) string {
var pretty string
if len(indentation) == 0 {
indentation = []int{0}
}
indent := 0
if node.NumberOfChildren() > 0 {
for i := 0; i < node.NumberOfChildren(); i++ {
indent = indentation[0]
child := node.Child(i)
if child.nodeType == ElementNode {
pretty += spaces(indent)
pretty += "<" + child.value
pretty += attributes(child.attributes)
// pretty = pretty + node.name
if child.NumberOfChildren() > 0 {
indent++
if child.Child(0).nodeType == ElementNode {
pretty += ">\n"
pretty += spaces(indent - 2)
pretty += child.ToString(indent)
pretty += spaces(indent - 2)
} else {
pretty += ">"
pretty += child.ToString(indent)
}
pretty += "</" + child.value + ">" + "\n"
} else {
pretty += "/>" + "\n"
}
} else {
pretty += child.value
}
}
}
return pretty
}
func spaces(step int) string {
indent := ""
for i := 0; i < step; i++ {
indent += " "
}
return indent
}
func attributes(m map[string]string) string {
attr := ""
for key, value := range m {
attr += " " + key + "=\"" + value + "\""
}
return attr
}
// MakeTextNode returns a new Text Node
func MakeTextNode(value string) Element {
return Element{value: value, nodeType: TextNode}
}
// MakeElementNode returns a new Element Node
func MakeElementNode(name string) Element {
return Element{value: name, nodeType: ElementNode, attributes: make(map[string]string)}
}