64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package htmlutil
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
func TestQuery(t *testing.T) {
|
|
node, _ := html.Parse(strings.NewReader(`
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title></title>
|
|
</head>
|
|
<body>
|
|
<div>
|
|
<p>test</p>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`))
|
|
nodes := Query(node, "p")
|
|
match := (
|
|
len(nodes) == 1 &&
|
|
nodes[0].Type == html.ElementNode &&
|
|
nodes[0].Data == "p")
|
|
if !match {
|
|
t.Fatalf("incorrect match: %#v", nodes)
|
|
}
|
|
}
|
|
|
|
func TestQueryMulti(t *testing.T) {
|
|
node, _ := html.Parse(strings.NewReader(`
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title></title>
|
|
</head>
|
|
<body>
|
|
<p>foo</p>
|
|
<div>
|
|
<p>bar</p>
|
|
<span>baz</span>
|
|
</div>
|
|
</body>
|
|
</html>
|
|
`))
|
|
nodes := Query(node, "p , span")
|
|
match := (
|
|
len(nodes) == 3 &&
|
|
nodes[0].Type == html.ElementNode && nodes[0].Data == "p" &&
|
|
nodes[1].Type == html.ElementNode && nodes[1].Data == "p" &&
|
|
nodes[2].Type == html.ElementNode && nodes[2].Data == "span")
|
|
if !match {
|
|
for i, n := range nodes {
|
|
t.Logf("%d: %s", i, HTML(n))
|
|
}
|
|
t.Fatal("incorrect match")
|
|
}
|
|
}
|