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
58 lines (50 loc) · 1.7 KB
/
render.js
File metadata and controls
58 lines (50 loc) · 1.7 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
import { isFalse } from "../shared/nodes";
import {sanitizeHtml} from "../shared/sanitizeString";
export default function render(node, scope) {
if(isFalse(node)) {
return "<!---->";
}
if(node.type === undefined) {
return (sanitizeHtml(node.toString()) || ' ') + "<!--#-->";
}
let element = `<${node.type}`;
for(let name in node.attributes) {
if(!name.startsWith('on') && name !== 'html') {
const type = typeof(node.attributes[name]);
if(type !== 'object' && type !== 'function') {
if(name != 'value' && node.attributes[name] === true) {
element += ` ${name}`;
} else if(name == 'value' || (node.attributes[name] !== false && node.attributes[name] !== null && node.attributes[name] !== undefined)) {
element += ` ${name}="${node.attributes[name]}"`;
}
}
}
}
const selfClosing = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr', 'menuitem'].includes(node.type);
if(selfClosing && node.children.length === 0) {
element += '/>';
} else {
element += '>';
if(node.attributes.html) {
const source = node.attributes.html;
if(node.type === 'head') {
scope.head += source;
} else {
element += source;
}
} else if(node.type === 'textarea') {
element += node.children[0];
} else {
for(let i = 0; i < node.children.length; i++) {
const source = render(node.children[i], scope);
if(node.type === 'head') {
scope.head += source;
} else {
element += source;
}
}
}
element += `</${node.type}>`;
}
return node.type === 'head' ? '<!-- -->' : element;
}