-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtml.go
433 lines (400 loc) · 13.8 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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package zui
import (
"errors"
"slices"
"strconv"
"strings"
"time"
"unicode"
"golang.org/x/net/html"
"github.com/tdewolff/parse/v2"
"github.com/tdewolff/parse/v2/js"
)
var (
replDashToUnderscore = strings.NewReplacer("-", "_")
replRemoveBrCloseTag = strings.NewReplacer("</br>", "")
)
func htmlSrc(node *html.Node) string {
if node == nil {
return ""
}
switch node.Type {
case html.TextNode:
return node.Data
case html.CommentNode:
return "<!--" + node.Data + "-->"
case html.ElementNode, html.DocumentNode:
ret := "<" + node.Data
for _, attr := range node.Attr {
ret += " " + attr.Key + "=" + strQ(attr.Val)
}
if node.FirstChild == nil {
ret += " />"
} else {
ret += ">"
for child_node := node.FirstChild; child_node != nil; child_node = child_node.NextSibling {
ret += htmlSrc(child_node)
}
ret += "</" + node.Data + ">"
}
return ret
}
return "<?>"
}
func htmlAttr(node *html.Node, attrName string) string {
for _, attr := range node.Attr {
if attr.Key == attrName {
return attr.Val
}
}
return ""
}
func htmlTextIsFullyWhitespace(s string) bool {
for _, r := range s {
if !unicode.IsSpace(r) {
return false
}
}
return true
}
func htmlNumTagNodes(parentNode *html.Node) (n int) {
if parentNode != nil {
for node := parentNode.FirstChild; node != nil; node = node.NextSibling {
if node.Type == html.ElementNode && node.Data != "" {
n++
}
}
}
return
}
func htmlReplaceNode(parentNode *html.Node, oldChildNode *html.Node, newChildNodes ...*html.Node) {
for i, new_child_node := range newChildNodes {
if i < len(newChildNodes)-1 {
new_child_node.NextSibling = newChildNodes[i+1]
}
if i > 0 {
new_child_node.PrevSibling = newChildNodes[i-1]
}
}
for old_child_node := parentNode.FirstChild; old_child_node != nil; old_child_node = old_child_node.NextSibling {
if old_child_node == oldChildNode {
if idx := 0; old_child_node.PrevSibling != nil {
old_child_node.PrevSibling.NextSibling = newChildNodes[idx]
newChildNodes[idx].PrevSibling = old_child_node.PrevSibling
}
if idx := len(newChildNodes) - 1; old_child_node.NextSibling != nil {
old_child_node.NextSibling.PrevSibling = newChildNodes[idx]
newChildNodes[idx].NextSibling = old_child_node.NextSibling
}
}
}
}
func (me *zui2js) htmlFixupSelfClosingZuiTagsPriorToParsing() (string, error) {
src_htm, zuiTagName := me.zuiFileSrcOrig, "zui_"+me.zuiFileIdent
for {
idx_close := strings.Index(src_htm, "/>")
if idx_close < 0 {
break
}
idx_open := strings.LastIndexByte(src_htm[:idx_close], '<')
if idx_open < 0 {
break
}
name := src_htm[idx_open+1 : idx_close]
if name == "" || !((name[0] >= 'a' && name[0] <= 'z') || (name[0] >= 'A' && name[0] <= 'Z')) {
return "", errors.New(me.zuiFilePath + ": invalid tag name '" + name + "'")
}
idx_space := strings.IndexFunc(name, unicode.IsSpace)
if idx_space > 0 {
name = name[:idx_space]
}
new_name := name
if name[0] >= 'A' && name[0] <= 'Z' {
new_name = zuiTagName
}
src_htm = src_htm[:idx_open] + "<" + new_name +
ıf(name == new_name, "", " zui-tag-name='"+name+"'") +
src_htm[idx_open+1+len(name):idx_close] + "></" + new_name + ">" + src_htm[idx_close+len("/>"):]
}
src_htm = replRemoveBrCloseTag.Replace(src_htm) // html parser oddity: it would turn <br></br> into <br/><br/> (though it won't for img, hr, etc.)
return src_htm, nil
}
func (me *zui2js) htmlTopLevelScriptElement(hayStack *html.Node, curScript *html.Node) (*html.Node, error) {
for node := hayStack.FirstChild; node != nil; node = node.NextSibling {
if node.Type == html.ElementNode && node.Data == "script" {
if curScript != nil {
return nil, errors.New(me.zuiFilePath + ": A component can only have one top-level <script> element")
}
curScript = node
}
}
return curScript, nil
}
type htmlTextAndExprsSplitItem struct {
text string
jsExpr js.INode
jsExprAsHtml bool
jsTopLevelRefs []string
jsBlockKind BlockKind
}
func (me *zui2js) htmlSplitTextAndJSExprs(htmlText string) (ret []htmlTextAndExprsSplitItem, _ error) {
html_text_orig := angleBracketSentinelReplUndo.Replace(htmlText) // for error messages only
for {
idx_open := strings.IndexByte(htmlText, '{')
if idx_open < 0 {
if htmlText != "" {
ret = append(ret, htmlTextAndExprsSplitItem{text: htmlText})
}
return
}
n, idx_close := 0, -1
for i := idx_open + 1; i < len(htmlText); i++ {
if htmlText[i] == '{' {
n++
} else if htmlText[i] == '}' {
if n == 0 {
idx_close = i // now idx of the `}`
break
} else {
n--
}
}
}
if idx_close < idx_open {
if htmlText != "" {
ret = append(ret, htmlTextAndExprsSplitItem{text: htmlText})
}
return
}
prev := htmlText[:idx_open]
cur := htmlText[idx_open : idx_close+1]
next := htmlText[idx_close+1:]
if prev != "" {
ret = append(ret, htmlTextAndExprsSplitItem{text: prev})
}
htmlText = next
src_js := strTrim(cur[:len(cur)-1][1:])
ret_item := htmlTextAndExprsSplitItem{jsExprAsHtml: strings.HasPrefix(src_js, "@html ") || strings.HasPrefix(src_js, "@html\t") || strings.HasPrefix(src_js, "@html\r") || strings.HasPrefix(src_js, "@html\n") || strings.HasPrefix(src_js, "@html\v") || strings.HasPrefix(src_js, "@html\f") || strings.HasPrefix(src_js, "@html\b")}
if ret_item.jsExprAsHtml {
src_js = strTrim(src_js[len("@html"):])
}
if src_js == "" {
return nil, errors.New(me.zuiFilePath + ": expression expected inside the '{}' in '" + html_text_orig + "'")
}
src_js = angleBracketSentinelReplUndo.Replace(src_js)
block_type, src_js, err := me.blocknessCheck(src_js)
if err != nil {
return nil, err
}
js_ast, err := js.Parse(parse.NewInputString(src_js), js.Options{Inline: true})
if err != nil {
return nil, errors.New(me.zuiFilePath + ": '" + err.Error() + "' in '" + src_js + "' near `" + html_text_orig + "`")
}
ret_item.jsTopLevelRefs, err = jsWalkAndRewriteTopLevelFuncAST(me, src_js, &js_ast.BlockStmt)
if err != nil {
return nil, err
}
ret_item.jsExpr, ret_item.jsBlockKind = js_ast, block_type
ret = append(ret, ret_item)
}
}
var (
angleBracketSentinelOpen = "__zui_abo_" + strconv.FormatInt(time.Now().UnixNano(), 36)
angleBracketSentinelClose = "__zui_abc_" + strconv.FormatInt(time.Now().UnixNano(), 36)
angleBracketSentinelReplDo = strings.NewReplacer("<", angleBracketSentinelOpen, ">", angleBracketSentinelClose)
angleBracketSentinelReplUndo = strings.NewReplacer(angleBracketSentinelOpen, "<", angleBracketSentinelClose, ">")
replApos = strings.NewReplacer("'", "'")
)
func htmlPreprocessCurlyAttrs(src string) string {
idx_start := 0
for {
idx_open := strings.Index(src[idx_start:], "={")
if idx_open < 0 {
break
}
idx_open = idx_open + idx_start + 1 // now idx of the `{`
n, idx_close := 0, -1
for i := idx_open + 2; i < len(src); i++ {
if src[i] == '{' {
n++
} else if src[i] == '}' {
if n == 0 {
idx_close = i // now idx of the `}`
break
} else {
n--
}
}
}
if idx_close < idx_open {
break
}
prev := src[:idx_open]
cur := src[idx_open : idx_close+1]
next := src[idx_close+1:]
cur = replApos.Replace(angleBracketSentinelReplDo.Replace(cur))
src = prev + "'" + cur + "'"
idx_start = len(src)
src += next
}
return src
}
func (me *zui2js) nextFnName() string { me.idxFn++; return "f" + itoa(me.idxFn) }
func (me *zui2js) nextElName() string { me.idxFn++; return "e" + itoa(me.idxFn) }
func (me *zui2js) htmlWalkElemNodeAndEmitJS(node *html.Node, nodeParentVarName string) error {
const pref = "\n "
for child_node := node.FirstChild; child_node != nil; child_node = child_node.NextSibling {
switch child_node.Type {
case html.TextNode:
if err := me.htmlWalkTextNodeAndEmitJS(child_node, node, &nodeParentVarName); err != nil {
return err
}
case html.ElementNode:
if err := me.htmlWalkTagNodeAndEmitJS(child_node, nodeParentVarName); err != nil {
return err
}
}
}
me.WriteString(pref + nodeParentVarName + ".replaceChildren(...n_" + nodeParentVarName + ");")
return nil
}
func (me *zui2js) htmlWalkTextNodeAndEmitJS(curNode *html.Node, parentNode *html.Node, parentNodeVarName *string) error {
const pref = "\n "
if parentNode.Type == html.ElementNode && parentNode.Data == "style" {
me.WriteString(pref + "n_" + *parentNodeVarName + ".push(newT(" + strQ(curNode.Data) + "));")
return nil
}
parts, err := me.htmlSplitTextAndJSExprs(curNode.Data)
if err != nil {
return err
}
for _, part := range parts {
if part.text != "" {
me.WriteString(pref + "n_" + *parentNodeVarName + ".push(newT(" + strQ(ıf(htmlTextIsFullyWhitespace(part.text), " ", part.text)) + "));")
} else if part.jsExpr != nil {
js_src := strings.TrimSpace(strings.TrimSuffix(jsString(part.jsExpr), ";"))
if part.jsBlockKind != 0 {
if err = me.blockFragmentEmitJS(js_src, &part, parentNode, parentNodeVarName); err != nil {
return err
}
} else {
fn_name, span_var_name := me.nextFnName(), me.nextElName()
me.WriteString(pref + "const " + fn_name + " = " + me.jsFnCached("(() => ("+js_src+")).bind(this)", fn_name) + ";")
if part.jsExprAsHtml {
me.WriteString(pref + "const " + span_var_name + " = newE('span');")
me.WriteString(pref + span_var_name + ".innerHTML = " + fn_name + "();")
} else {
me.WriteString(pref + "const " + span_var_name + " = newT(" + fn_name + "());")
}
for _, top_level_decl_name := range part.jsTopLevelRefs {
if !me.blockFnStackHasDep(top_level_decl_name) {
me.WriteString(pref + "this.zuiSub('" + top_level_decl_name + "', (() => { " + span_var_name + "." + ıf(part.jsExprAsHtml, "innerHTML", "nodeValue") + " = " + fn_name + "(); }));")
}
}
me.WriteString(pref + "n_" + *parentNodeVarName + ".push(" + span_var_name + ");")
}
}
}
return nil
}
func (me *zui2js) htmlWalkTagNodeAndEmitJS(curNode *html.Node, parentNodeVarName string) error {
const pref = "\n "
node_var_name := me.nextElName()
is_zui_tag := curNode.Data == ("zui_" + me.zuiFileIdent)
if is_zui_tag {
zui_tag_name := htmlAttr(curNode, "zui-tag-name")
assert(zui_tag_name != "")
zui_rel_file_path := me.imports[zui_tag_name]
if zui_rel_file_path == "" {
return errors.New(me.zuiFilePath + ": component '" + zui_tag_name + "' was not `import`ed")
}
me.WriteString(pref + "const " + node_var_name + " = newE(" + zui_tag_name + ".ZuiTagName);")
} else {
me.WriteString(pref + "const " + node_var_name + " = newE(" + strQ(curNode.Data) + ");")
}
me.WriteString(pref + "const n_" + node_var_name + " = [];")
for i := 0; i < len(curNode.Attr); i++ {
attr := curNode.Attr[i]
if strings.HasPrefix(attr.Key, "zui-") {
continue
}
spread := ""
if attr.Val == "" && strings.HasPrefix(attr.Key, "{") && strings.HasSuffix(attr.Key, "}") {
if attr_key := strTrim(attr.Key[:len(attr.Key)-1][1:]); strings.HasPrefix(attr_key, "...") {
spread = strTrim(attr_key[len("..."):])
} else {
attr.Val = attr.Key
attr.Key = attr_key
}
}
if spread != "" {
ref := "this." + ıf(slices.Contains(me.attrExports, spread), "", "#") + spread
me.WriteString(pref + "for (const prop in " + ref + ") {")
me.WriteString(pref + " this.zuiSet(" + node_var_name + ", prop, " + ref + "[prop]);")
me.WriteString(pref + "}")
} else {
parts, err := me.htmlSplitTextAndJSExprs(attr.Val)
if err != nil {
return err
}
fn_name, attr_val_js_expr, attr_val_js_funcs := "", "", ""
for _, part := range parts {
if part.jsExpr != nil {
attr_val_js_expr += ıf(attr_val_js_expr != "", " + ", "")
if part.jsExprAsHtml {
return errors.New(me.zuiFilePath + ": the '@html' special tag is not permitted in any attributes, including '" + attr.Key + "'")
}
js_src := strings.TrimSuffix(jsString(part.jsExpr), ";")
fn_name = me.nextFnName()
attr_val_js_funcs += (pref + "const " + fn_name + " = " + me.jsFnCached("(() => ("+js_src+")).bind(this)", fn_name) + ";")
attr_val_js_expr += " (" + fn_name + "()) "
} else if part.text != "" {
attr_val_js_expr += ıf(attr_val_js_expr != "", " + ", "")
attr_val_js_expr += strQ(part.text)
}
}
me.WriteString(attr_val_js_funcs)
if strings.Contains(attr.Key, ":") {
if len(parts) > 1 || (len(parts) > 0 && parts[0].jsExpr == nil) {
return errors.New(me.zuiFilePath + ": invalid directive attribute value in " + attr.Key + "='" + attr.Val + "'")
}
var part *htmlTextAndExprsSplitItem
if len(parts) > 0 {
part = &parts[0]
}
if add_attrs, err := me.doDirectiveAttr(&attr, curNode, node_var_name, fn_name, part); err != nil {
return err
} else {
curNode.Attr = append(curNode.Attr, add_attrs...)
}
} else {
fn_name_attr := me.nextFnName()
if len(parts) == 1 && parts[0].jsExpr != nil {
fn_name_attr = fn_name
} else {
me.WriteString(pref + "const " + fn_name_attr + " = " + me.jsFnCached("(() => ("+attr_val_js_expr+")).bind(this)", fn_name_attr) + ";")
}
me.WriteString(pref + "this.zuiSet(" + node_var_name + ", " + strQ(attr.Key) + ", " + fn_name_attr + "());")
attr_decl_sub_done := map[string]bool{}
for _, it := range me.blockFnStack {
for _, dep := range it.deps {
attr_decl_sub_done[dep] = true
}
}
for _, part := range parts {
for _, top_level_decl_name := range part.jsTopLevelRefs {
if !attr_decl_sub_done[top_level_decl_name] {
me.WriteString(pref + "this.zuiSub('" + top_level_decl_name + "', () => " + "this.zuiSet(" + node_var_name + ", " + strQ(attr.Key) + ", " + fn_name_attr + "()));")
attr_decl_sub_done[top_level_decl_name] = true
}
}
}
}
}
}
if err := me.htmlWalkElemNodeAndEmitJS(curNode, node_var_name); err != nil {
return err
}
me.WriteString(pref + "n_" + parentNodeVarName + ".push(" + node_var_name + ");")
return nil
}