-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_sutil.js
62 lines (55 loc) · 1.64 KB
/
dom_sutil.js
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
import { attrStringify } from './helper'
let accum = []
let indent = 0
const writeElement = (h) => {
if (h.tag == 'text') {
accum.push(`Html.text "${h.data}"\n`)
} else {
indent = indent + 4
if (h.tag.includes('-')) {
//webcomponents
accum.push(`Html.custom "${h.tag}" [`)
} else {
accum.push(`Html.${h.tag} [`)
}
let attrs = []
for (const attr in h.attrs) {
let p = attr
.trim()
.replace(/class$/, 'className')
.replace(/for$/, `for'`)
.replace(/type$/, "type'")
const _attr = attrStringify(h.attrs[attr])
if (p.includes('-')) {
//webcomponents
attrs.push(`Attr.custom("${p}", ${_attr})\n`)
} else {
if (p == "required") {
attrs.push(`Attr.${p} true\n`)
} else {
attrs.push(`Attr.${p} ${_attr}\n`)
}
}
}
if (attrs.length > 0) {
accum.push(`\n`)
accum.push(' '.repeat(indent))
} else {
accum.push(`\n`)
}
accum.push(attrs.join(' '.repeat(indent)))
h.children?.forEach((e) => {
accum.push(' '.repeat(indent))
writeElement(e)
})
indent = indent - 4
if (indent > 0) accum.push(' '.repeat(indent)) //TODO if exp not necessary. Just in case...
accum.push(`]\n`)
}
}
export function to_sutil(h) {
accum = []
indent = 0
h.forEach((e) => writeElement(e))
return accum.join('')
}