-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
55 lines (54 loc) · 1.4 KB
/
index.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
var parser = require('./parser');
var parseStyle = function(el){
var style = el.style;
var output = {};
for (var i = 0; i < style.length; ++i) {
var item = style.item(i);
output[item] = style[item];
}
return output;
};
var parseDOM = function(el){
if(!el.tagName && el.nodeType === Node.TEXT_NODE){
return JSON.stringify(el.textContent);
}
if(!el.attributes){
return;
}
var attributes = {};
for(var i = 0; i < el.attributes.length; i++){
var attr = el.attributes[i];
if(attr.name && typeof attr.value !== "undefined"){
if(attr.name == "style"){
attributes.style = parseStyle(el);
}
else{
attributes[attr.name] = attr.value;
}
}
}
var output = "h('" + el.tagName;
if(attributes.id){
output = output +'#'+ attributes.id;
delete attributes.id;
}
if(attributes.class){
output = output +'.'+ attributes.class.replace(/ /g,".");
delete attributes.class;
}
output += "',";
output += JSON.stringify(attributes);
var children = [];
output += ',[';
for(var i = 0; i < el.childNodes.length; i++){
output += parseDOM(el.childNodes[i]) + ",";
}
output += "])";
return output;
};
var parseHTML = function(html){
return parseDOM(parser(html));
};
exports.parseDOM = parseDOM;
exports.parseHTML = parseHTML;
module.exports = exports;