Skip to content

Commit a9d6a43

Browse files
authored
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
1 parent fcc0141 commit a9d6a43

File tree

3 files changed

+70
-62
lines changed

3 files changed

+70
-62
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
'use strict';
22
/**
33
* @module cheerio
4-
* @borrows static.load as load
4+
* @borrows load.load as load
55
* @borrows static.html as html
66
* @borrows static.text as text
77
* @borrows static.xml as xml
88
*/
9-
var staticMethods = require('./lib/static');
10-
119
exports = module.exports = require('./lib/cheerio');
1210

11+
var staticMethods = require('./lib/static');
12+
var loadMethod = require('./lib/load');
13+
1314
/**
1415
* An identifier describing the version of Cheerio which has been executed.
1516
*
1617
* @type {string}
1718
*/
1819
exports.version = require('./package.json').version;
1920

20-
exports.load = staticMethods.load;
21+
exports.load = loadMethod.load;
2122
exports.html = staticMethods.html;
2223
exports.text = staticMethods.text;
2324
exports.xml = staticMethods.xml;

lib/load.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
/**
3+
* @module cheerio/load
4+
* @ignore
5+
*/
6+
var defaultOptions = require('./options').default;
7+
var flattenOptions = require('./options').flatten;
8+
var staticMethods = require('./static');
9+
var Cheerio = require('./cheerio');
10+
var parse = require('./parse');
11+
12+
/**
13+
* Create a querying function, bound to a document created from the provided
14+
* markup. Note that similar to web browser contexts, this operation may
15+
* introduce `<html>`, `<head>`, and `<body>` elements; set `isDocument` to
16+
* `false` to switch to fragment mode and disable this.
17+
*
18+
* See the README section titled "Loading" for additional usage information.
19+
*
20+
* @param {string} content - Markup to be loaded.
21+
* @param {object} [options] - Options for the created instance.
22+
* @param {boolean} [isDocument] - Allows parser to be switched to fragment mode.
23+
* @returns {Cheerio} - The loaded document.
24+
*/
25+
exports.load = function (content, options, isDocument) {
26+
if (content === null || content === undefined) {
27+
throw new Error('cheerio.load() expects a string');
28+
}
29+
30+
options = Object.assign({}, defaultOptions, flattenOptions(options));
31+
32+
if (typeof isDocument === 'undefined') isDocument = true;
33+
34+
var root = parse(content, options, isDocument);
35+
36+
function initialize(selector, context, r, opts) {
37+
if (!(this instanceof initialize)) {
38+
return new initialize(selector, context, r, opts);
39+
}
40+
opts = Object.assign({}, options, opts);
41+
return Cheerio.call(this, selector, context, r || root, opts);
42+
}
43+
44+
// Ensure that selections created by the "loaded" `initialize` function are
45+
// true Cheerio instances.
46+
initialize.prototype = Object.create(Cheerio.prototype);
47+
initialize.prototype.constructor = initialize;
48+
49+
// Mimic jQuery's prototype alias for plugin authors.
50+
initialize.fn = initialize.prototype;
51+
52+
// Keep a reference to the top-level scope so we can chain methods that implicitly
53+
// resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root
54+
initialize.prototype._originalRoot = root;
55+
56+
// Add in the static methods
57+
Object.assign(initialize, staticMethods, exports);
58+
59+
// Add in the root
60+
initialize._root = root;
61+
// store options
62+
initialize._options = options;
63+
64+
return initialize;
65+
};

lib/static.js

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,6 @@ var flattenOptions = require('./options').flatten;
88
var select = require('cheerio-select').select;
99
var renderWithParse5 = require('./parsers/parse5').render;
1010
var renderWithHtmlparser2 = require('./parsers/htmlparser2').render;
11-
var parse = require('./parse');
12-
13-
/**
14-
* Create a querying function, bound to a document created from the provided
15-
* markup. Note that similar to web browser contexts, this operation may
16-
* introduce `<html>`, `<head>`, and `<body>` elements; set `isDocument` to
17-
* `false` to switch to fragment mode and disable this.
18-
*
19-
* See the README section titled "Loading" for additional usage information.
20-
*
21-
* @param {string} content - Markup to be loaded.
22-
* @param {object} [options] - Options for the created instance.
23-
* @param {boolean} [isDocument] - Allows parser to be switched to fragment mode.
24-
* @returns {Cheerio} - The loaded document.
25-
*/
26-
exports.load = function (content, options, isDocument) {
27-
if (content === null || content === undefined) {
28-
throw new Error('cheerio.load() expects a string');
29-
}
30-
31-
var Cheerio = require('./cheerio');
32-
33-
options = Object.assign({}, defaultOptions, flattenOptions(options));
34-
35-
if (typeof isDocument === 'undefined') isDocument = true;
36-
37-
var root = parse(content, options, isDocument);
38-
39-
function initialize(selector, context, r, opts) {
40-
if (!(this instanceof initialize)) {
41-
return new initialize(selector, context, r, opts);
42-
}
43-
opts = Object.assign({}, options, opts);
44-
return Cheerio.call(this, selector, context, r || root, opts);
45-
}
46-
47-
// Ensure that selections created by the "loaded" `initialize` function are
48-
// true Cheerio instances.
49-
initialize.prototype = Object.create(Cheerio.prototype);
50-
initialize.prototype.constructor = initialize;
51-
52-
// Mimic jQuery's prototype alias for plugin authors.
53-
initialize.fn = initialize.prototype;
54-
55-
// Keep a reference to the top-level scope so we can chain methods that implicitly
56-
// resolve selectors; e.g. $("<span>").(".bar"), which otherwise loses ._root
57-
initialize.prototype._originalRoot = root;
58-
59-
// Add in the static methods
60-
Object.assign(initialize, exports);
61-
62-
// Add in the root
63-
initialize._root = root;
64-
// store options
65-
initialize._options = options;
66-
67-
return initialize;
68-
};
6911

7012
/**
7113
* Helper function to render a DOM.

0 commit comments

Comments
 (0)