-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher.go
39 lines (33 loc) · 847 Bytes
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package htmlutil
import (
"strings"
"golang.org/x/net/html"
)
type Matcher func(*html.Node) bool
func MatcherTagName(tagName string) Matcher {
return func(n *html.Node) bool {
return n.Type == html.ElementNode && n.Data == tagName
}
}
func MatcherTagNameAndClass(tagName string, class string) Matcher {
return func(n *html.Node) bool {
if n.Type != html.ElementNode || n.Data != "tr" {
return false
}
for _, attr := range n.Attr {
if attr.Key == "class" {
classes := strings.Fields(attr.Val)
for _, c := range classes {
if c == class {
return true
}
}
break
}
}
return false
}
}