forked from HenrikJoreteg/html-parse-stringify
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstringify.js
More file actions
29 lines (27 loc) · 744 Bytes
/
stringify.js
File metadata and controls
29 lines (27 loc) · 744 Bytes
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
function attrString(attrs) {
var buff = [];
for (var key in attrs) {
buff.push(key + '=' + attrs[key]);
}
if (!buff.length) {
return '';
}
return ' ' + buff.join(' ');
}
function stringify(buff, doc) {
switch (doc.type) {
case 'text':
return buff + doc.content;
case 'tag':
buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>');
if (doc.voidElement) {
return buff;
}
return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>';
}
}
module.exports = function (doc) {
return doc.reduce(function (token, rootEl) {
return token + stringify('', rootEl);
}, '');
};