-
Notifications
You must be signed in to change notification settings - Fork 3
/
innerHTML.js
60 lines (48 loc) · 1.28 KB
/
innerHTML.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
define([
'./htmlToHierarchy',
'./attributeParser'
], function(htmlToHierarchy, attributeParser){
return function (html){
if(html.trim().indexOf('<') !== 0){
// just text
return html;
}
var
createElement = global.createElement,
tree = htmlToHierarchy(html),
nodeObject;
function createNode(opentag){
//console.log('createNode', opentag);
var
attrs = attributeParser(opentag),
nodeName = opentag.indexOf(' ')>-1 ?
opentag.substring(1,opentag.indexOf(' ')) :
opentag.substring(1,opentag.indexOf('>')),
node = createElement(nodeName);
node.attributes = attrs;
return node;
}
function walkTree(branch){
var
i,
node = createNode(branch.opentag);
if(branch.innerText){
node.textContent = branch.innerText;
}
if(branch.children && branch.children.length){
for(i = 0; i < branch.children.length; i++){
if(branch.children[i].opentag.indexOf('</') === -1){
node.appendChild(walkTree(branch.children[i]));
}
}
}
return node;
}
nodeObject = walkTree(tree);
//console.log('tree', tree);
//console.log('\n\nnodeObject:', nodeObject);
//console.log('attrs:', nodeObject.attributes);
//console.log('style:', nodeObject.style);
return nodeObject;
};
});