-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
cheerio.js
153 lines (133 loc) · 3.78 KB
/
cheerio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
/*
Module dependencies
*/
var parse = require('./parse');
var defaultOptions = require('./options').default;
var flattenOptions = require('./options').flatten;
var isHtml = require('./utils').isHtml;
/*
* The API
*/
var api = [
require('./api/attributes'),
require('./api/traversing'),
require('./api/manipulation'),
require('./api/css'),
require('./api/forms'),
];
/**
* Instance of cheerio. Methods are specified in the modules. Usage of this
* constructor is not recommended. Please use $.load instead.
*
* @class
* @param {string | Cheerio | Node | Node[]} selector - The new selection.
* @param {string | Cheerio | Node | Node[]} [context] - Context of the selection.
* @param {string | Cheerio | Node | Node[]} [root] - Sets the root node.
* @param {object} [options] - Options for the instance.
* @hideconstructor
* @mixes module:cheerio/attributes
* @mixes module:cheerio/css
* @mixes module:cheerio/forms
* @mixes module:cheerio/manipulation
* @mixes module:cheerio/traversing
*/
var Cheerio = (module.exports = function (selector, context, root, options) {
if (!(this instanceof Cheerio)) {
return new Cheerio(selector, context, root, options);
}
this.length = 0;
this.options = Object.assign(
{},
defaultOptions,
this.options,
flattenOptions(options)
);
// $(), $(null), $(undefined), $(false)
if (!selector) return this;
if (root) {
if (typeof root === 'string') root = parse(root, this.options, false);
this._root = Cheerio.call(this, root);
}
// $(<html>)
if (typeof selector === 'string' && isHtml(selector)) {
selector = parse(selector, this.options, false).children;
}
// $($)
if (selector.cheerio) return selector;
// $(dom)
if (isNode(selector)) selector = [selector];
// $([dom])
if (Array.isArray(selector)) {
selector.forEach(function (elem, idx) {
this[idx] = elem;
}, this);
this.length = selector.length;
return this;
}
// If we don't have a context, maybe we have a root, from loading
if (!context) {
context = this._root;
} else if (typeof context === 'string') {
if (isHtml(context)) {
// $('li', '<ul>...</ul>')
context = parse(context, this.options, false);
context = Cheerio.call(this, context);
} else {
// $('li', 'ul')
selector = context + ' ' + selector;
context = this._root;
}
} else if (!context.cheerio) {
// $('li', node), $('li', [nodes])
context = Cheerio.call(this, context);
}
// If we still don't have a context, return
if (!context) return this;
// #id, .class, tag
return context.find(selector);
});
/** Set a signature of the object. */
Cheerio.prototype.cheerio = '[cheerio object]';
/*
* Make cheerio an array-like object
*/
Cheerio.prototype.splice = Array.prototype.splice;
/**
* Make a cheerio object.
*
* @private
* @param {Node[]} dom - The contents of the new object.
* @param {Node[]} [context] - The context of the new object.
* @returns {Cheerio} The new cheerio object.
*/
Cheerio.prototype._make = function (dom, context) {
var cheerio = new this.constructor(dom, context, this._root, this.options);
cheerio.prevObject = this;
return cheerio;
};
/**
* Retrieve all the DOM elements contained in the jQuery set as an array.
*
* @example
* $('li').toArray(); //=> [ {...}, {...}, {...} ]
*
* @returns {Node[]} The contained items.
*/
Cheerio.prototype.toArray = function () {
return this.get();
};
// Support for (const element of $(...)) iteration:
Cheerio.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
// Plug in the API
api.forEach(function (mod) {
Object.assign(Cheerio.prototype, mod);
});
function isNode(obj) {
return (
obj.name ||
obj.type === 'root' ||
obj.type === 'text' ||
obj.type === 'comment'
);
}