From a9d6a43847666c3651a9044b9c1b1788426a78f9 Mon Sep 17 00:00:00 2001 From: 5saviahv <49443574+5saviahv@users.noreply.github.com> Date: Mon, 25 Jan 2021 09:41:39 +0200 Subject: [PATCH] fix(statics): Remove module caching dependency (#1691) Moves `.load()` function to its own file, then loads `load.js` file after the Cheerio class is created. Fixes #1311 Fixes #1689 --- index.js | 9 +++---- lib/load.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ lib/static.js | 58 --------------------------------------------- 3 files changed, 70 insertions(+), 62 deletions(-) create mode 100644 lib/load.js diff --git a/index.js b/index.js index f5a6c56280..33882be68a 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,16 @@ 'use strict'; /** * @module cheerio - * @borrows static.load as load + * @borrows load.load as load * @borrows static.html as html * @borrows static.text as text * @borrows static.xml as xml */ -var staticMethods = require('./lib/static'); - exports = module.exports = require('./lib/cheerio'); +var staticMethods = require('./lib/static'); +var loadMethod = require('./lib/load'); + /** * An identifier describing the version of Cheerio which has been executed. * @@ -17,7 +18,7 @@ exports = module.exports = require('./lib/cheerio'); */ exports.version = require('./package.json').version; -exports.load = staticMethods.load; +exports.load = loadMethod.load; exports.html = staticMethods.html; exports.text = staticMethods.text; exports.xml = staticMethods.xml; diff --git a/lib/load.js b/lib/load.js new file mode 100644 index 0000000000..7a2a732deb --- /dev/null +++ b/lib/load.js @@ -0,0 +1,65 @@ +'use strict'; +/** + * @module cheerio/load + * @ignore + */ +var defaultOptions = require('./options').default; +var flattenOptions = require('./options').flatten; +var staticMethods = require('./static'); +var Cheerio = require('./cheerio'); +var parse = require('./parse'); + +/** + * Create a querying function, bound to a document created from the provided + * markup. Note that similar to web browser contexts, this operation may + * introduce ``, `
`, and `` elements; set `isDocument` to + * `false` to switch to fragment mode and disable this. + * + * See the README section titled "Loading" for additional usage information. + * + * @param {string} content - Markup to be loaded. + * @param {object} [options] - Options for the created instance. + * @param {boolean} [isDocument] - Allows parser to be switched to fragment mode. + * @returns {Cheerio} - The loaded document. + */ +exports.load = function (content, options, isDocument) { + if (content === null || content === undefined) { + throw new Error('cheerio.load() expects a string'); + } + + options = Object.assign({}, defaultOptions, flattenOptions(options)); + + if (typeof isDocument === 'undefined') isDocument = true; + + var root = parse(content, options, isDocument); + + function initialize(selector, context, r, opts) { + if (!(this instanceof initialize)) { + return new initialize(selector, context, r, opts); + } + opts = Object.assign({}, options, opts); + return Cheerio.call(this, selector, context, r || root, opts); + } + + // Ensure that selections created by the "loaded" `initialize` function are + // true Cheerio instances. + initialize.prototype = Object.create(Cheerio.prototype); + initialize.prototype.constructor = initialize; + + // Mimic jQuery's prototype alias for plugin authors. + initialize.fn = initialize.prototype; + + // Keep a reference to the top-level scope so we can chain methods that implicitly + // resolve selectors; e.g. $("").(".bar"), which otherwise loses ._root + initialize.prototype._originalRoot = root; + + // Add in the static methods + Object.assign(initialize, staticMethods, exports); + + // Add in the root + initialize._root = root; + // store options + initialize._options = options; + + return initialize; +}; diff --git a/lib/static.js b/lib/static.js index c496efeb6d..fb3fe10726 100644 --- a/lib/static.js +++ b/lib/static.js @@ -8,64 +8,6 @@ var flattenOptions = require('./options').flatten; var select = require('cheerio-select').select; var renderWithParse5 = require('./parsers/parse5').render; var renderWithHtmlparser2 = require('./parsers/htmlparser2').render; -var parse = require('./parse'); - -/** - * Create a querying function, bound to a document created from the provided - * markup. Note that similar to web browser contexts, this operation may - * introduce ``, ``, and `` elements; set `isDocument` to - * `false` to switch to fragment mode and disable this. - * - * See the README section titled "Loading" for additional usage information. - * - * @param {string} content - Markup to be loaded. - * @param {object} [options] - Options for the created instance. - * @param {boolean} [isDocument] - Allows parser to be switched to fragment mode. - * @returns {Cheerio} - The loaded document. - */ -exports.load = function (content, options, isDocument) { - if (content === null || content === undefined) { - throw new Error('cheerio.load() expects a string'); - } - - var Cheerio = require('./cheerio'); - - options = Object.assign({}, defaultOptions, flattenOptions(options)); - - if (typeof isDocument === 'undefined') isDocument = true; - - var root = parse(content, options, isDocument); - - function initialize(selector, context, r, opts) { - if (!(this instanceof initialize)) { - return new initialize(selector, context, r, opts); - } - opts = Object.assign({}, options, opts); - return Cheerio.call(this, selector, context, r || root, opts); - } - - // Ensure that selections created by the "loaded" `initialize` function are - // true Cheerio instances. - initialize.prototype = Object.create(Cheerio.prototype); - initialize.prototype.constructor = initialize; - - // Mimic jQuery's prototype alias for plugin authors. - initialize.fn = initialize.prototype; - - // Keep a reference to the top-level scope so we can chain methods that implicitly - // resolve selectors; e.g. $("").(".bar"), which otherwise loses ._root - initialize.prototype._originalRoot = root; - - // Add in the static methods - Object.assign(initialize, exports); - - // Add in the root - initialize._root = root; - // store options - initialize._options = options; - - return initialize; -}; /** * Helper function to render a DOM.