-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
120 lines (113 loc) · 2.53 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
package jaws
import (
"html/template"
)
func getAttrsLen(attrs []string) (attrslen int) {
for _, s := range attrs {
if s != "" {
attrslen += 1 + len(s)
}
}
return
}
func appendAttrs(b []byte, attrs []string) []byte {
for _, s := range attrs {
if s != "" {
b = append(b, ' ')
b = append(b, s...)
}
}
return b
}
func HtmlInput(id, typ, val string, attrs ...string) template.HTML {
need := 11 + len(id) + 8 + len(typ) + 9 + len(val) + 1 + 1 + getAttrsLen(attrs) + 1
b := make([]byte, 0, need)
b = append(b, `<input jid="`...)
b = append(b, id...)
b = append(b, `" type="`...)
b = append(b, typ...)
if val != "" {
b = append(b, `" value="`...)
b = append(b, val...)
}
b = append(b, '"')
b = appendAttrs(b, attrs)
b = append(b, '>')
return template.HTML(b) // #nosec G203
}
var singletonTags = map[string]struct{}{
"area": {},
"base": {},
"br": {},
"col": {},
"command": {},
"embed": {},
"hr": {},
"img": {},
"input": {},
"keygen": {},
"link": {},
"meta": {},
"param": {},
"source": {},
"track": {},
"wbr": {},
}
func HtmlInner(id, tag, typ, inner string, attrs ...string) template.HTML {
need := 1 + len(tag)*2 + 5 + len(id) + 8 + len(typ) + 1 + 1 + getAttrsLen(attrs) + 1 + len(inner) + 2 + 1
b := make([]byte, 0, need)
b = append(b, '<')
b = append(b, tag...)
b = append(b, ` jid="`...)
b = append(b, id...)
if typ != "" {
b = append(b, `" type="`...)
b = append(b, typ...)
}
b = append(b, '"')
b = appendAttrs(b, attrs)
b = append(b, '>')
var singleton bool
if inner == "" {
_, singleton = singletonTags[tag]
}
if !singleton {
b = append(b, inner...)
b = append(b, "</"...)
b = append(b, tag...)
b = append(b, '>')
}
return template.HTML(b) // #nosec G203
}
func HtmlSelect(jid string, val *NamedBoolArray, attrs ...string) template.HTML {
need := 12 + len(jid) + 2 + getAttrsLen(attrs) + 2 + 10
if val != nil {
for _, nb := range *val {
need += 15 + len(nb.Value) + 2 + len(nb.Text) + 10
if nb.Checked {
need += 9
}
}
}
b := make([]byte, 0, need)
b = append(b, `<select jid="`...)
b = append(b, jid...)
b = append(b, '"')
b = appendAttrs(b, attrs)
b = append(b, ">\n"...)
if val != nil {
for _, nb := range *val {
b = append(b, `<option value="`...)
b = append(b, nb.Value...)
if nb.Checked {
b = append(b, `" selected>`...)
} else {
b = append(b, `">`...)
}
b = append(b, nb.Text...)
b = append(b, "</option>\n"...)
}
}
b = append(b, "</select>\n"...)
return template.HTML(b) // #nosec G203
}