-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
62 lines (54 loc) · 1.39 KB
/
util.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
package gsoup
import (
"strings"
"golang.org/x/net/html/atom"
)
func cloneWhitelist(in whitelist) (out whitelist) {
out = make(map[atom.Atom]*Tagdef)
for tag, tagdef := range in {
newdef := &Tagdef{Tag: tagdef.Tag, AllowedAttrs: make(Attrset)}
for attr := range tagdef.AllowedAttrs {
newdef.AllowedAttrs[attr] = struct{}{}
}
// enforced attrs
for key, value := range tagdef.EnforcedAttrs {
if newdef.EnforcedAttrs == nil {
newdef.EnforcedAttrs = make(map[string]string)
}
newdef.EnforcedAttrs[key] = value
}
// enforced protocols
for attr, protos := range tagdef.EnforcedProtocols {
if newdef.EnforcedProtocols == nil {
newdef.EnforcedProtocols = make(Protomap)
}
protoset := make(Protoset)
for proto := range protos {
protoset[proto] = struct{}{}
}
newdef.EnforcedProtocols[attr] = protoset
}
out[tag] = newdef
}
return out
}
func normalizeAttrKey(key string) string {
return strings.ToLower(strings.Map(func(r rune) rune {
if strings.IndexRune("\n\r\t />\"='\u0000", r) < 0 {
return r
}
return -1
}, key))
}
func normalizeProtocol(proto string) string {
proto = strings.Map(func(r rune) rune {
if strings.IndexRune("abcdefghijklmnopqrstuvwxyz0123456789+-.", r) >= 0 {
return r
}
return -1
}, strings.ToLower(proto))
if proto != "" && []rune(proto)[0] >= 'a' && []rune(proto)[0] <= 'z' {
return proto
}
return ""
}