-
Notifications
You must be signed in to change notification settings - Fork 0
/
attributes.go
118 lines (95 loc) · 2.9 KB
/
attributes.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package gohtml
import (
"regexp"
"strings"
"golang.org/x/net/html"
"github.com/saihon/gohtml/attr"
)
// Attributes returns all attributes on the element
func (e Element) Attributes() []html.Attribute {
return e.Node.Attr
}
// GetAttribute returns the value of a specified attribute
func (e Element) GetAttribute(key string) string {
return attr.Get(e.Node, key)
}
// GetAttributeNS returns the value of the attribute
// with the specified namespace and key
func (e Element) GetAttributeNS(namespace, key string) string {
return attr.GetNS(e.Node, namespace, key)
}
// GetAttributeNode returns the attribute, and the bool value
// indicating whether it exists with the specified key
func (e Element) GetAttributeNode(key string) (html.Attribute, bool) {
return attr.GetNode(e.Node, key)
}
// GetAttributeNodeNS returns the attribute, and the bool value
// indicating whether it exists with the specified namespace and key
func (e Element) GetAttributeNodeNS(namespace, key string) (html.Attribute, bool) {
return attr.GetNodeNS(e.Node, namespace, key)
}
// SetAttribute sets the value of an attribute on the element
func (e Element) SetAttribute(key string, value string) {
attr.Set(e.Node, key, value)
}
// SetAttributeNode sets the attribute.
// if already exist the key, it attribute overridden
func (e Element) SetAttributeNode(a html.Attribute) {
attr.SetNode(e.Node, a)
}
// SetAttributeNS sets the value of an attribute
// with the specified namespace and name
func (e Element) SetAttributeNS(namespace, key, value string) {
attr.SetNS(e.Node, namespace, key, value)
}
// SetAttributeNodeNS sets the namespaced attribute node on the element
func (e Element) SetAttributeNodeNS(a html.Attribute) {
attr.SetNodeNS(e.Node, a)
}
// HasAttributes returns the bool value indicating whether element has attributes
func (e Element) HasAttributes() bool {
return len(e.Node.Attr) > 0
}
// HasAttribute returns the bool value indicating
// whether element has an attribute with specified key
func (e Element) HasAttribute(key string) bool {
return attr.Has(e.Node, key)
}
// HasAttributeNS
func (e Element) HasAttributeNS(namespace, key string) bool {
return attr.HasNS(e.Node, namespace, key)
}
// RemoveAttribute
func (e Element) RemoveAttribute(key string) {
attr.Remove(e.Node, key)
}
// RemoveAttributeNS
func (e Element) RemoveAttributeNS(namespace, key string) {
attr.RemoveNS(e.Node, namespace, key)
}
// RemoveAttributeNode
func (e Element) RemoveAttributeNode(a html.Attribute) {
attr.RemoveNode(e.Node, a)
}
// DOMTokenList
type DOMTokenList struct {
List []string
Value string
}
var (
respace = regexp.MustCompile(`\s+`)
)
// ClassList
func (e Element) ClassList() DOMTokenList {
var tl DOMTokenList
tl.Value = strings.TrimSpace(attr.Get(e.Node, "class"))
if tl.Value == "" {
return tl
}
tl.List = respace.Split(tl.Value, -1)
return tl
}
// Length
func (t DOMTokenList) Length() int {
return len(t.List)
}