stringify.js 2.9 KB

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