-
Notifications
You must be signed in to change notification settings - Fork 8
/
atom_set.go
82 lines (71 loc) · 1.76 KB
/
atom_set.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
package htmlutil
import (
"fmt"
"sort"
"strings"
"golang.org/x/net/html/atom"
)
type AtomSet struct {
Atoms map[string]atom.Atom
}
func NewAtomSet(htmlAtoms ...atom.Atom) AtomSet {
atomSet := AtomSet{
Atoms: map[string]atom.Atom{}}
if len(htmlAtoms) > 0 {
atomSet.Add(htmlAtoms...)
}
return atomSet
}
func NewAtomSetStringMust(tagNames ...string) AtomSet {
atoms, err := NewAtomSetString(tagNames...)
if err != nil {
panic(err)
}
return atoms
}
func NewAtomSetString(tagNames ...string) (AtomSet, error) {
if len(tagNames) == 0 {
return NewAtomSet(), nil
}
atoms := []atom.Atom{}
unmatchedNames := []string{}
for _, tagName := range tagNames {
tagAtom := AtomLookupString(tagName)
if strings.TrimSpace(tagAtom.String()) == "" {
unmatchedNames = append(unmatchedNames, tagName)
} else {
atoms = append(atoms, tagAtom)
}
}
atomsSet := NewAtomSet(atoms...)
if len(unmatchedNames) > 0 {
return atomsSet, fmt.Errorf("unmatchedTagNames [%s]", strings.Join(unmatchedNames, ","))
}
return atomsSet, nil
}
// AtomLookupString returns the atom whose name is s. It returns zero if there is no such atom. The lookup is case sensitive.
func AtomLookupString(s string) atom.Atom {
return atom.Lookup([]byte(strings.ToLower(strings.TrimSpace(s))))
}
func (set AtomSet) Len() int {
return len(set.Atoms)
}
func (set AtomSet) Add(htmlAtoms ...atom.Atom) {
for _, htmlAtom := range htmlAtoms {
set.Atoms[htmlAtom.String()] = htmlAtom
}
}
func (set AtomSet) Exists(htmlAtom atom.Atom) bool {
if _, ok := set.Atoms[htmlAtom.String()]; ok {
return true
}
return false
}
func (set AtomSet) Names() []string {
names := []string{}
for _, htmlAtom := range set.Atoms {
names = append(names, htmlAtom.String())
}
sort.Strings(names)
return names
}