-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
240 lines (210 loc) · 6.85 KB
/
element.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
package dom
import (
"bytes"
"strings"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"github.com/crhntr/dom/spec"
)
type Element struct {
node *html.Node
}
func (e *Element) QuerySelectorEach(query string) spec.NodeIterator[spec.Element] {
m := cascadia.MustCompile(query)
return func(yield func(spec.Element) bool) {
querySelectorEach(e.node, m, yield)
}
}
// NewNode
func (e *Element) NodeType() spec.NodeType { return nodeType(e.node.Type) }
func (e *Element) IsConnected() bool { return isConnected(e.node) }
func (e *Element) OwnerDocument() spec.Document { return ownerDocument(e.node) }
func (e *Element) ParentNode() spec.Node { return parentNode(e.node) }
func (e *Element) ParentElement() spec.Element { return parentElement(e.node) }
func (e *Element) PreviousSibling() spec.ChildNode { return previousSibling(e.node) }
func (e *Element) NextSibling() spec.ChildNode { return nextSibling(e.node) }
func (e *Element) TextContent() string { return textContent(e.node) }
func (e *Element) CloneNode(deep bool) spec.Node { return NewNode(cloneNode(e.node, deep)) }
func (e *Element) IsSameNode(other spec.Node) bool { return isSameNode(e.node, other) }
func (e *Element) Length() int {
c := e.node.FirstChild
result := 0
for c != nil {
result++
c = c.NextSibling
}
return result
}
// ParentNode
func (e *Element) Children() spec.ElementCollection { return children(e.node) }
func (e *Element) FirstElementChild() spec.Element { return firstElementChild(e.node) }
func (e *Element) LastElementChild() spec.Element { return lastElementChild(e.node) }
func (e *Element) ChildElementCount() int { return childElementCount(e.node) }
func (e *Element) Prepend(nodes ...spec.Node) { prependNodes(e.node, nodes) }
func (e *Element) Append(nodes ...spec.Node) { appendNodes(e.node, nodes...) }
func (e *Element) ReplaceChildren(nodes ...spec.Node) { replaceChildren(e.node, nodes) }
func (e *Element) GetElementsByTagName(name string) spec.ElementCollection {
return getElementsByTagName(e.node, name)
}
func (e *Element) GetElementsByClassName(name string) spec.ElementCollection {
return getElementsByClassName(e.node, name)
}
func (e *Element) QuerySelector(query string) spec.Element {
return querySelector(e.node, query, false)
}
func (e *Element) QuerySelectorAll(query string) spec.NodeList[spec.Element] {
return querySelectorAll(e.node, query, false)
}
func (e *Element) Closest(selector string) spec.Element { return closest(e.node, selector) }
func (e *Element) Matches(selector string) bool { return matches(e.node, selector) }
func (e *Element) HasChildNodes() bool { return hasChildNodes(e.node) }
func (e *Element) ChildNodes() spec.NodeList[spec.Node] { return childNodes(e.node) }
func (e *Element) FirstChild() spec.ChildNode { return firstChild(e.node) }
func (e *Element) LastChild() spec.ChildNode { return lastChild(e.node) }
func (e *Element) Contains(other spec.Node) bool { return contains(e.node, other) }
func (e *Element) InsertBefore(node, child spec.ChildNode) spec.ChildNode {
return insertBefore(e.node, node, child)
}
func (e *Element) AppendChild(node spec.ChildNode) spec.ChildNode { return appendChild(e.node, node) }
func (e *Element) ReplaceChild(node, child spec.ChildNode) spec.ChildNode {
return replaceChild(e.node, node, child)
}
func (e *Element) RemoveChild(node spec.ChildNode) spec.ChildNode { return removeChild(e.node, node) }
// Element
func (e *Element) TagName() string { return strings.ToUpper(e.node.Data) }
func (e *Element) ID() string { return getAttribute(e.node, "id") }
func (e *Element) ClassName() string { return getAttribute(e.node, "class") }
func (e *Element) GetAttribute(name string) string { return getAttribute(e.node, name) }
func (e *Element) SetAttribute(name, value string) {
name = strings.ToLower(name)
for index, att := range e.node.Attr {
if att.Key == name {
e.node.Attr[index].Val = value
}
}
e.node.Attr = append(e.node.Attr, html.Attribute{
Key: name, Val: value,
})
}
func (e *Element) RemoveAttribute(name string) {
name = strings.ToLower(name)
filtered := e.node.Attr[:0]
for _, att := range e.node.Attr {
if att.Key == name {
continue
}
filtered = append(filtered, att)
}
e.node.Attr = filtered
}
func (e *Element) ToggleAttribute(name string) bool {
name = strings.ToLower(name)
if e.HasAttribute(name) {
e.RemoveAttribute(name)
return false
}
e.SetAttribute(name, "")
return true
}
func (e *Element) HasAttribute(name string) bool {
name = strings.ToLower(name)
for _, att := range e.node.Attr {
if att.Key == name {
return true
}
}
return false
}
func (e *Element) SetInnerHTML(s string) {
nodes, err := html.ParseFragment(strings.NewReader(s), &html.Node{Type: html.ElementNode})
if err != nil {
panic(err)
}
clearChildren(e.node)
for _, n := range nodes {
e.node.AppendChild(n)
}
}
func (e *Element) InnerHTML() string {
var buf bytes.Buffer
c := e.node.FirstChild
for c != nil {
err := html.Render(&buf, c)
if err != nil {
panic(err)
}
c = c.NextSibling
}
return buf.String()
}
func (e *Element) SetOuterHTML(s string) {
nodes, err := html.ParseFragment(strings.NewReader(s), &html.Node{Type: html.ElementNode})
if err != nil {
panic(err)
}
if len(nodes) == 0 {
return
}
if e.node.Parent == nil {
panic("browser: SetOuterHTML called on an unattached node")
}
for _, node := range nodes {
e.node.Parent.InsertBefore(node, e.node)
}
e.node.Parent.RemoveChild(e.node)
}
func (e *Element) OuterHTML() string { return outerHTML(e.node) }
func (e *Element) String() string { return e.OuterHTML() }
type siblingElements struct {
firstChild *html.Node
}
func (list siblingElements) Length() int {
result := 0
for c := list.firstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode {
continue
}
result++
}
return result
}
func (list siblingElements) Item(index int) spec.Element {
childIndex := 0
for c := list.firstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode {
continue
}
if childIndex == index {
return &Element{node: c}
}
childIndex++
}
return nil
}
func (list siblingElements) NamedItem(name string) spec.Element {
for c := list.firstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode {
continue
}
if isNamed(c, name) {
return &Element{node: c}
}
}
return nil
}
type elementList []*html.Node
func (list elementList) Length() int { return len(list) }
func (list elementList) Item(index int) spec.Element {
if index < 0 || index >= len(list) {
return nil
}
return &Element{node: list[index]}
}
func (list elementList) NamedItem(name string) spec.Element {
for _, el := range list {
if isNamed(el, name) {
return &Element{node: el}
}
}
return nil
}