123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- import { textContent } from 'domutils';
- import { flattenOptions as flattenOptions, } from './options.js';
- function render(that, dom, options) {
- if (!that)
- return '';
- return that(dom !== null && dom !== void 0 ? dom : that._root.children, null, undefined, options).toString();
- }
- function isOptions(dom, options) {
- return (!options &&
- typeof dom === 'object' &&
- dom != null &&
- !('length' in dom) &&
- !('type' in dom));
- }
- export function html(dom, options) {
-
- const toRender = isOptions(dom) ? ((options = dom), undefined) : dom;
-
- const opts = {
- ...this === null || this === void 0 ? void 0 : this._options,
- ...flattenOptions(options),
- };
- return render(this, toRender, opts);
- }
- export function xml(dom) {
- const options = { ...this._options, xmlMode: true };
- return render(this, dom, options);
- }
- export function text(elements) {
- const elems = elements !== null && elements !== void 0 ? elements : (this ? this.root() : []);
- let ret = '';
- for (let i = 0; i < elems.length; i++) {
- ret += textContent(elems[i]);
- }
- return ret;
- }
- export function parseHTML(data, context, keepScripts = typeof context === 'boolean' ? context : false) {
- if (!data || typeof data !== 'string') {
- return null;
- }
- if (typeof context === 'boolean') {
- keepScripts = context;
- }
- const parsed = this.load(data, this._options, false);
- if (!keepScripts) {
- parsed('script').remove();
- }
-
- return [...parsed.root()[0].children];
- }
- export function root() {
- return this(this._root);
- }
- export function contains(container, contained) {
-
- if (contained === container) {
- return false;
- }
-
- let next = contained;
- while (next && next !== next.parent) {
- next = next.parent;
- if (next === container) {
- return true;
- }
- }
- return false;
- }
- export function extract(map) {
- return this.root().extract(map);
- }
- export function merge(arr1, arr2) {
- if (!isArrayLike(arr1) || !isArrayLike(arr2)) {
- return;
- }
- let newLength = arr1.length;
- const len = +arr2.length;
- for (let i = 0; i < len; i++) {
- arr1[newLength++] = arr2[i];
- }
- arr1.length = newLength;
- return arr1;
- }
- function isArrayLike(item) {
- if (Array.isArray(item)) {
- return true;
- }
- if (typeof item !== 'object' ||
- item === null ||
- !('length' in item) ||
- typeof item.length !== 'number' ||
- item.length < 0) {
- return false;
- }
- for (let i = 0; i < item.length; i++) {
- if (!(i in item)) {
- return false;
- }
- }
- return true;
- }
|