-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtagmatch.go
More file actions
57 lines (46 loc) · 1.25 KB
/
tagmatch.go
File metadata and controls
57 lines (46 loc) · 1.25 KB
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
package csstool
type matcher interface {
Remove([]string) bool
}
// EmptyMatcher this keeps all elements, or rather, doesn't remove anything
type EmptyMatcher struct{}
// Remove always returns false (i.e. keep everything)
func (em *EmptyMatcher) Remove([]string) bool {
return false
}
// TagMatcher determines if a given CSS identifier should be kept or removed
// doesn't need to be public
type TagMatcher struct {
tags map[string]bool
}
// NewTagMatcher creates an initialized TagMatch object
func NewTagMatcher(tags []string) *TagMatcher {
tagmap := make(map[string]bool, len(tags))
tagmap[""] = true
tagmap["*"] = true
tagmap[":root"] = true
tagmap[":first-child"] = true
tagmap["::after"] = true
tagmap["::before"] = true
for _, tag := range tags {
tagmap[tag] = true
}
return &TagMatcher{tags: tagmap}
}
// Remove returns true if tag is to be dropped
func (tm *TagMatcher) Remove(selectors []string) bool {
for _, selector := range selectors {
if !tm.tags[selector] {
return true
}
}
return false //i.e. keep
}
// AddSelector adds a selector to save
func (tm *TagMatcher) AddSelector(key string) {
tm.tags[key] = true
}
// RemoveSelector deletes a selector to save
func (tm *TagMatcher) RemoveSelector(key string) {
delete(tm.tags, key)
}