fix: remove every occurrence of a repeated class

RemoveClass deleted each class with one strings.ReplaceAll pass over the
space-wrapped class list. Two adjacent occurrences share the space
between them, so " a a " holds only one non-overlapping " a " and the
second survived: RemoveClass("a") on class="a a" returned with
HasClass("a") still true. ToggleClass used the same expression and so
toggled such a class the wrong way.

Replace one occurrence at a time, re-scanning from the start, in a helper
shared by both call sites. Whitespace between the remaining classes is
untouched.
This commit is contained in:
dualfroz committed 2026-09-06 00:44:19 +02:00
1 parent d024a0aa26
commit d6450709f9
2 files changed
+76 -4

No files matched your search

+17 -4
View File
@@ -167,7 +167,7 @@ func (s *Selection) RemoveClass(class ...string) *Selection {
} else {
classes, attr := getClassesAndAttr(n)
for _, rcl := range rclasses {
classes = strings.ReplaceAll(classes, " "+rcl+" ", " ")
classes = removeClassFromClasses(classes, rcl)
}
setClasses(n, attr, classes)
@@ -191,9 +191,8 @@ func (s *Selection) ToggleClass(class ...string) *Selection {
for _, n := range s.Nodes {
classes, attr := getClassesAndAttr(n)
for _, tcl := range tcls {
spaceAroundTcl := " " + tcl + " "
if strings.Contains(classes, spaceAroundTcl) {
classes = strings.ReplaceAll(classes, spaceAroundTcl, " ")
if strings.Contains(classes, " "+tcl+" ") {
classes = removeClassFromClasses(classes, tcl)
} else {
classes += tcl + " "
}
@@ -205,6 +204,20 @@ func (s *Selection) ToggleClass(class ...string) *Selection {
return s
}
// removeClassFromClasses removes every occurrence of cl from the normalized
// class string. One ReplaceAll pass is not enough: adjacent occurrences
// share the space between them, so " a a " holds only one " a ".
func removeClassFromClasses(classes, cl string) string {
target := " " + cl + " "
for {
replaced := strings.Replace(classes, target, " ", 1)
if replaced == classes {
return classes
}
classes = replaced
}
}
func getAttributePtr(attrName string, n *html.Node) *html.Attribute {
if n == nil {
return nil