226 lines
7.2 KiB
JavaScript
226 lines
7.2 KiB
JavaScript
"use strict";
|
|
var __assign = (this && this.__assign) || function () {
|
|
__assign = Object.assign || function(t) {
|
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
s = arguments[i];
|
|
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
t[p] = s[p];
|
|
}
|
|
return t;
|
|
};
|
|
return __assign.apply(this, arguments);
|
|
};
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.merge = exports.contains = exports.root = exports.parseHTML = exports.text = exports.xml = exports.html = void 0;
|
|
var domutils_1 = require("domutils");
|
|
var options_js_1 = __importStar(require("./options.js"));
|
|
/**
|
|
* Helper function to render a DOM.
|
|
*
|
|
* @param that - Cheerio instance to render.
|
|
* @param dom - The DOM to render. Defaults to `that`'s root.
|
|
* @param options - Options for rendering.
|
|
* @returns The rendered document.
|
|
*/
|
|
function render(that, dom, options) {
|
|
if (!that)
|
|
return '';
|
|
return that(dom !== null && dom !== void 0 ? dom : that._root.children, null, undefined, options).toString();
|
|
}
|
|
/**
|
|
* Checks if a passed object is an options object.
|
|
*
|
|
* @param dom - Object to check if it is an options object.
|
|
* @returns Whether the object is an options object.
|
|
*/
|
|
function isOptions(dom, options) {
|
|
return (!options &&
|
|
typeof dom === 'object' &&
|
|
dom != null &&
|
|
!('length' in dom) &&
|
|
!('type' in dom));
|
|
}
|
|
function html(dom, options) {
|
|
/*
|
|
* Be flexible about parameters, sometimes we call html(),
|
|
* with options as only parameter
|
|
* check dom argument for dom element specific properties
|
|
* assume there is no 'length' or 'type' properties in the options object
|
|
*/
|
|
var toRender = isOptions(dom) ? ((options = dom), undefined) : dom;
|
|
/*
|
|
* Sometimes `$.html()` is used without preloading html,
|
|
* so fallback non-existing options to the default ones.
|
|
*/
|
|
var opts = __assign(__assign(__assign({}, options_js_1.default), this === null || this === void 0 ? void 0 : this._options), (0, options_js_1.flatten)(options !== null && options !== void 0 ? options : {}));
|
|
return render(this, toRender, opts);
|
|
}
|
|
exports.html = html;
|
|
/**
|
|
* Render the document as XML.
|
|
*
|
|
* @param dom - Element to render.
|
|
* @returns THe rendered document.
|
|
*/
|
|
function xml(dom) {
|
|
var options = __assign(__assign({}, this._options), { xmlMode: true });
|
|
return render(this, dom, options);
|
|
}
|
|
exports.xml = xml;
|
|
/**
|
|
* Render the document as text.
|
|
*
|
|
* This returns the `textContent` of the passed elements. The result will
|
|
* include the contents of `script` and `stype` elements. To avoid this, use
|
|
* `.prop('innerText')` instead.
|
|
*
|
|
* @param elements - Elements to render.
|
|
* @returns The rendered document.
|
|
*/
|
|
function text(elements) {
|
|
var elems = elements ? elements : this ? this.root() : [];
|
|
var ret = '';
|
|
for (var i = 0; i < elems.length; i++) {
|
|
ret += (0, domutils_1.textContent)(elems[i]);
|
|
}
|
|
return ret;
|
|
}
|
|
exports.text = text;
|
|
function parseHTML(data, context, keepScripts) {
|
|
if (keepScripts === void 0) { keepScripts = typeof context === 'boolean' ? context : false; }
|
|
if (!data || typeof data !== 'string') {
|
|
return null;
|
|
}
|
|
if (typeof context === 'boolean') {
|
|
keepScripts = context;
|
|
}
|
|
var parsed = this.load(data, options_js_1.default, false);
|
|
if (!keepScripts) {
|
|
parsed('script').remove();
|
|
}
|
|
/*
|
|
* The `children` array is used by Cheerio internally to group elements that
|
|
* share the same parents. When nodes created through `parseHTML` are
|
|
* inserted into previously-existing DOM structures, they will be removed
|
|
* from the `children` array. The results of `parseHTML` should remain
|
|
* constant across these operations, so a shallow copy should be returned.
|
|
*/
|
|
return parsed.root()[0].children.slice();
|
|
}
|
|
exports.parseHTML = parseHTML;
|
|
/**
|
|
* Sometimes you need to work with the top-level root element. To query it, you
|
|
* can use `$.root()`.
|
|
*
|
|
* @example
|
|
*
|
|
* ```js
|
|
* $.root().append('<ul id="vegetables"></ul>').html();
|
|
* //=> <ul id="fruits">...</ul><ul id="vegetables"></ul>
|
|
* ```
|
|
*
|
|
* @returns Cheerio instance wrapping the root node.
|
|
* @alias Cheerio.root
|
|
*/
|
|
function root() {
|
|
return this(this._root);
|
|
}
|
|
exports.root = root;
|
|
/**
|
|
* Checks to see if the `contained` DOM element is a descendant of the
|
|
* `container` DOM element.
|
|
*
|
|
* @param container - Potential parent node.
|
|
* @param contained - Potential child node.
|
|
* @returns Indicates if the nodes contain one another.
|
|
* @alias Cheerio.contains
|
|
* @see {@link https://api.jquery.com/jQuery.contains/}
|
|
*/
|
|
function contains(container, contained) {
|
|
// According to the jQuery API, an element does not "contain" itself
|
|
if (contained === container) {
|
|
return false;
|
|
}
|
|
/*
|
|
* Step up the descendants, stopping when the root element is reached
|
|
* (signaled by `.parent` returning a reference to the same object)
|
|
*/
|
|
var next = contained;
|
|
while (next && next !== next.parent) {
|
|
next = next.parent;
|
|
if (next === container) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
exports.contains = contains;
|
|
/**
|
|
* $.merge().
|
|
*
|
|
* @param arr1 - First array.
|
|
* @param arr2 - Second array.
|
|
* @returns `arr1`, with elements of `arr2` inserted.
|
|
* @alias Cheerio.merge
|
|
* @see {@link https://api.jquery.com/jQuery.merge/}
|
|
*/
|
|
function merge(arr1, arr2) {
|
|
if (!isArrayLike(arr1) || !isArrayLike(arr2)) {
|
|
return;
|
|
}
|
|
var newLength = arr1.length;
|
|
var len = +arr2.length;
|
|
for (var i = 0; i < len; i++) {
|
|
arr1[newLength++] = arr2[i];
|
|
}
|
|
arr1.length = newLength;
|
|
return arr1;
|
|
}
|
|
exports.merge = merge;
|
|
/**
|
|
* Checks if an object is array-like.
|
|
*
|
|
* @param item - Item to check.
|
|
* @returns Indicates if the item is array-like.
|
|
*/
|
|
function isArrayLike(item) {
|
|
if (Array.isArray(item)) {
|
|
return true;
|
|
}
|
|
if (typeof item !== 'object' ||
|
|
!Object.prototype.hasOwnProperty.call(item, 'length') ||
|
|
typeof item.length !== 'number' ||
|
|
item.length < 0) {
|
|
return false;
|
|
}
|
|
for (var i = 0; i < item.length; i++) {
|
|
if (!(i in item)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
//# sourceMappingURL=static.js.map
|