-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-tree.go
More file actions
226 lines (187 loc) · 6.37 KB
/
node-tree.go
File metadata and controls
226 lines (187 loc) · 6.37 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
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
package GoHtml
import (
"strings"
"golang.org/x/net/html"
)
// Node is a struct that represents a html elements. Nodes can have sibling nodes(NextNode and Previous Node) and child node that represent the child elements.
// Text is also stored as a node which can be checked by using IsTextNode method.
type Node struct {
nextNode *Node
previousNode *Node
childNode *Node
parentNode *Node
tagName string
attributes map[string]string
text string
}
// GetNextNode returns node next to the node.
func (node *Node) GetNextNode() *Node {
return node.nextNode
}
// SetNextNode make nodes next node as nextNode.
func (node *Node) SetNextNode(nextNode *Node) {
node.nextNode = nextNode
}
// GetPreviousNode returns the previous node.
func (node *Node) GetPreviousNode() *Node {
return node.previousNode
}
// SetPreviousNode sets nodes previous node to previousNode.
func (node *Node) SetPreviousNode(previousNode *Node) {
node.previousNode = previousNode
}
// GetChildNode returns the first child node of this node.
func (node *Node) GetChildNode() *Node {
return node.childNode
}
// getParentNode returns parent node.
func (node *Node) getParentNode() *Node {
return node.parentNode
}
func (node *Node) setParentNode(parentNode *Node) {
node.parentNode = parentNode
}
// Returns a string with the name of the tag for the given node.
func (node *Node) GetTagName() string {
if strings.ToUpper(node.tagName) == DOCTYPEDTD {
return strings.ToUpper(node.tagName)
}
return node.tagName
}
// SetTagName changes the html tag name to the tagName.
func (node *Node) SetTagName(tagName string) {
node.tagName = strings.TrimSpace(strings.ToLower(tagName))
}
// GetAttribute returns the specified attribute value form the node. If the specified attribute doesn't exists GetAttribute returns a empty string and false.
func (node *Node) GetAttribute(attributeName string) (string, bool) {
v, ok := node.attributes[strings.TrimSpace(strings.ToLower(attributeName))]
return v, ok
}
// RemoveAttribute remove or delete the specified attribute.
func (node *Node) RemoveAttribute(attributeName string) {
delete(node.attributes, strings.TrimSpace(strings.ToLower(attributeName)))
}
// IterateAttributes calls callback at every attribute in the node by passing attribute and value of the node.
func (node *Node) IterateAttributes(callback func(attribute, value string)) {
attributes := node.attributes
for k, v := range attributes {
callback(k, v)
}
}
// SetAttribute add a attribute to the node.
func (node *Node) SetAttribute(attribute, value string) {
node.attributes[strings.ToLower(strings.TrimSpace(attribute))] = strings.TrimSpace(value)
}
// GetText returns text on the node. This does not returns text on it's child nodes. If you also wants child nodes text use GetInnerText method on the node.
// HTML tags in returns value get escaped.
func (node *Node) GetText() string {
text := node.text
return text
}
// SetText add text to the node.
// SetText unescapes entities like "<" to become "<".
func (node *Node) SetText(text string) {
node.text = html.UnescapeString(text)
}
// The AppendChild() method of the Node adds a node to the end of the list of children of a specified parent node.
func (node *Node) AppendChild(childNode *Node) {
if node.GetChildNode() == nil {
node.childNode = childNode
childNode.setParentNode(node)
return
}
lastNode := node.GetChildNode().GetLastNode()
childNode.SetPreviousNode(lastNode)
childNode.setParentNode(lastNode.GetParent())
lastNode.SetNextNode(childNode)
}
// Append inserts the newNode to end of the node chain.
func (node *Node) Append(newNode *Node) {
lastNode := node.GetLastNode()
newNode.SetPreviousNode(lastNode)
newNode.setParentNode(lastNode.GetParent())
lastNode.SetNextNode(newNode)
}
// GetParent returns a pointer to the parent node.
func (node *Node) GetParent() *Node {
return node.parentNode
}
// GetLastNode returns the last node in the node branch.
func (node *Node) GetLastNode() *Node {
traverser := NewTraverser(node)
for traverser.GetCurrentNode().GetNextNode() != nil {
traverser.Next()
}
return traverser.GetCurrentNode()
}
// GetFirstNode returns the first node of the node branch.
func (node *Node) GetFirstNode() *Node {
traverser := NewTraverser(node)
for traverser.GetCurrentNode().GetPreviousNode() != nil {
traverser.Previous()
}
return traverser.GetCurrentNode()
}
// AppendText append text to the node.
func (node *Node) AppendText(text string) {
textNode := CreateTextNode(text)
if node.GetTagName() == "" || IsVoidTag(node.GetTagName()) {
node.GetLastNode().Append(textNode)
return
}
node.GetLastNode().AppendChild(textNode)
}
// GetInnerText returns all of the text inside the node.
func (node *Node) GetInnerText() string {
text := ""
traverser := NewTraverser(node.childNode)
traverser.Walkthrough(func(node *Node) TraverseCondition {
if node.GetTagName() != "" {
return ContinueWalkthrough
}
text += node.GetText()
return ContinueWalkthrough
})
return text
}
// RemoveNode removes the node from the branch safely by connecting sibling nodes.
func (node *Node) RemoveNode() {
previousNode := node.previousNode
nextNode := node.nextNode
parentNode := node.parentNode
node.previousNode = nil
node.nextNode = nil
node.parentNode = nil
if previousNode != nil {
previousNode.SetNextNode(nextNode)
}
if nextNode != nil {
nextNode.SetPreviousNode(previousNode)
}
if nextNode != nil && previousNode == nil {
nextNode.setParentNode(parentNode)
}
if parentNode != nil {
parentNode.childNode = nextNode
}
}
// IsTextNode returns a boolean value indicating node is a text node or not.
func (node *Node) IsTextNode() bool {
return node.GetTagName() == ""
}
// Closest traverses the node tree and its parents (heading toward the root node) until it finds a node that matches the selector and returns that node.
// Adapted from [https://developer.mozilla.org/en-US/docs/Web/API/Element/closest](MDN Element: closest() method)
func (node *Node) Closest(selector string) *Node {
traverser := NewTraverser(node)
selectors := TokenizeSelectorsAndCombinators(selector)
for traverser.GetCurrentNode() != nil {
if matchFromRightMostSelectors(traverser.GetCurrentNode(), selectors) {
break
} else if traverser.GetCurrentNode().GetPreviousNode() == nil {
traverser.SetCurrentNodeTo(traverser.GetCurrentNode().GetParent())
} else {
traverser.Previous()
}
}
return traverser.GetCurrentNode()
}