MCPcopy Create free account
hub / github.com/PuerkitoBio/goquery / AddClass

Method AddClass

property.go:110–130  ·  view source on GitHub ↗

AddClass adds the given class(es) to each element in the set of matched elements. Multiple class names can be specified, separated by a space or via multiple arguments.

(class ...string)

Source from the content-addressed store, hash-verified

108// AddClass adds the given class(es) to each element in the set of matched elements.
109// Multiple class names can be specified, separated by a space or via multiple arguments.
110func (s *Selection) AddClass(class ...string) *Selection {
111 classStr := strings.Trim(strings.Join(class, " "), " \t\r\n\f")
112
113 if classStr == "" {
114 return s
115 }
116
117 tcls := getClassesSlice(classStr)
118 for _, n := range s.Nodes {
119 curClasses, attr := getClassesAndAttr(n)
120 for _, newClass := range tcls {
121 if !strings.Contains(curClasses, " "+newClass+" ") {
122 curClasses += newClass + " "
123 }
124 }
125
126 setClasses(n, attr, curClasses)
127 }
128
129 return s
130}
131
132// HasClass determines whether any of the matched elements are assigned the
133// given class.

Calls 4

getClassesSliceFunction · 0.85
getClassesAndAttrFunction · 0.85
setClassesFunction · 0.85
ContainsMethod · 0.80

Tested by 10

TestAddClassFunction · 0.64
TestAddClassSimilarFunction · 0.64
TestAddEmptyClassFunction · 0.64
TestAddClassesFunction · 0.64
BenchmarkAddClassFunction · 0.64
BenchmarkRemoveClassFunction · 0.64