2021-11-24 19:46:47 +11:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.innerText = exports.textContent = exports.getText = exports.getInnerHTML = exports.getOuterHTML = void 0;
|
|
|
|
var domhandler_1 = require("domhandler");
|
|
|
|
var dom_serializer_1 = __importDefault(require("dom-serializer"));
|
|
|
|
var domelementtype_1 = require("domelementtype");
|
|
|
|
/**
|
2022-09-03 17:12:22 +10:00
|
|
|
* @category Stringify
|
|
|
|
* @deprecated Use the `dom-serializer` module directly.
|
2021-11-24 19:46:47 +11:00
|
|
|
* @param node Node to get the outer HTML of.
|
|
|
|
* @param options Options for serialization.
|
|
|
|
* @returns `node`'s outer HTML.
|
|
|
|
*/
|
|
|
|
function getOuterHTML(node, options) {
|
|
|
|
return (0, dom_serializer_1.default)(node, options);
|
|
|
|
}
|
|
|
|
exports.getOuterHTML = getOuterHTML;
|
|
|
|
/**
|
2022-09-03 17:12:22 +10:00
|
|
|
* @category Stringify
|
|
|
|
* @deprecated Use the `dom-serializer` module directly.
|
2021-11-24 19:46:47 +11:00
|
|
|
* @param node Node to get the inner HTML of.
|
|
|
|
* @param options Options for serialization.
|
|
|
|
* @returns `node`'s inner HTML.
|
|
|
|
*/
|
|
|
|
function getInnerHTML(node, options) {
|
|
|
|
return (0, domhandler_1.hasChildren)(node)
|
|
|
|
? node.children.map(function (node) { return getOuterHTML(node, options); }).join("")
|
|
|
|
: "";
|
|
|
|
}
|
|
|
|
exports.getInnerHTML = getInnerHTML;
|
|
|
|
/**
|
|
|
|
* Get a node's inner text. Same as `textContent`, but inserts newlines for `<br>` tags.
|
|
|
|
*
|
2022-09-03 17:12:22 +10:00
|
|
|
* @category Stringify
|
2021-11-24 19:46:47 +11:00
|
|
|
* @deprecated Use `textContent` instead.
|
|
|
|
* @param node Node to get the inner text of.
|
|
|
|
* @returns `node`'s inner text.
|
|
|
|
*/
|
|
|
|
function getText(node) {
|
|
|
|
if (Array.isArray(node))
|
|
|
|
return node.map(getText).join("");
|
|
|
|
if ((0, domhandler_1.isTag)(node))
|
|
|
|
return node.name === "br" ? "\n" : getText(node.children);
|
|
|
|
if ((0, domhandler_1.isCDATA)(node))
|
|
|
|
return getText(node.children);
|
|
|
|
if ((0, domhandler_1.isText)(node))
|
|
|
|
return node.data;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
exports.getText = getText;
|
|
|
|
/**
|
|
|
|
* Get a node's text content.
|
|
|
|
*
|
2022-09-03 17:12:22 +10:00
|
|
|
* @category Stringify
|
2021-11-24 19:46:47 +11:00
|
|
|
* @param node Node to get the text content of.
|
|
|
|
* @returns `node`'s text content.
|
|
|
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent}
|
|
|
|
*/
|
|
|
|
function textContent(node) {
|
|
|
|
if (Array.isArray(node))
|
|
|
|
return node.map(textContent).join("");
|
|
|
|
if ((0, domhandler_1.hasChildren)(node) && !(0, domhandler_1.isComment)(node)) {
|
|
|
|
return textContent(node.children);
|
|
|
|
}
|
|
|
|
if ((0, domhandler_1.isText)(node))
|
|
|
|
return node.data;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
exports.textContent = textContent;
|
|
|
|
/**
|
|
|
|
* Get a node's inner text.
|
|
|
|
*
|
2022-09-03 17:12:22 +10:00
|
|
|
* @category Stringify
|
2021-11-24 19:46:47 +11:00
|
|
|
* @param node Node to get the inner text of.
|
|
|
|
* @returns `node`'s inner text.
|
|
|
|
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText}
|
|
|
|
*/
|
|
|
|
function innerText(node) {
|
|
|
|
if (Array.isArray(node))
|
|
|
|
return node.map(innerText).join("");
|
|
|
|
if ((0, domhandler_1.hasChildren)(node) && (node.type === domelementtype_1.ElementType.Tag || (0, domhandler_1.isCDATA)(node))) {
|
|
|
|
return innerText(node.children);
|
|
|
|
}
|
|
|
|
if ((0, domhandler_1.isText)(node))
|
|
|
|
return node.data;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
exports.innerText = innerText;
|
2022-09-03 17:12:22 +10:00
|
|
|
//# sourceMappingURL=stringify.js.map
|