forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
68 lines (57 loc) · 1.93 KB
/
render.js
File metadata and controls
68 lines (57 loc) · 1.93 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
import { isFalse, isText } from '../shared/nodes';
import { anchorableElement } from './anchorableNode';
export default function render(node, options) {
if (isFalse(node) || node.type === 'head') {
return document.createComment("");
}
if (isText(node)) {
return document.createTextNode(node);
}
const svg = (options && options.svg) || node.type === 'svg';
let element;
if (svg) {
element = document.createElementNS("http://www.w3.org/2000/svg", node.type);
} else {
element = document.createElement(node.type);
}
if (node.instance) {
node.instance._self.element = element;
}
for (let name in node.attributes) {
if (name === 'html') {
element.innerHTML = node.attributes[name];
anchorableElement(element);
} else if (name.startsWith('on')) {
if (node.attributes[name] !== undefined) {
const eventName = name.replace('on', '');
const key = '_event.' + eventName;
node[key] = (event) => {
if (node.attributes.default !== true) {
event.preventDefault();
}
node.attributes[name]({ ...node.attributes, event });
};
element.addEventListener(eventName, node[key]);
}
} else {
const type = typeof (node.attributes[name]);
if (type !== 'object' && type !== 'function') {
if (name != 'value' && node.attributes[name] === true) {
element.setAttribute(name, '');
} else if (name == 'value' || (node.attributes[name] !== false && node.attributes[name] !== null && node.attributes[name] !== undefined)) {
element.setAttribute(name, node.attributes[name]);
}
}
}
}
if (!node.attributes.html) {
for (let i = 0; i < node.children.length; i++) {
const child = render(node.children[i], { svg });
element.appendChild(child);
}
if (node.type == 'select') {
element.value = node.attributes.value;
}
}
return element;
}