-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
tag.go
215 lines (177 loc) · 4.38 KB
/
tag.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
package tags
import (
"bytes"
"database/sql/driver"
"fmt"
"html/template"
"reflect"
"strings"
"time"
)
// void tags https://www.w3.org/TR/html5/syntax.html#void-elements
const voidTags = " area base br col embed hr img input keygen link meta param source track wbr "
// Body is a Tag inner content.
type Body interface{}
// EncodedBody is a Tag inner text content (HTML encoded).
type EncodedBody interface{}
// BeforeTag is content placed right before the Tag
type BeforeTag interface{}
// AfterTag is content placed right after the Tag
type AfterTag interface{}
// Tag describes a HTML tag meta data.
type Tag struct {
Name string
Options Options
Selected bool
Checked bool
BeforeTag []BeforeTag
Body []Body
EncodedBody []EncodedBody
AfterTag []AfterTag
}
// Append adds new Body part(s) after the current Tag inner contents.
func (t *Tag) Append(b ...Body) {
t.Body = append(t.Body, b...)
}
// Prepend adds new Body part(s) before the current Tag inner contents.
func (t *Tag) Prepend(b ...Body) {
t.Body = append(b, t.Body...)
}
type interfacer interface {
Interface() interface{}
}
type htmler interface {
HTML() template.HTML
}
func parseTagEmbed(b interface{}) string {
switch tb := b.(type) {
case htmler:
return fmt.Sprint(tb.HTML())
case fmt.Stringer:
return tb.String()
case interfacer:
val := tb.Interface()
if tb.Interface() == nil {
val = ""
}
return fmt.Sprint(val)
default:
return fmt.Sprint(tb)
}
}
func (t Tag) String() string {
bb := &bytes.Buffer{}
for _, bt := range t.BeforeTag {
bb.WriteString(parseTagEmbed(bt))
}
bb.WriteString("<")
bb.WriteString(t.Name)
if len(t.Options) > 0 {
bb.WriteString(" ")
bb.WriteString(t.Options.String())
}
if t.Selected {
bb.WriteString(" selected")
}
if t.Checked {
bb.WriteString(" checked")
}
if len(t.Body) > 0 {
bb.WriteString(">")
for _, b := range t.Body {
bb.WriteString(parseTagEmbed(b))
}
bb.WriteString("</")
bb.WriteString(t.Name)
bb.WriteString(">")
for _, at := range t.AfterTag {
bb.WriteString(parseTagEmbed(at))
}
return bb.String()
}
// EncodedBody is like Body but must be encoded
if len(t.EncodedBody) > 0 {
bb.WriteString(">")
for _, b := range t.EncodedBody {
bb.WriteString(template.HTMLEscapeString(parseTagEmbed(b)))
}
bb.WriteString("</")
bb.WriteString(t.Name)
bb.WriteString(">")
for _, at := range t.AfterTag {
bb.WriteString(parseTagEmbed(at))
}
return bb.String()
}
if !strings.Contains(voidTags, " "+t.Name+" ") {
bb.WriteString("></")
bb.WriteString(t.Name)
bb.WriteString(">")
for _, at := range t.AfterTag {
bb.WriteString(parseTagEmbed(at))
}
return bb.String()
}
bb.WriteString(" />")
for _, at := range t.AfterTag {
bb.WriteString(parseTagEmbed(at))
}
return bb.String()
}
// HTML gets the Tag string representation as a HTML template.
func (t Tag) HTML() template.HTML {
return template.HTML(t.String())
}
// New creates a new Tag with given name and options.
func New(name string, opts Options) *Tag {
tag := &Tag{
Name: name,
Options: opts,
}
if tag.Options["body"] != nil {
tag.Body = []Body{tag.Options["body"]}
delete(tag.Options, "body")
}
if tag.Options["encoded_body"] != nil {
tag.EncodedBody = []EncodedBody{tag.Options["encoded_body"]}
delete(tag.Options, "encoded_body")
}
if tag.Options["before_tag"] != nil {
tag.BeforeTag = []BeforeTag{tag.Options["before_tag"]}
delete(tag.Options, "before_tag")
}
if tag.Options["after_tag"] != nil {
tag.AfterTag = []AfterTag{tag.Options["after_tag"]}
delete(tag.Options, "after_tag")
}
if tag.Options["value"] != nil {
val := tag.Options["value"]
switch val.(type) {
case time.Time:
format := tag.Options["format"]
if format == nil || format.(string) == "" {
format = "2006-01-02"
}
delete(tag.Options, "format")
tag.Options["value"] = val.(time.Time).Format(format.(string))
default:
r := reflect.ValueOf(val)
if !r.IsValid() {
tag.Options["value"] = ""
}
i := r.Interface()
if dv, ok := i.(driver.Valuer); ok {
value, _ := dv.Value()
if value == nil {
tag.Options["value"] = ""
}
tag.Options["value"] = fmt.Sprintf("%v", value)
}
}
}
if tag.Options["selected"] != nil {
tag.Selected = template.HTMLEscaper(opts["value"]) == template.HTMLEscaper(opts["selected"])
delete(tag.Options, "selected")
}
return tag
}