UNPKG

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