-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoElems.js
51 lines (45 loc) · 1.37 KB
/
toElems.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
export const toElemsById = (ast) => {
const byId = Object.create(null)
const elems = toElems(ast, byId)
return [elems, byId]
}
export const toElems = (ast, byId = Object.create(null)) => {
const {subjevkos, suffix} = ast
let ret = []
for (const {prefix, jevko} of subjevkos) {
const [pre, tag, post] = trim(prefix)
if (pre.length > 0) ret.push(document.createTextNode(pre))
if (tag === '') ret.push(...toElems(jevko, byId))
else if (tag.endsWith('=')) ret.push([tag.slice(0, -1), jevko])
else {
const elem = document.createElement(tag)
const elems = toElems(jevko, byId)
for (const e of elems) {
if (Array.isArray(e)) {
const [tag, jevko] = e
if (jevko.subjevkos.length > 0) throw Error('attrib must be suffix-only')
elem.setAttribute(tag, jevko.suffix)
if (tag === 'id') byId[jevko.suffix] = elem
} else {
elem.append(e)
}
}
ret.push(elem)
}
}
ret.push(document.createTextNode(suffix))
return ret
}
const trim = (prefix) => {
let i = 0, j = 0
for (; i < prefix.length; ++i) {
if (isWhitespace(prefix[i]) === false) break
}
for (j = i; j < prefix.length; ++j) {
if (isWhitespace(prefix[j])) break
}
return [prefix.slice(0, i), prefix.slice(i, j), prefix.slice(j)]
}
const isWhitespace = (c) => {
return ' \n\r\t'.includes(c)
}