-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrenderer.js
29 lines (29 loc) · 1.3 KB
/
renderer.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function render(element) {
if (element == null)
return '';
if (typeof element !== "object")
element = String(element);
if (typeof element === "string")
return element.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
//if (element instanceof Raw) return element.html;
console.assert(!!element.attributes, 'Element attributes must be defined:\n' + JSON.stringify(element));
var elementAttributes = element.attributes;
var attributes = Object.keys(elementAttributes).filter(function (key) {
var value = elementAttributes[key];
return value != null;
}).map(function (key) {
var value = elementAttributes[key];
if (value === true) {
return key;
}
return key + "=\"" + String(value).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"') + "\"";
}).join(' ');
if (attributes.length > 0) {
attributes = ' ' + attributes;
}
var children = element.children.length > 0 ? ">" + element.children.map(function (child) { return render(child); }).join('') : '>';
return "<" + element.name + attributes + children + "</" + element.name + ">";
}
exports.render = render;