MatchElemAttr func implementation
This commit is contained in:
38
match.go
Normal file
38
match.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package scraper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"golang.org/x/net/html/atom"
|
||||
)
|
||||
|
||||
var body = &html.Node{
|
||||
Type: html.ElementNode,
|
||||
Data: "body",
|
||||
DataAtom: atom.Body,
|
||||
}
|
||||
|
||||
var ErrNotAnElementNode = errors.New("not an ElementNode")
|
||||
|
||||
func MatchElemAttr(s string, n2 *html.Node) (bool, error) {
|
||||
r := strings.NewReader(s)
|
||||
n, err := html.ParseFragment(r, body)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n1 := n[0]
|
||||
|
||||
if !(n1.Type == html.ElementNode &&
|
||||
n2.Type == html.ElementNode) {
|
||||
return false, ErrNotAnElementNode
|
||||
}
|
||||
|
||||
if n1.Data == n2.Data &&
|
||||
slices.Equal(n1.Attr, n2.Attr) {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user