/
element.go
401 lines (338 loc) · 8.35 KB
/
element.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package wikifier
import (
htmlfmt "html"
"strconv"
"strings"
)
var identifiers = make(map[string]int)
// HTML encapsulates a string to indicate that it is preformatted HTML.
// It lets quiki's parsers know not to attempt to format it any further.
type HTML string
type element interface {
// ID
id() string
// tag/type
tag() string
setTag(tag string)
elementType() string
// attributes
hasAttr(name string) bool
attr(name string) string
boolAttr(name string) bool
setAttr(name, value string)
setBoolAttr(name string, value bool)
// styles
hasStyle(name string) bool
style(name string) string
setStyle(name, value string)
// metadata
meta(name string) bool
setMeta(name string, value bool)
// adding content
add(i interface{})
addText(s string)
addHTML(h HTML)
addChild(child element)
createChild(tag, typ string) element
// classes
addClass(class ...string)
removeClass(class string) bool
// parent
parent() element
setParent(parent element)
// invisibility
hide()
hidden() bool
// html generation
generate() HTML
generateIndented(indent int) []indentedLine
}
type genericElement struct {
_tag string // html tag
_id string // unique element identifier
attrs map[string]interface{} // html attributes
styles map[string]string // inline styles
metas map[string]bool // metadata
typ string // primary quiki class
classes []string // quiki user-defined classes
content []interface{} // mixed text and child elements
parentElement element // parent element, if any
cachedHTML HTML // cached version
shouldHide bool // whether to hide the element
}
func newElement(tag, typ string) element {
identifiers[typ]++
return &genericElement{
_tag: tag,
_id: typ + "-" + strconv.Itoa(identifiers[typ]),
typ: typ,
attrs: make(map[string]interface{}),
styles: make(map[string]string),
metas: make(map[string]bool),
}
}
// fetch ID
func (el *genericElement) id() string {
return el._id
}
// fetch tag
func (el *genericElement) tag() string {
return el._tag
}
// set the tag
func (el *genericElement) setTag(tag string) {
el._tag = tag
}
// fetch type
func (el *genericElement) elementType() string {
return el.typ
}
// fetch string value for a meta
func (el *genericElement) meta(name string) bool {
return el.metas[name]
}
// set string value for a meta
func (el *genericElement) setMeta(name string, value bool) {
if value == false {
delete(el.metas, name)
return
}
el.metas[name] = value
}
// true when an attr is present on an element
func (el *genericElement) hasAttr(name string) bool {
_, exist := el.attrs[name]
return exist
}
// fetch string value for an attribute
func (el *genericElement) attr(name string) string {
attr, exist := el.attrs[name]
if !exist {
return ""
}
if attrStr, ok := attr.(string); ok {
return attrStr
}
return ""
}
// fetch boolean value for an attribute
func (el *genericElement) boolAttr(name string) bool {
attr, exist := el.attrs[name]
if !exist {
return false
}
if attrBool, ok := attr.(bool); ok {
return attrBool
}
return false
}
// set a string attribute
func (el *genericElement) setAttr(name, value string) {
if value == "" {
delete(el.attrs, name)
return
}
el.attrs[name] = value
}
// set a boolean attribute
func (el *genericElement) setBoolAttr(name string, value bool) {
if value == false {
delete(el.attrs, name)
return
}
el.attrs[name] = true
}
// true when a style key is present on an element
func (el *genericElement) hasStyle(name string) bool {
_, exist := el.styles[name]
return exist
}
// fetch string value for a style
func (el *genericElement) style(name string) string {
return el.styles[name]
}
// set string value for a style
func (el *genericElement) setStyle(name, value string) {
el.styles[name] = value
}
// add something
func (el *genericElement) add(i interface{}) {
switch v := i.(type) {
case string:
el.addText(v)
case HTML:
el.addHTML(v)
case element:
el.addChild(v)
case []interface{}:
for _, val := range v {
el.add(val)
}
case block:
panic("add() block " + v.blockType() + "{} to element " + el.elementType())
default:
panic("add() unknown type to element " + el.elementType())
}
}
// add a text node
func (el *genericElement) addText(s string) {
el.content = append(el.content, s)
}
// add inner html
func (el *genericElement) addHTML(h HTML) {
el.content = append(el.content, h)
}
// add child element
func (el *genericElement) addChild(child element) {
el.content = append(el.content, child)
}
// create a child element and add it
func (el *genericElement) createChild(tag, typ string) element {
child := newElement(tag, typ)
el.addChild(child)
return child
}
// fetch element's parent
func (el *genericElement) parent() element {
return el.parentElement
}
// set this element's parent (internal only)
func (el *genericElement) setParent(parent element) {
el.parentElement = parent // recursive!!
}
// add a class
func (el *genericElement) addClass(class ...string) {
el.classes = append(el.classes, class...)
}
// remove a class, returning true if it was present
func (el *genericElement) removeClass(class string) bool {
for i, v := range el.classes {
if v == class {
el.classes = append(el.classes[:i], el.classes[i+1:]...)
return true
}
}
return false
}
func (el *genericElement) hide() {
el.shouldHide = true
}
func (el *genericElement) hidden() bool {
return el.shouldHide
}
func (el *genericElement) generate() HTML {
// cached version
if el.cachedHTML != "" {
return el.cachedHTML
}
el.cachedHTML = generateIndentedLines(el.generateIndented(0))
return el.cachedHTML
}
type indentedLine struct {
line string
indent int
}
func (el *genericElement) generateIndented(indent int) []indentedLine {
var lines []indentedLine
if el.hidden() {
return nil
}
// tags
if !el.meta("noTags") {
openingTag := "<" + el._tag
// classes
var classes []string
if el.typ == "" {
classes = make([]string, len(el.classes))
} else {
classes = make([]string, len(el.classes)+1)
classes[0] = "q-" + el.typ
}
for i, name := range el.classes {
if name[0] == '!' {
name = name[1:]
classes[i+1] = name
} else {
classes[i+1] = "q-" + name
}
}
// inject ID
if el.meta("needID") {
classes = append([]string{"q-" + el._id}, classes...)
}
if len(classes) != 0 {
openingTag += ` class="` + strings.Join(classes, " ") + `"`
}
// styles
styles := ""
for key, val := range el.styles {
styles += key + ": " + val + "; "
}
if styles != "" {
openingTag += ` style="` + strings.TrimSpace(styles) + `"`
}
// other attributes
for key, val := range el.attrs {
switch v := val.(type) {
case string:
openingTag += " " + key + `="` + htmlfmt.EscapeString(v) + `"`
case bool:
openingTag += " " + key
}
}
// non-container
if el.meta("nonContainer") {
lines = append(lines, indentedLine{openingTag + " />", indent})
return lines
}
// container
lines = append(lines, indentedLine{openingTag + ">", indent})
}
// inner content
for _, textOrEl := range el.content {
var addLines []indentedLine
switch v := textOrEl.(type) {
case element:
if v.meta("invisible") {
break
}
if v.meta("noIndent") {
addLines = v.generateIndented(0)
} else {
addLines = v.generateIndented(indent + 1)
}
case string:
myIndent := indent + 1
if el.meta("noIndent") {
myIndent = 0
}
addLines = []indentedLine{indentedLine{htmlfmt.EscapeString(v), myIndent}}
case HTML:
myIndent := indent + 1
if el.meta("noIndent") {
myIndent = 0
}
addLines = []indentedLine{indentedLine{string(v), myIndent}}
}
lines = append(lines, addLines...)
}
// close it off
if !el.meta("noTags") && !el.meta("noClose") {
lines = append(lines, indentedLine{"</" + el._tag + ">", indent})
}
return lines
}
func generateIndentedLines(lines []indentedLine) HTML {
generated := ""
for _, line := range lines {
generated += strings.Repeat(" ", line.indent) + line.line
// if len(lines) >= i+2 && lines[i+1].indent == 0 {
// // next peice is noIndent, so no newline before either
// continue
// }
if line.line == "" || line.line[len(line.line)-1] != '\n' {
generated += "\n"
}
}
return HTML(generated)
}