-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
538 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package dom | ||
|
||
import ( | ||
"bytes" | ||
"slices" | ||
|
||
"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) 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.ChildNode) { | ||
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.ChildNode) { | ||
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.ChildNode) { | ||
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) | ||
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)...) | ||
} | ||
return slices.Clip(list) | ||
} |
Oops, something went wrong.