2025-03-29 15:51:03 -04:00

149 lines
6.3 KiB
JavaScript

"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) {
/**
* Create a querying function, bound to a document created from the provided
* markup.
*
* Note that similar to web browser contexts, this operation may introduce
* `<html>`, `<head>`, and `<body>` elements; set `isDocument` to `false` to
* switch to fragment mode and disable this.
*
* @param content - Markup to be loaded.
* @param options - Options for the created instance.
* @param isDocument - Allows parser to be switched to fragment mode.
* @returns The loaded document.
* @see {@link https://cheerio.js.org#loading} for additional usage information.
*/
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);
/**
* Create an extended class here, so that extensions only live on one
* instance.
*/
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);
// Add a cyclic reference, so that calling methods on `_root` never fails.
rootInstance._root = rootInstance;
// $(), $(null), $(undefined), $(false)
if (!selector) {
return new LoadedCheerio(undefined, rootInstance, options);
}
const elements = typeof selector === 'string' && (0, utils_js_1.isHtml)(selector)
? // $(<html>)
parse(selector, options, false, null).children
: isNode(selector)
? // $(dom)
[selector]
: Array.isArray(selector)
? // $([dom])
selector
: undefined;
const instance = new LoadedCheerio(elements, rootInstance, options);
if (elements) {
return instance;
}
if (typeof selector !== 'string') {
throw new TypeError('Unexpected type of selector');
}
// We know that our selector is a string now.
let search = selector;
const searchContext = context
? // If we don't have a context, maybe we have a root, from loading
typeof context === 'string'
? (0, utils_js_1.isHtml)(context)
? // $('li', '<ul>...</ul>')
new LoadedCheerio([parse(context, options, false, null)], rootInstance, options)
: // $('li', 'ul')
((search = `${context} ${search}`), rootInstance)
: (0, utils_js_1.isCheerio)(context)
? // $('li', $)
context
: // $('li', node), $('li', [nodes])
new LoadedCheerio(Array.isArray(context) ? context : [context], rootInstance, options)
: rootInstance;
// If we still don't have a context, return
if (!searchContext)
return instance;
/*
* #id, .class, tag
*/
return searchContext.find(search);
}
// Add in static methods & properties
Object.assign(initialize, staticMethods, {
load,
// `_root` and `_options` are used in static methods.
_root: initialRoot,
_options: internalOpts,
// Add `fn` for plugins
fn: LoadedCheerio.prototype,
// Add the prototype here to maintain `instanceof` behavior.
prototype: LoadedCheerio.prototype,
});
return initialize;
};
}
function isNode(obj) {
return (!!obj.name ||
obj.type === 'root' ||
obj.type === 'text' ||
obj.type === 'comment');
}
//# sourceMappingURL=load.js.map