Upstream PuerkitoBio/goquery state of the touched files before b4ef68dec8
This commit is contained in:
1 parent
048bc98ddf
commit
d024a0aa26
2 files changed
+4
-81
No files matched your search
+4
-22
@@ -167,7 +167,7 @@ func (s *Selection) RemoveClass(class ...string) *Selection {
|
||||
} else {
|
||||
classes, attr := getClassesAndAttr(n)
|
||||
for _, rcl := range rclasses {
|
||||
classes = removeClassFromClasses(classes, rcl)
|
||||
classes = strings.ReplaceAll(classes, " "+rcl+" ", " ")
|
||||
}
|
||||
|
||||
setClasses(n, attr, classes)
|
||||
@@ -191,8 +191,9 @@ func (s *Selection) ToggleClass(class ...string) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
classes, attr := getClassesAndAttr(n)
|
||||
for _, tcl := range tcls {
|
||||
if strings.Contains(classes, " "+tcl+" ") {
|
||||
classes = removeClassFromClasses(classes, tcl)
|
||||
spaceAroundTcl := " " + tcl + " "
|
||||
if strings.Contains(classes, spaceAroundTcl) {
|
||||
classes = strings.ReplaceAll(classes, spaceAroundTcl, " ")
|
||||
} else {
|
||||
classes += tcl + " "
|
||||
}
|
||||
@@ -204,25 +205,6 @@ func (s *Selection) ToggleClass(class ...string) *Selection {
|
||||
return s
|
||||
}
|
||||
|
||||
// removeClassFromClasses removes every occurrence of cl from the normalized
|
||||
// class string (a class list wrapped in spaces, as returned by
|
||||
// getClassesAndAttr).
|
||||
//
|
||||
// A single strings.ReplaceAll pass is not enough: two adjacent occurrences
|
||||
// share the space between them, so " a a " contains only one non-overlapping
|
||||
// " a " and the second one survives the replacement. Replacing one at a time
|
||||
// re-scans from the start and reaches it.
|
||||
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
|
||||
|
||||
Reference in new issue
Block a user