stringify.js 607 B

12345678910111213141516171819202122
  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) || elem.type === ElementType.CDATA) return getText(elem.children);
  17. if(elem.type === ElementType.Text) return elem.data;
  18. return "";
  19. }