Compare commits
3 commits
393210aa6f
...
947b928a5d
Author | SHA1 | Date | |
---|---|---|---|
ptrcnull | 947b928a5d | ||
ptrcnull | 3a2cfbb8be | ||
ptrcnull | 91fb097308 |
22
html.go
22
html.go
|
@ -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) {
|
||||||
|
|
Loading…
Reference in a new issue