-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment.go
146 lines (126 loc) · 3.22 KB
/
fragment.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
package dom
import (
"bytes"
"slices"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"github.com/crhntr/dom/spec"
)
type DocumentFragment struct {
nodes []*html.Node
}
func NewDocumentFragment(nodes []*html.Node) *DocumentFragment {
return &DocumentFragment{nodes: nodes}
}
func (d *DocumentFragment) String() string { return outerHTML(d.nodes...) }
func (d *DocumentFragment) NodeType() spec.NodeType { return spec.NodeTypeDocumentFragment }
func (d *DocumentFragment) CloneNode(deep bool) spec.Node {
if !deep {
return &DocumentFragment{nodes: d.nodes}
}
df := &DocumentFragment{nodes: make([]*html.Node, len(d.nodes))}
for _, e := range d.nodes {
df.nodes = append(df.nodes, cloneNode(e, deep))
}
return df
}
func (d *DocumentFragment) IsSameNode(other spec.Node) bool {
o, ok := other.(*DocumentFragment)
if !ok {
return false
}
return d == o
}
func (d *DocumentFragment) TextContent() string {
var buf bytes.Buffer
for _, n := range d.nodes {
recursiveTextContent(&buf, n)
}
return buf.String()
}
func (d *DocumentFragment) Children() spec.ElementCollection {
elementChildren := make(elementList, 0, len(d.nodes))
for _, n := range d.nodes {
if n != nil && n.Type == html.ElementNode {
elementChildren = append(elementChildren, n)
}
}
return slices.Clip(elementChildren)
}
func (d *DocumentFragment) FirstElementChild() spec.Element {
for _, n := range d.nodes {
if n.Type == html.ElementNode {
return &Element{node: n}
}
}
return nil
}
func (d *DocumentFragment) LastElementChild() spec.Element {
for i := range d.nodes {
n := d.nodes[len(d.nodes)-1-i]
if n.Type == html.ElementNode {
return &Element{node: n}
}
}
return nil
}
func (d *DocumentFragment) ChildElementCount() int {
count := 0
for _, n := range d.nodes {
if n.Type == html.ElementNode {
count++
}
}
return count
}
func (d *DocumentFragment) Append(nodes ...spec.Node) {
d.nodes = slices.Grow(d.nodes, len(nodes))
for _, node := range nodes {
d.nodes = append(d.nodes, domNodeToHTMLNode(node))
}
}
func (d *DocumentFragment) Prepend(nodes ...spec.Node) {
children := make([]*html.Node, 0, len(d.nodes)+len(nodes))
for _, node := range nodes {
children = append(children, domNodeToHTMLNode(node))
}
d.nodes = append(children, d.nodes...)
}
func (d *DocumentFragment) ReplaceChildren(nodes ...spec.Node) {
list := make([]*html.Node, 0, len(nodes))
for _, node := range nodes {
list = append(list, domNodeToHTMLNode(node))
}
d.nodes = list
}
func (d *DocumentFragment) QuerySelector(query string) spec.Element {
for _, n := range d.nodes {
el := querySelector(n, query, true)
if el != nil {
return el
}
}
return nil
}
func (d *DocumentFragment) QuerySelectorAll(query string) spec.NodeList[spec.Element] {
var list nodeListHTMLElements
for _, n := range d.nodes {
list = append(list, querySelectorAll(n, query, true)...)
}
return slices.Clip(list)
}
func (d *DocumentFragment) QuerySelectorEach(query string) spec.NodeIterator[spec.Element] {
m := cascadia.MustCompile(query)
return func(yield func(spec.Element) bool) {
for _, n := range d.nodes {
if m.Match(n) {
if !yield(&Element{node: n}) {
return
}
}
if !querySelectorEach(n, m, yield) {
return
}
}
}
}