-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_bolero.js
51 lines (44 loc) · 1.27 KB
/
dom_bolero.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
let accum = []
let indent = 0
const writeElement = (h) => {
if (h.tag == 'text') {
accum.push(`"${h.data}"\n`)
} else {
indent = indent + 4
accum.push(`${h.tag} {`)
let attrs = []
for (const property in h.attrs) {
let p = property
.trim()
.replace(/class$/, '``class``')
.replace(/type$/, '``type``')
const _attr = h.attrs[property]
if (p.includes('-')) {
//webcomponents
attrs.push(`"${p}" => "${_attr}"\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_bolero(h) {
accum = []
indent = 0
h.forEach((e) => writeElement(e))
return accum.join('')
}