-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrenderer-example.js
57 lines (56 loc) · 1.92 KB
/
renderer-example.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
/** Renders browser DOM using some other DOM.
* @namespace
*/
var htmlRenderer = /** @lends htmlRenderer. */ {
/** Render a DOM Node or document into a node.
* @param n A Node or Document
* @param into A node to append the constructed elements to
* @returns {HTMLElement}
*/
render : function node(n, into, options) {
var i, d;
options = options || {};
if (!n) return false;
if (Object.prototype.toString.call(n)==='[object Array]') {
for (i=0; i<n.length; i++) {
node(n[i], into, options);
}
}
else if (n.nodeName==='#document') {
node(n.childNodes, into, options);
}
else {
if (n.nodeType===3) {
d = document.createTextNode(n.textContent);
}
else if (n.nodeType===4) {
d = document.createComment(' <![CDATA['+n.textContent+']]> ');
}
else if (n.nodeType===8) {
d = document.createComment(n.textContent);
}
else {
if (options.blacklist && options.blacklist.indexOf(n.nodeName)>-1) {
return false;
}
if (options.whitelist && options.whitelist.indexOf(n.nodeName)===-1) {
return false;
}
d = (options.document || document).createElement(n.nodeName);
for (i in n.attributes) {
if (n.attributes.hasOwnProperty(i)) {
d.setAttribute(i, n.attributes[i]);
}
}
if (n.childNodes && n.childNodes.length>0) {
node(n.childNodes, d, options);
}
else {
d.innerHTML = n.innerHTML;
}
}
into.appendChild(d);
}
return d;
}
};