feat: Add Node#QuerySelectorAll

This commit is contained in:
ptrcnull 2022-07-07 23:57:08 +02:00
parent 91fb097308
commit 3a2cfbb8be

14
html.go
View file

@ -30,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)