1 | function attrString(attrs) {
|
2 | var buff = [];
|
3 | for (var key in attrs) {
|
4 | buff.push(key + '="' + attrs[key] + '"');
|
5 | }
|
6 | if (!buff.length) {
|
7 | return '';
|
8 | }
|
9 | return ' ' + buff.join(' ');
|
10 | }
|
11 |
|
12 | function stringify(buff, doc) {
|
13 | switch (doc.type) {
|
14 | case 'text':
|
15 | return buff + doc.content;
|
16 | case 'tag':
|
17 | buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>');
|
18 | if (doc.voidElement) {
|
19 | return buff;
|
20 | }
|
21 | return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>';
|
22 | }
|
23 | }
|
24 |
|
25 | module.exports = function (doc) {
|
26 | return doc.reduce(function (token, rootEl) {
|
27 | return token + stringify('', rootEl);
|
28 | }, '');
|
29 | };
|