UNPKG

10.9 kBTypeScriptView Raw
1/// <reference types="node" />
2
3interface Document {}
4
5declare namespace cheerio {
6 type Element = TextElement | TagElement | CommentElement;
7
8 interface TextElement {
9 type: "text";
10 next: Element | null;
11 prev: Element | null;
12 parent: Element;
13 data?: string | undefined;
14 startIndex?: number | undefined;
15 endIndex?: number | undefined;
16 }
17
18 interface TagElement {
19 tagName: string;
20 type: "tag" | "script" | "style";
21 name: string;
22 attribs: { [attr: string]: string };
23 "x-attribsNamespace": { [attr: string]: string };
24 "x-prefixNamespace": { [attr: string]: string };
25 children: Element[];
26 childNodes: Element[] | null;
27 lastChild: Element | null;
28 firstChild: Element | null;
29 next: Element | null;
30 nextSibling: Element;
31 prev: Element | null;
32 previousSibling: Element;
33 parent: Element;
34 parentNode: Element;
35 nodeValue: string;
36 data?: string | undefined;
37 startIndex?: number | undefined;
38 endIndex?: number | undefined;
39 }
40
41 interface CommentElement {
42 type: "comment";
43 next: Element | null;
44 prev: Element | null;
45 parent: Element;
46 data?: string | undefined;
47 startIndex?: number | undefined;
48 endIndex?: number | undefined;
49 }
50
51 type AttrFunction = (el: Element, i: number, currentValue: string) => any;
52
53 interface Cheerio {
54 // Document References
55 // Cheerio https://github.com/cheeriojs/cheerio
56 // JQuery http://api.jquery.com
57
58 [Symbol.iterator](): IterableIterator<Element>;
59 [index: number]: Element;
60 cheerio: string;
61 length: number;
62
63 // Attributes
64
65 attr(): { [attr: string]: string };
66 attr(name: string): string | undefined;
67 attr(name: string, value: AttrFunction): Cheerio;
68 // `value` *can* be `any` here but:
69 // 1. That makes type-checking the function-type useless
70 // 2. It's converted to a string anyways
71 attr(name: string, value: string): Cheerio;
72 // The map's values *can* be `any` but they'll all be cast to strings
73 // regardless.
74 attr(map: { [key: string]: any }): Cheerio;
75
76 data(): any;
77 data(name: string): any;
78 data(name: string, value: any): any;
79
80 val(): string;
81 val(value: string): Cheerio;
82
83 removeAttr(name: string): Cheerio;
84
85 has(selector: string): Cheerio;
86 has(element: Element): Cheerio;
87
88 hasClass(className: string): boolean;
89 addClass(classNames: string): Cheerio;
90
91 removeClass(): Cheerio;
92 removeClass(className: string): Cheerio;
93 removeClass(func: (index: number, className: string) => string): Cheerio;
94
95 toggleClass(className: string): Cheerio;
96 toggleClass(className: string, toggleSwitch: boolean): Cheerio;
97 toggleClass(toggleSwitch?: boolean): Cheerio;
98 toggleClass(
99 func: (index: number, className: string, toggleSwitch: boolean) => string,
100 toggleSwitch?: boolean,
101 ): Cheerio;
102
103 is(selector: string): boolean;
104 is(element: Element): boolean;
105 is(element: Element[]): boolean;
106 is(selection: Cheerio): boolean;
107 is(func: (index: number, element: Element) => boolean): boolean;
108
109 // Form
110 serialize(): string;
111 serializeArray(): Array<{ name: string; value: string }>;
112
113 // Traversing
114
115 find(selector: string): Cheerio;
116 find(element: Cheerio): Cheerio;
117
118 parent(selector?: string): Cheerio;
119 parents(selector?: string): Cheerio;
120 parentsUntil(selector?: string, filter?: string): Cheerio;
121 parentsUntil(element: Element, filter?: string): Cheerio;
122 parentsUntil(element: Cheerio, filter?: string): Cheerio;
123
124 prop(name: string): any;
125 prop(name: string, value: any): Cheerio;
126
127 closest(): Cheerio;
128 closest(selector: string): Cheerio;
129
130 next(selector?: string): Cheerio;
131 nextAll(): Cheerio;
132 nextAll(selector: string): Cheerio;
133
134 nextUntil(selector?: string, filter?: string): Cheerio;
135 nextUntil(element: Element, filter?: string): Cheerio;
136 nextUntil(element: Cheerio, filter?: string): Cheerio;
137
138 prev(selector?: string): Cheerio;
139 prevAll(): Cheerio;
140 prevAll(selector: string): Cheerio;
141
142 prevUntil(selector?: string, filter?: string): Cheerio;
143 prevUntil(element: Element, filter?: string): Cheerio;
144 prevUntil(element: Cheerio, filter?: string): Cheerio;
145
146 slice(start: number, end?: number): Cheerio;
147
148 siblings(selector?: string): Cheerio;
149
150 children(selector?: string): Cheerio;
151
152 contents(): Cheerio;
153
154 each(func: (index: number, element: Element) => any): Cheerio;
155 map(func: (index: number, element: Element) => any): Cheerio;
156
157 filter(selector: string): Cheerio;
158 filter(selection: Cheerio): Cheerio;
159 filter(element: Element): Cheerio;
160 filter(elements: Element[]): Cheerio;
161 filter(func: (index: number, element: Element) => boolean): Cheerio;
162
163 not(selector: string): Cheerio;
164 not(selection: Cheerio): Cheerio;
165 not(element: Element): Cheerio;
166 not(func: (index: number, element: Element) => boolean): Cheerio;
167
168 first(): Cheerio;
169 last(): Cheerio;
170
171 eq(index: number): Cheerio;
172
173 get(): any[];
174 get(index: number): any;
175
176 index(): number;
177 index(selector: string): number;
178 index(selection: Cheerio): number;
179
180 end(): Cheerio;
181
182 add(selectorOrHtml: string): Cheerio;
183 add(selector: string, context: Document): Cheerio;
184 add(element: Element): Cheerio;
185 add(elements: Element[]): Cheerio;
186 add(selection: Cheerio): Cheerio;
187
188 addBack(): Cheerio;
189 addBack(filter: string): Cheerio;
190
191 // Manipulation
192 appendTo(target: Cheerio): Cheerio;
193 prependTo(target: Cheerio): Cheerio;
194
195 append(content: string, ...contents: any[]): Cheerio;
196 append(content: Document, ...contents: any[]): Cheerio;
197 append(content: Document[], ...contents: any[]): Cheerio;
198 append(content: Cheerio, ...contents: any[]): Cheerio;
199
200 prepend(content: string, ...contents: any[]): Cheerio;
201 prepend(content: Document, ...contents: any[]): Cheerio;
202 prepend(content: Document[], ...contents: any[]): Cheerio;
203 prepend(content: Cheerio, ...contents: any[]): Cheerio;
204
205 after(content: string, ...contents: any[]): Cheerio;
206 after(content: Document, ...contents: any[]): Cheerio;
207 after(content: Document[], ...contents: any[]): Cheerio;
208 after(content: Cheerio, ...contents: any[]): Cheerio;
209
210 insertAfter(content: string): Cheerio;
211 insertAfter(content: Document): Cheerio;
212 insertAfter(content: Cheerio): Cheerio;
213
214 before(content: string, ...contents: any[]): Cheerio;
215 before(content: Document, ...contents: any[]): Cheerio;
216 before(content: Document[], ...contents: any[]): Cheerio;
217 before(content: Cheerio, ...contents: any[]): Cheerio;
218
219 insertBefore(content: string): Cheerio;
220 insertBefore(content: Document): Cheerio;
221 insertBefore(content: Cheerio): Cheerio;
222
223 remove(selector?: string): Cheerio;
224
225 replaceWith(content: string): Cheerio;
226 replaceWith(content: Element): Cheerio;
227 replaceWith(content: Element[]): Cheerio;
228 replaceWith(content: Cheerio): Cheerio;
229 replaceWith(content: () => Cheerio): Cheerio;
230
231 empty(): Cheerio;
232
233 html(): string | null;
234 html(html: string): Cheerio;
235
236 text(): string;
237 text(text: string): Cheerio;
238
239 wrap(content: string): Cheerio;
240 wrap(content: Document): Cheerio;
241 wrap(content: Cheerio): Cheerio;
242
243 css(propertyName?: string): string;
244 css(propertyNames: string[]): string[];
245 css(propertyName: string, value: string): Cheerio;
246 css(propertyName: string, value: number): Cheerio;
247 css(propertyName: string, func: (index: number, value: string) => string): Cheerio;
248 css(propertyName: string, func: (index: number, value: string) => number): Cheerio;
249 css(properties: Object): Cheerio;
250
251 // Rendering
252
253 // Miscellaneous
254
255 clone(): Cheerio;
256
257 // Not Documented
258
259 toArray(): Element[];
260 }
261
262 interface CheerioParserOptions {
263 // Document References
264 // Cheerio https://github.com/cheeriojs/cheerio
265 // HTMLParser2 https://github.com/fb55/htmlparser2/wiki/Parser-options
266 // DomHandler https://github.com/fb55/DomHandler
267
268 xmlMode?: boolean | undefined;
269 decodeEntities?: boolean | undefined;
270 lowerCaseTags?: boolean | undefined;
271 lowerCaseAttributeNames?: boolean | undefined;
272 recognizeCDATA?: boolean | undefined;
273 recognizeSelfClosing?: boolean | undefined;
274 normalizeWhitespace?: boolean | undefined;
275 withStartIndices?: boolean | undefined;
276 withEndIndices?: boolean | undefined;
277 ignoreWhitespace?: boolean | undefined;
278 _useHtmlParser2?: boolean | undefined;
279 }
280
281 interface Selector {
282 (selector: string): Cheerio;
283 (selector: string, context: string): Cheerio;
284 (selector: string, context: Element): Cheerio;
285 (selector: string, context: Element[]): Cheerio;
286 (selector: string, context: Cheerio): Cheerio;
287 (selector: string, context: string, root: string): Cheerio;
288 (selector: string, context: Element, root: string): Cheerio;
289 (selector: string, context: Element[], root: string): Cheerio;
290 (selector: string, context: Cheerio, root: string): Cheerio;
291 (selector: any): Cheerio;
292 }
293
294 interface Root extends Selector {
295 // Document References
296 // Cheerio https://github.com/cheeriojs/cheerio
297 // JQuery http://api.jquery.com
298 root(): Cheerio;
299 contains(container: Element, contained: Element): boolean;
300 parseHTML(data: string, context?: Document | null, keepScripts?: boolean): Document[];
301
302 html(options?: CheerioParserOptions): string;
303 html(dom: string | Cheerio | Element, options?: CheerioParserOptions): string;
304
305 xml(dom?: string | Cheerio | Element): string;
306 }
307
308 interface CheerioAPI extends Root {
309 version: string;
310 load(html: string | Buffer, options?: CheerioParserOptions): Root;
311 load(element: Element | Element[], options?: CheerioParserOptions): Root;
312 }
313}
314
315declare module "cheerio" {
316 const cheerioModule: cheerio.CheerioAPI;
317 export = cheerioModule;
318}