Merge pull request #560 from jvoisin/if

perf: pre-check raw class attribute in HasClass
This commit is contained in:
Martin Angers
2026-05-28 10:17:55 -04:00
committed by GitHub
+4
View File
@@ -137,12 +137,16 @@ func (s *Selection) AddClass(class ...string) *Selection {
// HasClass determines whether any of the matched elements are assigned the
// given class.
func (s *Selection) HasClass(class string) bool {
rawClass := class
class = " " + class + " "
for _, n := range s.Nodes {
if n.Type != html.ElementNode {
continue
}
if attr := getAttributePtr("class", n); attr != nil {
if !strings.Contains(attr.Val, rawClass) {
continue
}
val := classTrimReplacer.Replace(attr.Val)
if strings.Contains(" "+val+" ", class) {
return true