1
1
import parse from './parse' ;
2
- import {
3
- CheerioOptions ,
4
- InternalOptions ,
5
- default as defaultOptions ,
6
- flatten as flattenOptions ,
7
- } from './options' ;
2
+ import { InternalOptions , default as defaultOptions } from './options' ;
8
3
import { isHtml , isCheerio } from './utils' ;
9
- import type { Node , Document , Element } from 'domhandler' ;
10
- import * as Static from './static' ;
11
- import type { load } from './load' ;
12
- import { SelectorType , BasicAcceptedElems } from './types' ;
4
+ import type { Node , Document } from 'domhandler' ;
5
+ import { BasicAcceptedElems } from './types' ;
13
6
14
7
import * as Attributes from './api/attributes' ;
15
8
import * as Traversing from './api/traversing' ;
@@ -23,16 +16,11 @@ type ManipulationType = typeof Manipulation;
23
16
type CssType = typeof Css ;
24
17
type FormsType = typeof Forms ;
25
18
26
- /*
27
- * The API
28
- */
29
- const api = [ Attributes , Traversing , Manipulation , Css , Forms ] ;
30
-
31
19
export class Cheerio < T > implements ArrayLike < T > {
32
- length ! : number ;
20
+ length = 0 ;
33
21
[ index : number ] : T ;
34
22
35
- options ! : InternalOptions ;
23
+ options : InternalOptions ;
36
24
/**
37
25
* The root of the document. Can be overwritten by using the `root` argument
38
26
* of the constructor.
@@ -42,37 +30,6 @@ export class Cheerio<T> implements ArrayLike<T> {
42
30
_root : Cheerio < Document > | undefined ;
43
31
/** @function */
44
32
find ! : typeof Traversing . find ;
45
- /**
46
- * The root the document was originally loaded with. Same as the static
47
- * `_root` property.
48
- *
49
- * @private
50
- */
51
- _originalRoot : Document | undefined ;
52
-
53
- /**
54
- * The root the document was originally loaded with. Set in `.load`.
55
- *
56
- * @private
57
- */
58
- static _root : Document | undefined ;
59
- /**
60
- * The options the document was originally loaded with. Set in `.load`.
61
- *
62
- * @private
63
- */
64
- static _options : InternalOptions | undefined ;
65
- public static html = Static . html ;
66
- public static xml = Static . xml ;
67
- public static text = Static . text ;
68
- public static parseHTML = Static . parseHTML ;
69
- public static root = Static . root ;
70
- public static contains = Static . contains ;
71
- public static merge = Static . merge ;
72
- public static load : typeof load ;
73
-
74
- /** Mimic jQuery's prototype alias for plugin authors. */
75
- public static fn = Cheerio . prototype ;
76
33
77
34
/**
78
35
* Instance of cheerio. Methods are specified in the modules. Usage of this
@@ -87,27 +44,24 @@ export class Cheerio<T> implements ArrayLike<T> {
87
44
constructor (
88
45
selector ?: T extends Node ? BasicAcceptedElems < T > : Cheerio < T > | T [ ] ,
89
46
context ?: BasicAcceptedElems < Node > | null ,
90
- root ?: BasicAcceptedElems < Document > ,
91
- options ?: CheerioOptions
47
+ root ?: BasicAcceptedElems < Document > | null ,
48
+ options : InternalOptions = defaultOptions
92
49
) {
93
- if ( ! ( this instanceof Cheerio ) ) {
94
- return new Cheerio ( selector , context , root , options ) ;
95
- }
96
-
97
- this . length = 0 ;
98
-
99
- this . options = {
100
- ...defaultOptions ,
101
- ...this . options ,
102
- ...flattenOptions ( options ) ,
103
- } ;
50
+ this . options = options ;
104
51
105
52
// $(), $(null), $(undefined), $(false)
106
53
if ( ! selector ) return this ;
107
54
108
55
if ( root ) {
109
56
if ( typeof root === 'string' ) root = parse ( root , this . options , false ) ;
110
- this . _root = ( Cheerio as any ) . call ( this , root ) ;
57
+ this . _root = new ( this . constructor as typeof Cheerio ) (
58
+ root ,
59
+ null ,
60
+ null ,
61
+ this . options
62
+ ) ;
63
+ // Add a cyclic reference, so that calling methods on `_root` never fails.
64
+ this . _root . _root = this . _root ;
111
65
}
112
66
113
67
// $($)
@@ -142,14 +96,14 @@ export class Cheerio<T> implements ArrayLike<T> {
142
96
: typeof context === 'string'
143
97
? isHtml ( context )
144
98
? // $('li', '<ul>...</ul>')
145
- new Cheerio ( parse ( context , this . options , false ) )
99
+ this . _make ( parse ( context , this . options , false ) )
146
100
: // $('li', 'ul')
147
101
( ( search = `${ context } ${ search } ` ) , this . _root )
148
102
: isCheerio ( context )
149
103
? // $('li', $)
150
104
context
151
105
: // $('li', node), $('li', [nodes])
152
- new Cheerio ( context ) ;
106
+ this . _make ( context ) ;
153
107
154
108
// If we still don't have a context, return
155
109
if ( ! searchContext ) return this ;
@@ -172,47 +126,30 @@ export class Cheerio<T> implements ArrayLike<T> {
172
126
*/
173
127
_make < T > (
174
128
dom : Cheerio < T > | T [ ] | T | string ,
175
- context ?: BasicAcceptedElems < Node > | null ,
176
- root : BasicAcceptedElems < Document > | undefined = this . _root
129
+ context ?: BasicAcceptedElems < Node >
177
130
) : Cheerio < T > {
178
131
const cheerio = new ( this . constructor as any ) (
179
132
dom ,
180
133
context ,
181
- root ,
134
+ this . _root ,
182
135
this . options
183
136
) ;
184
137
cheerio . prevObject = this ;
185
138
186
139
return cheerio ;
187
140
}
188
-
189
- /**
190
- * Retrieve all the DOM elements contained in the jQuery set as an array.
191
- *
192
- * @example
193
- *
194
- * ```js
195
- * $('li').toArray();
196
- * //=> [ {...}, {...}, {...} ]
197
- * ```
198
- *
199
- * @returns The contained items.
200
- */
201
- toArray ( ) : T [ ] {
202
- return this . get ( ) ;
203
- }
204
141
}
205
142
206
143
export interface Cheerio < T >
207
144
extends AttributesType ,
208
145
TraversingType ,
209
146
ManipulationType ,
210
147
CssType ,
211
- FormsType {
148
+ FormsType ,
149
+ Iterable < T > {
212
150
cheerio : '[cheerio object]' ;
213
151
214
152
splice : typeof Array . prototype . slice ;
215
- [ Symbol . iterator ] ( ) : Iterator < T > ;
216
153
}
217
154
218
155
/** Set a signature of the object. */
@@ -227,7 +164,14 @@ Cheerio.prototype.splice = Array.prototype.splice;
227
164
Cheerio . prototype [ Symbol . iterator ] = Array . prototype [ Symbol . iterator ] ;
228
165
229
166
// Plug in the API
230
- api . forEach ( ( mod ) => Object . assign ( Cheerio . prototype , mod ) ) ;
167
+ Object . assign (
168
+ Cheerio . prototype ,
169
+ Attributes ,
170
+ Traversing ,
171
+ Manipulation ,
172
+ Css ,
173
+ Forms
174
+ ) ;
231
175
232
176
function isNode ( obj : any ) : obj is Node {
233
177
return (
@@ -237,21 +181,3 @@ function isNode(obj: any): obj is Node {
237
181
obj . type === 'comment'
238
182
) ;
239
183
}
240
-
241
- type CheerioClassType = typeof Cheerio ;
242
-
243
- /**
244
- * Wrapper around the `Cheerio` class, making it possible to create a new
245
- * instance without using `new`.
246
- */
247
- export interface CheerioAPI extends CheerioClassType {
248
- < T extends Node , S extends string > (
249
- selector ?: S | BasicAcceptedElems < T > ,
250
- context ?: BasicAcceptedElems < Node > | null ,
251
- root ?: BasicAcceptedElems < Document > ,
252
- options ?: CheerioOptions
253
- ) : Cheerio < S extends SelectorType ? Element : T > ;
254
- }
255
-
256
- // Make it possible to call Cheerio without using `new`.
257
- export default ( Cheerio as unknown ) as CheerioAPI ;
0 commit comments