stringify.js 668 B

1234567891011121314151617181920212223
  1. var ElementType = require("domelementtype"),
  2. getOuterHTML = require("dom-serializer"),
  3. isTag = ElementType.isTag;
  4. module.exports = {
  5. getInnerHTML: getInnerHTML,
  6. getOuterHTML: getOuterHTML,
  7. getText: getText
  8. };
  9. function getInnerHTML(elem, opts){
  10. return elem.children ? elem.children.map(function(elem){
  11. return getOuterHTML(elem, opts);
  12. }).join("") : "";
  13. }
  14. function getText(elem){
  15. if(Array.isArray(elem)) return elem.map(getText).join("");
  16. if(isTag(elem)) return elem.name === "br" ? "\n" : getText(elem.children);
  17. if(elem.type === ElementType.CDATA) return getText(elem.children);
  18. if(elem.type === ElementType.Text) return elem.data;
  19. return "";
  20. }