generated from gouef/github-lib-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
236 lines (201 loc) · 4.67 KB
/
html.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package html
type Html struct {
attributes []*Attribute
children []interface{}
name string
empty bool
}
func newHtml() *Html {
return &Html{
empty: false,
attributes: []*Attribute{},
children: []interface{}{},
}
}
// El create html element
// Example:
//
// el := El("p")
func El(name string) *Html {
el := newHtml()
el.setName(name)
return el
}
// setName set html element tag name
func (h *Html) setName(name string) *Html {
h.name = name
return h.UpdateEmpty()
}
// GetName get html element name
func (h *Html) GetName() string {
return h.name
}
// UpdateEmpty update element status pair
func (h *Html) UpdateEmpty() *Html {
h.empty = isEmptyElement(h.name)
return h
}
// IsEmpty check if element is pair
func (h *Html) IsEmpty() bool {
return h.empty
}
// AddAttribute add attribute to element
// Example:
//
// el := El("div")
// el.AddAttribute("class", "container")
func (h *Html) AddAttribute(name string, value interface{}) *Html {
return h.addAttributeObject(NewAttribute(name, value))
}
// AddData add attributes with prefix data-* to element
// Example:
//
// el := El("div")
// el.AddData(map[string][string{"level": "35",})
func (h *Html) AddData(data map[string]string) *Html {
for name, value := range data {
h.AddAttribute("data-"+name, value)
}
return h
}
// AddId add id attribute to element
// Example:
//
// el := El("div")
// el.AddId("root")
func (h *Html) AddId(id string) *Html {
h.AddAttribute("id", id)
return h
}
func (h *Html) addAttributeObject(attribute *Attribute) *Html {
if existAttr := h.existAttribute(attribute); existAttr != nil {
h.attributes[*existAttr] = attribute
return h
}
h.attributes = append(h.attributes, attribute)
return h
}
func (h *Html) existAttribute(attribute *Attribute) *int {
for index, existingAttr := range h.attributes {
if existingAttr.name == attribute.name {
return &index
}
}
return nil
}
// AddAttributes add attributes to element
// Example:
//
// el := El("div")
//
// el.AddAttributes([]map[string]interface{}{
// {"class": "container"},
// {"id": "main"},
// })
func (h *Html) AddAttributes(attributes []map[string]interface{}) *Html {
for _, attr := range attributes {
for name, value := range attr {
h.AddAttribute(name, value)
}
}
return h
}
// GetAttributes get attributes of element
func (h *Html) GetAttributes() []*Attribute {
return h.attributes
}
// GetAttribute get single attribute of element
func (h *Html) GetAttribute(name string) *Attribute {
for _, attr := range h.attributes {
if attr.name == name {
return attr
}
}
return nil
}
// RemoveAttribute remove attribute from element
func (h *Html) RemoveAttribute(name string) *Html {
for i, attr := range h.attributes {
if attr.name == name {
h.attributes = append(h.attributes[:i], h.attributes[i+1:]...)
break
}
}
return h
}
// RemoveAttributes remove multiple attributes from element
func (h *Html) RemoveAttributes(names []string) *Html {
for _, name := range names {
h.RemoveAttribute(name)
}
return h
}
// AddHtml add html child to element
// Example:
//
// el := El("div")
// child := El("p").AddString("some text")
// el.AddHtml(child)
func (h *Html) AddHtml(child *Html) *Html {
return h.AddChild(child)
}
// AddHtmlChildren add html children to element
// Example:
//
// el := El("div")
// p := El("p").AddString("some text")
// div := El("div")
// children := []*html.Html{div, p}
// el.AddHtmlChildren(children)
func (h *Html) AddHtmlChildren(children []*Html) *Html {
for _, child := range children {
h.AddHtml(child)
}
return h
}
// AddString add html children to element
// Example:
//
// p := El("p").AddString("some text")
func (h *Html) AddString(str string) *Html {
var child interface{} = str
return h.AddChild(child)
}
// AddStringChildren add string children to element
// Example:
//
// el := El("div")
// children := []string{"some text", "<p>another text</p>"}
// el.AddHtmlChildren(children)
func (h *Html) AddStringChildren(strings []string) *Html {
for _, child := range strings {
h.AddString(child)
}
return h
}
// AddChild add mixed child to element
func (h *Html) AddChild(child interface{}) *Html {
switch v := child.(type) {
case *Html:
h.children = append(h.children, v)
case string:
h.children = append(h.children, v)
}
return h
}
// AddChildren add mixed children to element
func (h *Html) AddChildren(children []interface{}) *Html {
for _, child := range children {
h.AddChild(child)
}
return h
}
// GetChildren get html children of element
func (h *Html) GetChildren() []interface{} {
return h.children
}
// RemoveChildren clear children node of element
func (h *Html) RemoveChildren() *Html {
h.children = []interface{}{}
return h
}