Compare commits

...

1 Commits

Author SHA1 Message Date
55a6a4d3ef MatchElemAttr func implementation 2026-01-16 01:13:53 +03:00
3 changed files with 67 additions and 3 deletions

38
match.go Normal file
View 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
}

26
match_test.go Normal file
View File

@@ -0,0 +1,26 @@
package scraper
import (
"strings"
"testing"
"golang.org/x/net/html"
)
const fragment = `<div id="main-copy"></div>`
func TestMatchElemAttr(t *testing.T) {
n, err := html.ParseFragment(strings.NewReader(fragment), body)
if err != nil {
t.Error(err)
}
n1 := n[0]
result, err := MatchElemAttr(fragment, n1)
if err != nil {
t.Error(err)
}
if !result {
t.Fail()
}
}

View File

@@ -15,7 +15,7 @@ func TestSearchElem(t *testing.T) {
html.Parse(strings.NewReader(htmlStr)),
)
ch := searchElem(doc, "span")
ch := SearchElem(doc, "span")
count := 0
for range ch {
count++
@@ -31,7 +31,7 @@ func TestSearchAttr(t *testing.T) {
html.Parse(strings.NewReader(htmlStr)),
)
ch := searchAttr(doc, "id", "test")
ch := SearchAttr(doc, "id", "test")
count := 0
for range ch {
count++
@@ -47,7 +47,7 @@ func TestSearchElemAttr(t *testing.T) {
html.Parse(strings.NewReader(htmlStr)),
)
ch := searchElemAttr(doc, "span", "id", "test")
ch := SearchElemAttr(doc, "span", "id", "test")
count := 0
for range ch {
count++