stringify.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getOuterHTML = getOuterHTML;
  7. exports.getInnerHTML = getInnerHTML;
  8. exports.getText = getText;
  9. exports.textContent = textContent;
  10. exports.innerText = innerText;
  11. var domhandler_1 = require("domhandler");
  12. var dom_serializer_1 = __importDefault(require("dom-serializer"));
  13. var domelementtype_1 = require("domelementtype");
  14. /**
  15. * @category Stringify
  16. * @deprecated Use the `dom-serializer` module directly.
  17. * @param node Node to get the outer HTML of.
  18. * @param options Options for serialization.
  19. * @returns `node`'s outer HTML.
  20. */
  21. function getOuterHTML(node, options) {
  22. return (0, dom_serializer_1.default)(node, options);
  23. }
  24. /**
  25. * @category Stringify
  26. * @deprecated Use the `dom-serializer` module directly.
  27. * @param node Node to get the inner HTML of.
  28. * @param options Options for serialization.
  29. * @returns `node`'s inner HTML.
  30. */
  31. function getInnerHTML(node, options) {
  32. return (0, domhandler_1.hasChildren)(node)
  33. ? node.children.map(function (node) { return getOuterHTML(node, options); }).join("")
  34. : "";
  35. }
  36. /**
  37. * Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags. Ignores comments.
  38. *
  39. * @category Stringify
  40. * @deprecated Use `textContent` instead.
  41. * @param node Node to get the inner text of.
  42. * @returns `node`'s inner text.
  43. */
  44. function getText(node) {
  45. if (Array.isArray(node))
  46. return node.map(getText).join("");
  47. if ((0, domhandler_1.isTag)(node))
  48. return node.name === "br" ? "\n" : getText(node.children);
  49. if ((0, domhandler_1.isCDATA)(node))
  50. return getText(node.children);
  51. if ((0, domhandler_1.isText)(node))
  52. return node.data;
  53. return "";
  54. }
  55. /**
  56. * Get a node's text content. Ignores comments.
  57. *
  58. * @category Stringify
  59. * @param node Node to get the text content of.
  60. * @returns `node`'s text content.
  61. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent}
  62. */
  63. function textContent(node) {
  64. if (Array.isArray(node))
  65. return node.map(textContent).join("");
  66. if ((0, domhandler_1.hasChildren)(node) && !(0, domhandler_1.isComment)(node)) {
  67. return textContent(node.children);
  68. }
  69. if ((0, domhandler_1.isText)(node))
  70. return node.data;
  71. return "";
  72. }
  73. /**
  74. * Get a node's inner text, ignoring `<script>` and `<style>` tags. Ignores comments.
  75. *
  76. * @category Stringify
  77. * @param node Node to get the inner text of.
  78. * @returns `node`'s inner text.
  79. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText}
  80. */
  81. function innerText(node) {
  82. if (Array.isArray(node))
  83. return node.map(innerText).join("");
  84. if ((0, domhandler_1.hasChildren)(node) && (node.type === domelementtype_1.ElementType.Tag || (0, domhandler_1.isCDATA)(node))) {
  85. return innerText(node.children);
  86. }
  87. if ((0, domhandler_1.isText)(node))
  88. return node.data;
  89. return "";
  90. }
  91. //# sourceMappingURL=stringify.js.map