-
Notifications
You must be signed in to change notification settings - Fork 1
/
html.go
100 lines (91 loc) · 2.27 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
package jaws
import (
"html/template"
"io"
"strconv"
"github.com/linkdata/jaws/jid"
)
var singletonTags = map[string]struct{}{
"area": {},
"base": {},
"br": {},
"col": {},
"command": {},
"embed": {},
"hr": {},
"img": {},
"input": {},
"keygen": {},
"link": {},
"meta": {},
"param": {},
"source": {},
"track": {},
"wbr": {},
}
func needClosingTag(tag string) bool {
_, ok := singletonTags[tag]
return !ok
}
func appendAttrs(b []byte, attrs []template.HTMLAttr) []byte {
for _, s := range attrs {
if s != "" {
b = append(b, ' ')
b = append(b, s...)
}
}
return b
}
func WriteHtmlTag(w io.Writer, jid jid.Jid, htmlTag, typeAttr, valueAttr string, attrs []template.HTMLAttr) (err error) {
b := make([]byte, 0, 64)
b = jid.AppendStartTagAttr(b, htmlTag)
if typeAttr != "" {
b = append(b, ` type=`...)
b = strconv.AppendQuote(b, typeAttr)
}
if valueAttr != "" {
b = append(b, ` value=`...)
b = strconv.AppendQuote(b, valueAttr)
}
b = appendAttrs(b, attrs)
b = append(b, '>')
_, err = w.Write(b)
return
}
func WriteHtmlInput(w io.Writer, jid jid.Jid, typeAttr, valueAttr string, attrs []template.HTMLAttr) (err error) {
return WriteHtmlTag(w, jid, "input", typeAttr, valueAttr, attrs)
}
func WriteHtmlInner(w io.Writer, jid jid.Jid, htmlTag, typeAttr string, innerHtml template.HTML, attrs ...template.HTMLAttr) (err error) {
if err = WriteHtmlTag(w, jid, htmlTag, typeAttr, "", attrs); err == nil {
if innerHtml != "" || needClosingTag(htmlTag) {
if _, err = w.Write([]byte(innerHtml)); err == nil {
var b []byte
b = append(b, "</"...)
b = append(b, htmlTag...)
b = append(b, '>')
_, err = w.Write(b)
}
}
}
return
}
func WriteHtmlSelect(w io.Writer, jid jid.Jid, nba *NamedBoolArray, attrs []template.HTMLAttr) (err error) {
if err = WriteHtmlTag(w, jid, "select", "", "", attrs); err == nil {
var b []byte
nba.ReadLocked(func(nba []*NamedBool) {
for _, nb := range nba {
b = append(b, "\n<option value="...)
b = strconv.AppendQuote(b, nb.Name())
if nb.Checked() {
b = append(b, ` selected`...)
}
b = append(b, '>')
b = append(b, nb.Html()...)
b = append(b, "</option>"...)
}
})
b = append(b, "\n</select>"...)
_, err = w.Write(b)
}
return
}