You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

30 lines
751 B

  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. function stringify(buff, doc) {
  12. switch (doc.type) {
  13. case 'text':
  14. return buff + doc.content;
  15. case 'tag':
  16. buff += '<' + doc.name + (doc.attrs ? attrString(doc.attrs) : '') + (doc.voidElement ? '/>' : '>');
  17. if (doc.voidElement) {
  18. return buff;
  19. }
  20. return buff + doc.children.reduce(stringify, '') + '</' + doc.name + '>';
  21. }
  22. }
  23. module.exports = function (doc) {
  24. return doc.reduce(function (token, rootEl) {
  25. return token + stringify('', rootEl);
  26. }, '');
  27. };