-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.go
96 lines (83 loc) · 2.65 KB
/
class.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package html5tag
import (
"strings"
)
// Utilities to manage class strings
// MergeWords is a utility function that appends the given space separated words to the end
// of the given string, if the words are not already in the string. This is primarily used for
// adding classes to a class attribute, but other attributes work as well, like
// aria-labelledby and aria-describedby attributes.
//
// MergeWords returns the new string, which will have no duplicates.
//
// Since the order of a class list in html makes a difference, you should take care in the
// order of the classes you add if this matters in your situation.
func MergeWords(originalValues string, newValues string) string {
var found bool
wordArray := strings.Fields(originalValues)
newWordArray := strings.Fields(newValues)
for _, s := range newWordArray {
found = false
for _, s2 := range wordArray {
if s2 == s {
found = true
}
}
if !found {
wordArray = append(wordArray, s)
}
}
return strings.Join(wordArray, " ")
}
// HasWord searches haystack for the given needle.
func HasWord(haystack string, needle string) (found bool) {
classArray := strings.Fields(haystack)
for _, s := range classArray {
if s == needle {
found = true
break
}
}
return
}
// RemoveWords removes a value from the list of space-separated values given.
// You can give it more than one value to remove by
// separating the values with spaces in the removeValue string. This is particularly useful
// for removing a class from a class list in a class attribute.
func RemoveWords(originalValues string, removeValues string) string {
classes := strings.Fields(originalValues)
removeClasses := strings.Fields(removeValues)
ret := ""
var found bool
for _, s := range classes {
found = false
for _, s2 := range removeClasses {
if s2 == s {
found = true
}
}
if !found {
ret = ret + s + " "
}
}
ret = strings.TrimSpace(ret)
return ret
}
// RemoveClassesWithPrefix will remove all classes from the class string with the given prefix.
//
// Many CSS frameworks use families of classes, which are built up from a base family name. For example,
// Bootstrap uses 'col-lg-6' to represent a table that is 6 units wide on large screens and Foundation
// uses 'large-6' to do the same thing. This utility removes classes that start with a particular prefix
// to remove whatever sizing class was specified.
// Returns the resulting class list.
func RemoveClassesWithPrefix(class string, prefix string) string {
classes := strings.Fields(class)
ret := ""
for _, s := range classes {
if !strings.HasPrefix(s, prefix) {
ret = ret + s + " "
}
}
ret = strings.TrimSpace(ret)
return ret
}