-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathrender.js
More file actions
71 lines (62 loc) · 2.3 KB
/
render.js
File metadata and controls
71 lines (62 loc) · 2.3 KB
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
import { sanitizeInnerHtml } from '../shared/sanitizeString'
import generateTruthyString from '../shared/generateTruthyString'
import { isFalse, isText } from '../shared/nodes'
import { anchorableElement } from './anchorableNode'
import { generateCallback, generateSubject } from './events'
import { ref } from './ref'
export default function render(node, options) {
if (isFalse(node) || node.type === 'head') {
node.element = document.createComment('')
return node.element
}
if (isText(node)) {
node.element = document.createTextNode(node.text)
return node.element
}
const svg = (options && options.svg) || node.type === 'svg'
if (svg) {
node.element = document.createElementNS('http://www.w3.org/2000/svg', node.type)
} else {
node.element = document.createElement(node.type)
}
ref(node.attributes, node.element)
for (const name in node.attributes) {
if (name === 'debounce') continue
if (name === 'html') {
node.element.innerHTML = sanitizeInnerHtml(node.attributes[name])
node.head || anchorableElement(node.element)
} else if (name.startsWith('on')) {
if (node.attributes[name] !== undefined) {
const eventName = name.substring(2)
const callback = generateCallback(node.element, name)
node.element.addEventListener(eventName, callback)
generateSubject(node.element, node.attributes, name)
}
} else {
let nodeValue
if ((name === 'class' || name === 'style') && Array.isArray(node.attributes[name])) {
nodeValue = generateTruthyString(node.attributes[name])
} else {
nodeValue = node.attributes[name]
}
const type = typeof nodeValue
if (type !== 'object' && type !== 'function') {
if (name !== 'value' && nodeValue === true) {
node.element.setAttribute(name, '')
} else if (name === 'value' || (nodeValue !== false && nodeValue !== null && nodeValue !== undefined)) {
node.element.setAttribute(name, nodeValue)
}
}
}
}
if (!node.attributes.html) {
for (let i = 0; i < node.children.length; i++) {
const child = render(node.children[i], { svg })
node.element.appendChild(child)
}
if (node.type === 'select') {
node.element.value = node.attributes.value
}
}
return node.element
}