stringify.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.innerText = exports.textContent = exports.getText = exports.getInnerHTML = exports.getOuterHTML = void 0;
  7. var domhandler_1 = require("domhandler");
  8. var dom_serializer_1 = __importDefault(require("dom-serializer"));
  9. var domelementtype_1 = require("domelementtype");
  10. /**
  11. * @category Stringify
  12. * @deprecated Use the `dom-serializer` module directly.
  13. * @param node Node to get the outer HTML of.
  14. * @param options Options for serialization.
  15. * @returns `node`'s outer HTML.
  16. */
  17. function getOuterHTML(node, options) {
  18. return (0, dom_serializer_1.default)(node, options);
  19. }
  20. exports.getOuterHTML = getOuterHTML;
  21. /**
  22. * @category Stringify
  23. * @deprecated Use the `dom-serializer` module directly.
  24. * @param node Node to get the inner HTML of.
  25. * @param options Options for serialization.
  26. * @returns `node`'s inner HTML.
  27. */
  28. function getInnerHTML(node, options) {
  29. return (0, domhandler_1.hasChildren)(node)
  30. ? node.children.map(function (node) { return getOuterHTML(node, options); }).join("")
  31. : "";
  32. }
  33. exports.getInnerHTML = getInnerHTML;
  34. /**
  35. * Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
  36. *
  37. * @category Stringify
  38. * @deprecated Use `textContent` instead.
  39. * @param node Node to get the inner text of.
  40. * @returns `node`'s inner text.
  41. */
  42. function getText(node) {
  43. if (Array.isArray(node))
  44. return node.map(getText).join("");
  45. if ((0, domhandler_1.isTag)(node))
  46. return node.name === "br" ? "\n" : getText(node.children);
  47. if ((0, domhandler_1.isCDATA)(node))
  48. return getText(node.children);
  49. if ((0, domhandler_1.isText)(node))
  50. return node.data;
  51. return "";
  52. }
  53. exports.getText = getText;
  54. /**
  55. * Get a node's text content.
  56. *
  57. * @category Stringify
  58. * @param node Node to get the text content of.
  59. * @returns `node`'s text content.
  60. * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent}
  61. */
  62. function textContent(node) {
  63. if (Array.isArray(node))
  64. return node.map(textContent).join("");
  65. if ((0, domhandler_1.hasChildren)(node) && !(0, domhandler_1.isComment)(node)) {
  66. return textContent(node.children);
  67. }
  68. if ((0, domhandler_1.isText)(node))
  69. return node.data;
  70. return "";
  71. }
  72. exports.textContent = textContent;
  73. /**
  74. * Get a node's inner text.
  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. exports.innerText = innerText;
  92. //# sourceMappingURL=stringify.js.map