123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- "use strict";
- 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.getLoad = getLoad;
- const options_js_1 = require("./options.js");
- const staticMethods = __importStar(require("./static.js"));
- const cheerio_js_1 = require("./cheerio.js");
- const utils_js_1 = require("./utils.js");
- function getLoad(parse, render) {
-
- return function load(content, options, isDocument = true) {
- if (content == null) {
- throw new Error('cheerio.load() expects a string');
- }
- const internalOpts = (0, options_js_1.flattenOptions)(options);
- const initialRoot = parse(content, internalOpts, isDocument, null);
-
- class LoadedCheerio extends cheerio_js_1.Cheerio {
- _make(selector, context) {
- const cheerio = initialize(selector, context);
- cheerio.prevObject = this;
- return cheerio;
- }
- _parse(content, options, isDocument, context) {
- return parse(content, options, isDocument, context);
- }
- _render(dom) {
- return render(dom, this.options);
- }
- }
- function initialize(selector, context, root = initialRoot, opts) {
-
- if (selector && (0, utils_js_1.isCheerio)(selector))
- return selector;
- const options = (0, options_js_1.flattenOptions)(opts, internalOpts);
- const r = typeof root === 'string'
- ? [parse(root, options, false, null)]
- : 'length' in root
- ? root
- : [root];
- const rootInstance = (0, utils_js_1.isCheerio)(r)
- ? r
- : new LoadedCheerio(r, null, options);
-
- rootInstance._root = rootInstance;
-
- if (!selector) {
- return new LoadedCheerio(undefined, rootInstance, options);
- }
- const elements = typeof selector === 'string' && (0, utils_js_1.isHtml)(selector)
- ?
- parse(selector, options, false, null).children
- : isNode(selector)
- ?
- [selector]
- : Array.isArray(selector)
- ?
- selector
- : undefined;
- const instance = new LoadedCheerio(elements, rootInstance, options);
- if (elements) {
- return instance;
- }
- if (typeof selector !== 'string') {
- throw new TypeError('Unexpected type of selector');
- }
-
- let search = selector;
- const searchContext = context
- ?
- typeof context === 'string'
- ? (0, utils_js_1.isHtml)(context)
- ?
- new LoadedCheerio([parse(context, options, false, null)], rootInstance, options)
- :
- ((search = `${context} ${search}`), rootInstance)
- : (0, utils_js_1.isCheerio)(context)
- ?
- context
- :
- new LoadedCheerio(Array.isArray(context) ? context : [context], rootInstance, options)
- : rootInstance;
-
- if (!searchContext)
- return instance;
-
- return searchContext.find(search);
- }
-
- Object.assign(initialize, staticMethods, {
- load,
-
- _root: initialRoot,
- _options: internalOpts,
-
- fn: LoadedCheerio.prototype,
-
- prototype: LoadedCheerio.prototype,
- });
- return initialize;
- };
- }
- function isNode(obj) {
- return (!!obj.name ||
- obj.type === 'root' ||
- obj.type === 'text' ||
- obj.type === 'comment');
- }
|