-
Notifications
You must be signed in to change notification settings - Fork 3
/
light.js
71 lines (60 loc) · 1.54 KB
/
light.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
var parse = require("html-parse-stringify").parse;
var IncrementalDOM = require("incremental-dom");
var elementOpen = IncrementalDOM.elementOpen;
var elementClose = IncrementalDOM.elementClose;
var elementVoid = IncrementalDOM.elementVoid;
var text = IncrementalDOM.text;
var patch = IncrementalDOM.patch;
/**
* build IDOM for ast node
* @private
* @param {Object} node - An AST node to render
*/
function renderAstNode(node) {
if (node.type == "text") {
text(node.content);
}
if (node.type == "tag") {
var argsArray = [node.name, null, null];
// convert attribs object into a flat array
for (var attr in node.attrs) {
argsArray.push(attr);
argsArray.push(node.attrs[attr]);
}
if (node.voidElement) {
elementVoid.apply(null, argsArray);
} else {
elementOpen.apply(null, argsArray);
for (var i = 0, len = node.children.length; i < len; i++) {
renderAstNode(node.children[i]);
}
elementClose(node.name);
}
}
}
/**
* render function for IDOM that takes a string of HTML
* @param {String} html - The string of HTML to render
*/
function renderToIDom(html) {
var ast = parse(html);
if (Array.isArray(ast)) {
ast.forEach(renderAstNode);
} else {
renderAstNode(ast);
}
};
/**
* apply the HTML to an element via Incremental DOM's `patch`
* @param {Element} el - The element to apply the patch to
* @param {String} html - A string of HTML
*/
function patchHTML(el, html) {
patch(el, function() {
return renderToIDom(html);
});
}
module.exports = {
renderToIDom: renderToIDom,
patchHTML: patchHTML
}