stringify.js
1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getText = exports.getInnerHTML = exports.getOuterHTML = void 0;
var domhandler_1 = require("domhandler");
var dom_serializer_1 = __importDefault(require("dom-serializer"));
/**
* @param node Node to get the outer HTML of.
* @param options Options for serialization.
* @deprecated Use the `dom-serializer` module directly.
* @returns `node`'s outer HTML.
*/
function getOuterHTML(node, options) {
return dom_serializer_1.default(node, options);
}
exports.getOuterHTML = getOuterHTML;
/**
* @param node Node to get the inner HTML of.
* @param options Options for serialization.
* @deprecated Use the `dom-serializer` module directly.
* @returns `node`'s inner HTML.
*/
function getInnerHTML(node, options) {
return domhandler_1.hasChildren(node)
? node.children.map(function (node) { return getOuterHTML(node, options); }).join("")
: "";
}
exports.getInnerHTML = getInnerHTML;
/**
* Get a node's inner text.
*
* @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 (domhandler_1.isTag(node))
return node.name === "br" ? "\n" : getText(node.children);
if (domhandler_1.isCDATA(node))
return getText(node.children);
if (domhandler_1.isText(node))
return node.data;
return "";
}
exports.getText = getText;