Compare commits

...

3 commits

Author SHA1 Message Date
ptrcnull 947b928a5d feat: Add Node#RemoveChild 2022-07-07 23:57:16 +02:00
ptrcnull 3a2cfbb8be feat: Add Node#QuerySelectorAll 2022-07-07 23:57:08 +02:00
ptrcnull 91fb097308 style: Reformat imports 2022-07-07 23:56:53 +02:00

22
html.go
View file

@ -2,9 +2,10 @@ package html
import ( import (
"bytes" "bytes"
"golang.org/x/net/html"
"io" "io"
"strings" "strings"
"golang.org/x/net/html"
) )
type Node struct { type Node struct {
@ -29,6 +30,20 @@ func (n *Node) QuerySelector(selector string) *Node {
return n.GetElementByTagName(selector) return n.GetElementByTagName(selector)
} }
func (n *Node) QuerySelectorAll(selector string) []*Node {
return n.FindMany(func(n *Node) bool {
if strings.HasPrefix(selector, "#") {
return n.HasAttr("id", selector[1:])
}
if strings.HasPrefix(selector, ".") {
return n.HasClass(selector[1:])
}
return n.Type == html.ElementNode && n.Data == selector
})
}
func (n *Node) GetElementById(id string) *Node { func (n *Node) GetElementById(id string) *Node {
return n.FindOne(func(n *Node) bool { return n.FindOne(func(n *Node) bool {
return n.HasAttr("id", id) return n.HasAttr("id", id)
@ -94,6 +109,11 @@ func (n *Node) Children() []*Node {
return res return res
} }
func (n *Node) RemoveChild(other *Node) {
// thanks go stdlib!
n.Node.RemoveChild(other.Node)
}
func (n *Node) Traverse(cb func(n *Node)) { func (n *Node) Traverse(cb func(n *Node)) {
var f func(*Node) var f func(*Node)
f = func(n *Node) { f = func(n *Node) {