| "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 () { |
| var ownKeys = function(o) { |
| ownKeys = Object.getOwnPropertyNames || function (o) { |
| var ar = []; |
| for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; |
| return ar; |
| }; |
| return ownKeys(o); |
| }; |
| return function (mod) { |
| if (mod && mod.__esModule) return mod; |
| var result = {}; |
| if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); |
| __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"); |
| const htmlparser2_1 = require("htmlparser2"); |
| 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 === htmlparser2_1.ElementType.Root || |
| |
| obj.type === htmlparser2_1.ElementType.Text || |
| |
| obj.type === htmlparser2_1.ElementType.Comment); |
| } |
| |