-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.go
More file actions
190 lines (144 loc) · 2.93 KB
/
element.go
File metadata and controls
190 lines (144 loc) · 2.93 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* Copyright 2019 Kilobit Labs Inc. */
package elements // import "kilobit.ca/go/elements"
import "strings"
type ElementOption func(el *Element)
type Attributes map[string]string
type Element struct {
tag string // Tag name
attrs Attributes // Attributes
cs []Node // Children
fmt *Format // Format
}
func NewElement(tag string, opts ...ElementOption) *Element {
el := &Element{
tag,
make(Attributes),
[]Node{},
&defaultFormat,
}
for _, opt := range opts {
opt(el)
}
return el
}
func (el *Element) Copy() *Element {
attrs := make(Attributes, len(el.attrs))
for k, v := range el.attrs {
attrs[k] = v
}
cs := make([]Node, len(el.cs))
for i, c := range el.cs {
switch v := c.(type) {
case *Element:
cs[i] = v.Copy()
default:
cs[i] = c
}
}
fmt := *el.fmt
return &Element{
el.tag,
attrs,
cs,
&fmt,
}
}
func (el *Element) Tag() string {
return el.tag
}
func (el *Element) AddAttr(ks ...string) *Element {
for _, k := range ks {
el.attrs[k] = ""
}
return el
}
func (el *Element) SetAttr(k string, v string) *Element {
el.attrs[k] = v
return el
}
func (el *Element) SetAttrs(kvs map[string]string) *Element {
for k, v := range kvs {
el.attrs[k] = v
}
return el
}
func (el *Element) Attr(k string) string {
return el.attrs[k]
}
func (el *Element) SetID(id string) *Element {
el.SetAttr("id", id)
return el
}
func (el *Element) ID() string {
return el.Attr("id")
}
func (el *Element) AddClass(class string) *Element {
cur := el.Attr("class")
if cur == "" {
el.SetAttr("class", class)
return el
}
cur += " " + class
el.SetAttr("class", cur)
return el
}
func (el *Element) HasClass(class string) bool {
classes := el.Attr("class")
if classes == "" {
return false
}
for _, c := range strings.Split(classes, " ") {
if strings.Index(c, class) != -1 {
return true
}
}
return false
}
func (el *Element) AddChild(c ...Node) *Element {
el.cs = append(el.cs, c...)
return el
}
func (el *Element) Children() []Node {
return el.cs
}
func (el *Element) ChildN(n int) Node {
if len(el.cs) <= n {
return nil
}
return el.cs[n]
}
func (el *Element) SetFormat(fmt *Format) {
el.fmt = fmt
}
func (el *Element) Format() *Format {
return el.fmt
}
func (el *Element) String() string {
sattrs := ""
for k, v := range el.attrs {
if v == "" {
sattrs += el.fmt.AttrSep + el.fmt.AttrKFmt(k)
} else {
sattrs += el.fmt.AttrSep + el.fmt.AttrKFmt(k) + "=" +
el.fmt.AttrVFmt(v)
}
}
s := el.fmt.Prefix + el.fmt.TagOpen + el.fmt.TagFmt(el.tag) + sattrs +
el.fmt.TagClose
// Single tag without children
if len(el.cs) == 0 && el.fmt.Close {
return s
}
s += el.fmt.AttrSep
for _, c := range el.cs {
s += el.fmt.CPrefix + c.String() + el.fmt.Sep
}
s += el.fmt.Prefix + el.fmt.EndTagOpen + el.fmt.TagFmt(el.tag) +
el.fmt.EndTagClose + el.fmt.Sep
return s
}
func OptFormat(fmt *Format) ElementOption {
return func(el *Element) {
el.fmt = fmt
}
}