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.

23 lines
668 B

4 years ago
  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. }