-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
86 lines (70 loc) · 2.35 KB
/
document.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
package dom
import (
"strings"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
"github.com/crhntr/dom/spec"
)
type Document struct {
node *html.Node
}
func (d *Document) Head() spec.Element { return d.QuerySelector("head") }
func (d *Document) Body() spec.Element { return d.QuerySelector("body") }
func (d *Document) String() string { return outerHTML(d.node) }
func (d *Document) NodeType() spec.NodeType { return nodeType(d.node.Type) }
func (d *Document) CloneNode(deep bool) spec.Node { return NewNode(cloneNode(d.node, deep)) }
func (d *Document) IsSameNode(other spec.Node) bool { return isSameNode(d.node, other) }
func (d *Document) GetElementsByTagName(name string) spec.ElementCollection {
return getElementsByTagName(d.node, name)
}
func (d *Document) GetElementsByClassName(name string) spec.ElementCollection {
return getElementsByClassName(d.node, name)
}
func (d *Document) QuerySelector(query string) spec.Element {
return querySelector(d.node, query, false)
}
func (d *Document) QuerySelectorAll(query string) spec.NodeList[spec.Element] {
return querySelectorAll(d.node, query, false)
}
func (d *Document) QuerySelectorEach(query string) spec.NodeIterator[spec.Element] {
m := cascadia.MustCompile(query)
return func(yield func(spec.Element) bool) {
querySelectorEach(d.node, m, yield)
}
}
func (d *Document) Contains(other spec.Node) bool { return contains(d.node, other) }
// TextContent returns an empty string.
// The spec says it should return null
// https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
func (d *Document) TextContent() string { return "" }
// Document
func (*Document) CreateElement(localName string) spec.Element {
localName = strings.ToLower(localName)
return &Element{
node: &html.Node{
DataAtom: atom.Lookup([]byte(localName)),
Type: html.ElementNode,
Data: localName,
},
}
}
func (*Document) CreateElementIs(localName, is string) spec.Element {
localName = strings.ToLower(localName)
return &Element{
node: &html.Node{
DataAtom: atom.Lookup([]byte(localName)),
Type: html.ElementNode,
Data: localName,
Attr: []html.Attribute{{Key: "is", Val: is}},
},
}
}
func (*Document) CreateTextNode(text string) spec.Text {
return &Text{
node: &html.Node{
Type: html.TextNode,
Data: text,
},
}
}