Skip to content

Commit

Permalink
css: fix class selector for elements with multiple classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchiang committed Jun 25, 2022
1 parent afb36d4 commit 6e0f06c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions css.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,12 @@ func (s *subclassSelectorMatcher) match(n *html.Node) bool {

if s.classSelector != "" {
for _, a := range n.Attr {
if a.Key == "class" && a.Val == s.classSelector {
return true
if a.Key == "class" {
for _, val := range strings.Fields(a.Val) {
if val == s.classSelector {
return true
}
}
}
}
return false
Expand Down
8 changes: 8 additions & 0 deletions css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ func TestSelector(t *testing.T) {
`<div class="foo"></div>`,
},
},
{
".foo",
`<h1><h2 class="foo bar"></h2><div class="bar foo"></div><div id="foo"></div></h1>`,
[]string{
`<h2 class="foo bar"></h2>`,
`<div class="bar foo"></div>`,
},
},
{
"div.foo",
`<h1><h2 class="foo"></h2><div class="foo"></div><div id="foo"></div></h1>`,
Expand Down

0 comments on commit 6e0f06c

Please sign in to comment.