-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (144 loc) · 3.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var fs = require('fs');
var htmlparser = require("htmlparser2");
var level = -1;
var output = "";
var recursiveVisit = function (dom, callback) {
if (dom instanceof Array) {
level++;
for (var i = 0; i < dom.length; i++) {
callback(dom[i], i);
recursiveVisit(dom[i].children, callback);
}
level--;
}
};
var isLastChild = function (item) {
if (!item.parent) {
return true;
}
var isLast = true;
var parentChilds = item.parent.children;
for (var i = item.index + 1; i < parentChilds.length; i++) {
if (parentChilds[i].type == "text") {
if (parentChilds[i].data.trim().length != 0) {
isLast = false;
break;
}
} else {
isLast = false;
break;
}
}
return isLast;
};
var styleStrToObj = function (style) {
var r = style.split(";")
.map(function (s) {
return s.trim();
})
.filter(function(s) {
return s.length > 0;
})
.map(function (s) {
return s.split(":").map(function (s) { return s.trim(); });
})
.filter(function (s) {
return s.length === 2;
})
.map(function (s) {
return '"' + s[0] + '": "' + s[1] + '"';
})
.join(", ");
return JSON.parse("{" + r + "}");
}
var tagToStr = function () {
var text = this.text;
var attr = this.attribs;
var childs = this.children.filter(function (ch) {
if (ch.type == "text") {
return ch.data.trim().length > 0;
} else {
return true;
}
return ch.type != "text";
});
if (attr && Object.keys(attr).length > 0) {
var props = null;
var clas = attr.class;
var style = attr.style;
delete attr.class;
delete attr.style;
if (Object.keys(attr).length > 0) {
props = attr;
}
attr = {};
if (clas) {
attr.class = {};
clas.split(" ").forEach(function (cls) {
attr.class[cls] = true;
})
}
if (style) {
attr.style = styleStrToObj(style);
}
if (props) {
attr.props = props;
}
attr = JSON.stringify(attr);
if (childs.length) {
attr += ", ";
}
} else {
attr = "";
}
var str = "";
str += this.ident;
str += 'h("' + this.name + '", ' + attr;
if (childs.length) {
str += "\n";
str += this.ident2;
str += "[\n";
str += childs.join("");
str += "\n";
str += this.ident2;
str += "]";
}
str += "\n";
str += this.ident;
str += ")";
if (isLastChild(this) == false) {
str += ",\n";
}
return str;
};
var textToStr = function () {
this.data = this.data.trim().replace(/\r/g, "");
this.data = this.data.replace(/\n/g, "\\\n");
return this.ident2 + "'" + this.data + "'";
};
var commentToStr = function () {
return this.ident2 + "/*" + this.data + "*/\n";
};
var handler = new htmlparser.DomHandler(function (error, dom) {
recursiveVisit(dom, function (obj, i) {
obj.ident = Array(level*4).join(" ");
obj.ident2 = Array((level+1)*4).join(" ");
obj.index = i;
if (obj.type == "tag") {
obj.toString = tagToStr;
} else if (obj.type == "text") {
obj.toString = textToStr;
} else if (obj.type == "comment") {
obj.toString = commentToStr;
} else {
throw "unknown type"
}
});
output = dom.toString();
});
module.exports = function (html) {
var parser = new htmlparser.Parser(handler);
parser.write(html);
parser.done();
return output;
};