-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom_websharper.js
60 lines (55 loc) · 1.6 KB
/
dom_websharper.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
let accum = []
let indent = 0
const writeElement = (h) => {
indent = indent + 2
if (h.tag == 'text') {
accum.push(`text "${h.data}"\n`)
} else {
accum.push(`${h.tag}`)
let attrs = []
for (const property in h.attrs) {
attrs.push(`attr.${property} "${h.attrs[property]}"\n`)
}
if (attrs.length > 0) {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[`)
accum.push(`\n`)
indent = indent + 2
accum.push(' '.repeat(indent))
accum.push(attrs.join(' '.repeat(indent)))
indent = indent - 2
accum.push(' '.repeat(indent))
accum.push(`]\n`)
} else {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[]\n`)
}
if (h.children?.length > 0) {
h.children?.forEach((e) => {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[`)
accum.push(`\n`)
indent = indent + 2
accum.push(' '.repeat(indent))
writeElement(e)
indent = indent - 2
accum.push(' '.repeat(indent))
accum.push(`]\n`)
})
} else {
accum.push(`\n`)
accum.push(' '.repeat(indent))
accum.push(`[]\n`)
}
}
indent = indent - 2
}
export function to_websharper(h) {
accum = []
indent = 0
h.forEach((e) => writeElement(e))
return accum.join('')
}