Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled multiple attribute matchers for same attribute. #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type PseudoClass func(*html.Node) bool

type CSSSelector struct {
Tag string
Attrs map[string]*regexp.Regexp
Attrs map[string][]*regexp.Regexp
Pseudo PseudoClass
}

Expand All @@ -90,15 +90,16 @@ func (s CSSSelector) Match(node *html.Node) bool {
return false
}
}
for attrKey, matcher := range s.Attrs {
for attrKey, matchers := range s.Attrs {
matched := false
for _, attr := range node.Attr {
if attrKey == attr.Key {
if !matcher.MatchString(attr.Val) {
return false
for _, matcher := range matchers {
if !matcher.MatchString(attr.Val) {
return false
}
matched = true
}
matched = true
break
}
}
if !matched {
Expand All @@ -116,7 +117,7 @@ func (s CSSSelector) Match(node *html.Node) bool {
func ParseSelector(cmd string) (selector CSSSelector, err error) {
selector = CSSSelector{
Tag: "",
Attrs: map[string]*regexp.Regexp{},
Attrs: map[string][]*regexp.Regexp{},
Pseudo: nil,
}
var s scanner.Scanner
Expand Down Expand Up @@ -153,13 +154,21 @@ func ParseTagMatcher(selector *CSSSelector, s scanner.Scanner) error {
}
}

func AddAttrSelector(selector *CSSSelector, attrname string, regex *regexp.Regexp) {
if slice, ok := selector.Attrs[attrname]; ok && len(slice) > 0 {
selector.Attrs[attrname] = append(selector.Attrs[attrname], regex)
} else {
selector.Attrs[attrname] = []*regexp.Regexp{regex}
}
}

// Parse a class matcher
// e.g. `.btn`
func ParseClassMatcher(selector *CSSSelector, s scanner.Scanner) error {
var class bytes.Buffer
defer func() {
regexpStr := `(\A|\s)` + regexp.QuoteMeta(class.String()) + `(\s|\z)`
selector.Attrs["class"] = regexp.MustCompile(regexpStr)
AddAttrSelector(selector, "class", regexp.MustCompile(regexpStr))
}()
for {
c := s.Next()
Expand Down Expand Up @@ -188,7 +197,7 @@ func ParseIdMatcher(selector *CSSSelector, s scanner.Scanner) error {
var id bytes.Buffer
defer func() {
regexpStr := `^` + regexp.QuoteMeta(id.String()) + `$`
selector.Attrs["id"] = regexp.MustCompile(regexpStr)
AddAttrSelector(selector, "id", regexp.MustCompile(regexpStr))
}()
for {
c := s.Next()
Expand Down Expand Up @@ -233,9 +242,9 @@ func ParseAttrMatcher(selector *CSSSelector, s scanner.Scanner) error {
case '~':
regexpStr = `(\A|\s)` + regexp.QuoteMeta(attrVal.String()) + `(\s|\z)`
}
selector.Attrs[attrKey.String()] = regexp.MustCompile(regexpStr)
AddAttrSelector(selector, attrKey.String(), regexp.MustCompile(regexpStr))
} else {
selector.Attrs[attrKey.String()] = regexp.MustCompile(`^.*$`)
AddAttrSelector(selector, attrKey.String(), regexp.MustCompile(`^.*$`))
}
}()
// After reaching ']' proceed
Expand Down