UNPKG

17.6 kBTypeScriptView Raw
1// Type definitions for jsdom 16.2
2// Project: https://github.com/jsdom/jsdom
3// Definitions by: Leonard Thieu <https://github.com/leonard-thieu>
4// Johan Palmfjord <https://github.com/palmfjord>
5// ExE Boss <https://github.com/ExE-Boss>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 3.0
8
9/// <reference types="node" />
10
11import { EventEmitter } from 'events';
12import { ElementLocation } from 'parse5';
13import { Context } from 'vm';
14import * as tough from 'tough-cookie';
15
16// Needed to allow adding properties to `DOMWindow` that are only supported
17// in newer TypeScript versions:
18// tslint:disable-next-line: no-declare-current-package no-single-declare-module
19declare module 'jsdom' {
20 const toughCookie: typeof tough;
21 class CookieJar extends tough.CookieJar {}
22
23 class JSDOM {
24 constructor(html?: string | Buffer | BinaryData, options?: ConstructorOptions);
25
26 static fromURL(url: string, options?: BaseOptions): Promise<JSDOM>;
27 static fromFile(url: string, options?: FileOptions): Promise<JSDOM>;
28 static fragment(html: string): DocumentFragment;
29
30 readonly window: DOMWindow;
31 readonly virtualConsole: VirtualConsole;
32 readonly cookieJar: CookieJar;
33
34 /**
35 * The serialize() method will return the HTML serialization of the document, including the doctype.
36 */
37 serialize(): string;
38
39 /**
40 * The nodeLocation() method will find where a DOM node is within the source document,
41 * returning the parse5 location info for the node.
42 *
43 * @throws {Error} If the JSDOM was not created with `includeNodeLocations`
44 */
45 nodeLocation(node: Node): ElementLocation | null;
46
47 /**
48 * The built-in `vm` module of Node.js is what underpins JSDOM's script-running magic.
49 * Some advanced use cases, like pre-compiling a script and then running it multiple
50 * times, benefit from using the `vm` module directly with a jsdom-created `Window`.
51 *
52 * @throws {TypeError}
53 * Note that this method will throw an exception if the `JSDOM` instance was created
54 * without `runScripts` set, or if you are using JSDOM in a web browser.
55 */
56 getInternalVMContext(): Context;
57
58 /**
59 * The reconfigure method allows changing the `window.top` and url from the outside.
60 */
61 reconfigure(settings: ReconfigureSettings): void;
62 }
63
64 class ResourceLoader {
65 fetch(url: string, options: FetchOptions): Promise<Buffer> | null;
66
67 constructor(obj?: ResourceLoaderConstructorOptions);
68 }
69
70 class VirtualConsole extends EventEmitter {
71 on<K extends keyof Console>(method: K, callback: Console[K]): this;
72 on(event: 'jsdomError', callback: (e: Error) => void): this;
73
74 sendTo(console: Console, options?: VirtualConsoleSendToOptions): this;
75 }
76
77 type BinaryData =
78 | ArrayBuffer
79 | DataView
80 | Int8Array
81 | Uint8Array
82 | Uint8ClampedArray
83 | Int16Array
84 | Uint16Array
85 | Int32Array
86 | Uint32Array
87 | Float32Array
88 | Float64Array;
89
90 interface BaseOptions {
91 /**
92 * referrer just affects the value read from document.referrer.
93 * It defaults to no referrer (which reflects as the empty string).
94 */
95 referrer?: string;
96
97 /**
98 * userAgent affects the value read from navigator.userAgent, as well as the User-Agent header sent while fetching subresources.
99 *
100 * @default
101 * `Mozilla/5.0 (${process.platform}) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/${jsdomVersion}`
102 */
103 userAgent?: string;
104
105 /**
106 * `includeNodeLocations` preserves the location info produced by the HTML parser,
107 * allowing you to retrieve it with the nodeLocation() method (described below).
108 *
109 * It defaults to false to give the best performance,
110 * and cannot be used with an XML content type since our XML parser does not support location info.
111 *
112 * @default false
113 */
114 includeNodeLocations?: boolean;
115 runScripts?: 'dangerously' | 'outside-only';
116 resources?: 'usable' | ResourceLoader;
117 virtualConsole?: VirtualConsole;
118 cookieJar?: CookieJar;
119
120 /**
121 * jsdom does not have the capability to render visual content, and will act like a headless browser by default.
122 * It provides hints to web pages through APIs such as document.hidden that their content is not visible.
123 *
124 * When the `pretendToBeVisual` option is set to `true`, jsdom will pretend that it is rendering and displaying
125 * content.
126 *
127 * @default false
128 */
129 pretendToBeVisual?: boolean;
130 beforeParse?(window: DOMWindow): void;
131 }
132
133 interface FileOptions extends BaseOptions {
134 /**
135 * url sets the value returned by window.location, document.URL, and document.documentURI,
136 * and affects things like resolution of relative URLs within the document
137 * and the same-origin restrictions and referrer used while fetching subresources.
138 * It will default to a file URL corresponding to the given filename, instead of to "about:blank".
139 */
140 url?: string;
141
142 /**
143 * contentType affects the value read from document.contentType, and how the document is parsed: as HTML or as XML.
144 * Values that are not "text/html" or an XML mime type will throw. It will default to "application/xhtml+xml" if
145 * the given filename ends in .xhtml or .xml; otherwise it will continue to default to "text/html".
146 */
147 contentType?: string;
148 }
149
150 interface ConstructorOptions extends BaseOptions {
151 /**
152 * url sets the value returned by window.location, document.URL, and document.documentURI,
153 * and affects things like resolution of relative URLs within the document
154 * and the same-origin restrictions and referrer used while fetching subresources.
155 * It defaults to "about:blank".
156 */
157 url?: string;
158
159 /**
160 * contentType affects the value read from document.contentType, and how the document is parsed: as HTML or as XML.
161 * Values that are not "text/html" or an XML mime type will throw. It defaults to "text/html".
162 */
163 contentType?: string;
164
165 /**
166 * The maximum size in code units for the separate storage areas used by localStorage and sessionStorage.
167 * Attempts to store data larger than this limit will cause a DOMException to be thrown. By default, it is set
168 * to 5,000,000 code units per origin, as inspired by the HTML specification.
169 *
170 * @default 5_000_000
171 */
172 storageQuota?: number;
173 }
174
175 interface VirtualConsoleSendToOptions {
176 omitJSDOMErrors: boolean;
177 }
178
179 interface ReconfigureSettings {
180 windowTop?: DOMWindow;
181 url?: string;
182 }
183
184 interface FetchOptions {
185 cookieJar?: CookieJar;
186 referrer?: string;
187 accept?: string;
188 element?: HTMLScriptElement | HTMLLinkElement | HTMLIFrameElement | HTMLImageElement;
189 }
190
191 interface ResourceLoaderConstructorOptions {
192 strictSSL?: boolean;
193 proxy?: string;
194 userAgent?: string;
195 }
196
197 interface DOMWindow extends Pick<Window, Exclude<keyof Window, 'top' | 'self' | 'window'>> {
198 [key: string]: any;
199
200 /* node_modules/jsdom/browser/Window.js */
201 Window: typeof Window;
202 readonly top: DOMWindow;
203 readonly self: DOMWindow;
204 readonly window: DOMWindow;
205
206 /* ECMAScript Globals */
207 globalThis: DOMWindow;
208 readonly ['Infinity']: number;
209 readonly ['NaN']: number;
210 readonly undefined: undefined;
211
212 eval(script: string): any;
213 parseInt(s: string, radix?: number): number;
214 parseFloat(string: string): number;
215 isNaN(number: number): boolean;
216 isFinite(number: number): boolean;
217 decodeURI(encodedURI: string): string;
218 decodeURIComponent(encodedURIComponent: string): string;
219 encodeURI(uri: string): string;
220 encodeURIComponent(uriComponent: string | number | boolean): string;
221 escape(string: string): string;
222 unescape(string: string): string;
223
224 Array: typeof Array;
225 ArrayBuffer: typeof ArrayBuffer;
226 Boolean: typeof Boolean;
227 DataView: typeof DataView;
228 Date: typeof Date;
229 Error: typeof Error;
230 EvalError: typeof EvalError;
231 Float32Array: typeof Float32Array;
232 Float64Array: typeof Float64Array;
233 Function: typeof Function;
234 Int16Array: typeof Int16Array;
235 Int32Array: typeof Int32Array;
236 Int8Array: typeof Int8Array;
237 Intl: typeof Intl;
238 JSON: typeof JSON;
239 Map: typeof Map;
240 Math: typeof Math;
241 Number: typeof Number;
242 Object: typeof Object;
243 Promise: typeof Promise;
244 Proxy: typeof Proxy;
245 RangeError: typeof RangeError;
246 ReferenceError: typeof ReferenceError;
247 Reflect: typeof Reflect;
248 RegExp: typeof RegExp;
249 Set: typeof Set;
250 String: typeof String;
251 Symbol: typeof Symbol;
252 SyntaxError: typeof SyntaxError;
253 TypeError: typeof TypeError;
254 URIError: typeof URIError;
255 Uint16Array: typeof Uint16Array;
256 Uint32Array: typeof Uint32Array;
257 Uint8Array: typeof Uint8Array;
258 Uint8ClampedArray: typeof Uint8ClampedArray;
259 WeakMap: typeof WeakMap;
260 WeakSet: typeof WeakSet;
261
262 /* node_modules/jsdom/living/interfaces.js */
263 DOMException: typeof DOMException;
264
265 URL: typeof URL;
266 URLSearchParams: typeof URLSearchParams;
267
268 EventTarget: typeof EventTarget;
269
270 NamedNodeMap: typeof NamedNodeMap;
271 Node: typeof Node;
272 Attr: typeof Attr;
273 Element: typeof Element;
274 DocumentFragment: typeof DocumentFragment;
275 Document: typeof Document;
276 XMLDocument: typeof XMLDocument;
277 CharacterData: typeof CharacterData;
278 Text: typeof Text;
279 CDATASection: typeof CDATASection;
280 ProcessingInstruction: typeof ProcessingInstruction;
281 Comment: typeof Comment;
282 DocumentType: typeof DocumentType;
283 DOMImplementation: typeof DOMImplementation;
284 NodeList: typeof NodeList;
285 HTMLCollection: typeof HTMLCollection;
286 HTMLOptionsCollection: typeof HTMLOptionsCollection;
287 DOMStringMap: typeof DOMStringMap;
288 DOMTokenList: typeof DOMTokenList;
289
290 HTMLElement: typeof HTMLElement;
291 HTMLHeadElement: typeof HTMLHeadElement;
292 HTMLTitleElement: typeof HTMLTitleElement;
293 HTMLBaseElement: typeof HTMLBaseElement;
294 HTMLLinkElement: typeof HTMLLinkElement;
295 HTMLMetaElement: typeof HTMLMetaElement;
296 HTMLStyleElement: typeof HTMLStyleElement;
297 HTMLBodyElement: typeof HTMLBodyElement;
298 HTMLHeadingElement: typeof HTMLHeadingElement;
299 HTMLParagraphElement: typeof HTMLParagraphElement;
300 HTMLHRElement: typeof HTMLHRElement;
301 HTMLPreElement: typeof HTMLPreElement;
302 HTMLUListElement: typeof HTMLUListElement;
303 HTMLOListElement: typeof HTMLOListElement;
304 HTMLLIElement: typeof HTMLLIElement;
305 HTMLMenuElement: typeof HTMLMenuElement;
306 HTMLDListElement: typeof HTMLDListElement;
307 HTMLDivElement: typeof HTMLDivElement;
308 HTMLAnchorElement: typeof HTMLAnchorElement;
309 HTMLAreaElement: typeof HTMLAreaElement;
310 HTMLBRElement: typeof HTMLBRElement;
311 HTMLButtonElement: typeof HTMLButtonElement;
312 HTMLCanvasElement: typeof HTMLCanvasElement;
313 HTMLDataElement: typeof HTMLDataElement;
314 HTMLDataListElement: typeof HTMLDataListElement;
315 HTMLDetailsElement: typeof HTMLDetailsElement;
316 HTMLDialogElement: typeof HTMLDialogElement;
317 HTMLDirectoryElement: typeof HTMLDirectoryElement;
318 HTMLFieldSetElement: typeof HTMLFieldSetElement;
319 HTMLFontElement: typeof HTMLFontElement;
320 HTMLFormElement: typeof HTMLFormElement;
321 HTMLHtmlElement: typeof HTMLHtmlElement;
322 HTMLImageElement: typeof HTMLImageElement;
323 HTMLInputElement: typeof HTMLInputElement;
324 HTMLLabelElement: typeof HTMLLabelElement;
325 HTMLLegendElement: typeof HTMLLegendElement;
326 HTMLMapElement: typeof HTMLMapElement;
327 HTMLMarqueeElement: typeof HTMLMarqueeElement;
328 HTMLMediaElement: typeof HTMLMediaElement;
329 HTMLMeterElement: typeof HTMLMeterElement;
330 HTMLModElement: typeof HTMLModElement;
331 HTMLOptGroupElement: typeof HTMLOptGroupElement;
332 HTMLOptionElement: typeof HTMLOptionElement;
333 HTMLOutputElement: typeof HTMLOutputElement;
334 HTMLPictureElement: typeof HTMLPictureElement;
335 HTMLProgressElement: typeof HTMLProgressElement;
336 HTMLQuoteElement: typeof HTMLQuoteElement;
337 HTMLScriptElement: typeof HTMLScriptElement;
338 HTMLSelectElement: typeof HTMLSelectElement;
339 HTMLSourceElement: typeof HTMLSourceElement;
340 HTMLSpanElement: typeof HTMLSpanElement;
341 HTMLTableCaptionElement: typeof HTMLTableCaptionElement;
342 HTMLTableCellElement: typeof HTMLTableCellElement;
343 HTMLTableColElement: typeof HTMLTableColElement;
344 HTMLTableElement: typeof HTMLTableElement;
345 HTMLTimeElement: typeof HTMLTimeElement;
346 HTMLTableRowElement: typeof HTMLTableRowElement;
347 HTMLTableSectionElement: typeof HTMLTableSectionElement;
348 HTMLTemplateElement: typeof HTMLTemplateElement;
349 HTMLTextAreaElement: typeof HTMLTextAreaElement;
350 HTMLUnknownElement: typeof HTMLUnknownElement;
351 HTMLFrameElement: typeof HTMLFrameElement;
352 HTMLFrameSetElement: typeof HTMLFrameSetElement;
353 HTMLIFrameElement: typeof HTMLIFrameElement;
354 HTMLEmbedElement: typeof HTMLEmbedElement;
355 HTMLObjectElement: typeof HTMLObjectElement;
356 HTMLParamElement: typeof HTMLParamElement;
357 HTMLVideoElement: typeof HTMLVideoElement;
358 HTMLAudioElement: typeof HTMLAudioElement;
359 HTMLTrackElement: typeof HTMLTrackElement;
360
361 SVGElement: typeof SVGElement;
362 SVGGraphicsElement: typeof SVGGraphicsElement;
363 SVGSVGElement: typeof SVGSVGElement;
364 SVGTitleElement: typeof SVGTitleElement;
365 SVGAnimatedString: typeof SVGAnimatedString;
366 SVGNumber: typeof SVGNumber;
367 SVGStringList: typeof SVGStringList;
368
369 Event: typeof Event;
370 CloseEvent: typeof CloseEvent;
371 CustomEvent: typeof CustomEvent;
372 MessageEvent: typeof MessageEvent;
373 ErrorEvent: typeof ErrorEvent;
374 HashChangeEvent: typeof HashChangeEvent;
375 PopStateEvent: typeof PopStateEvent;
376 StorageEvent: typeof StorageEvent;
377 ProgressEvent: typeof ProgressEvent;
378 PageTransitionEvent: typeof PageTransitionEvent;
379
380 UIEvent: typeof UIEvent;
381 FocusEvent: typeof FocusEvent;
382 MouseEvent: typeof MouseEvent;
383 KeyboardEvent: typeof KeyboardEvent;
384 TouchEvent: typeof TouchEvent;
385 CompositionEvent: typeof CompositionEvent;
386 WheelEvent: typeof WheelEvent;
387
388 BarProp: typeof BarProp;
389 Location: typeof Location;
390 History: typeof History;
391 Screen: typeof Screen;
392 Performance: typeof Performance;
393 Navigator: typeof Navigator;
394
395 PluginArray: typeof PluginArray;
396 MimeTypeArray: typeof MimeTypeArray;
397 Plugin: typeof Plugin;
398 MimeType: typeof MimeType;
399
400 FileReader: typeof FileReader;
401 Blob: typeof Blob;
402 File: typeof File;
403 FileList: typeof FileList;
404 ValidityState: typeof ValidityState;
405
406 DOMParser: typeof DOMParser;
407 XMLSerializer: typeof XMLSerializer;
408
409 FormData: typeof FormData;
410 XMLHttpRequestEventTarget: typeof XMLHttpRequestEventTarget;
411 XMLHttpRequestUpload: typeof XMLHttpRequestUpload;
412 XMLHttpRequest: typeof XMLHttpRequest;
413 WebSocket: typeof WebSocket;
414
415 NodeIterator: typeof NodeIterator;
416 TreeWalker: typeof TreeWalker;
417
418 Range: typeof Range;
419 Selection: typeof Selection;
420
421 Storage: typeof Storage;
422
423 MutationObserver: typeof MutationObserver;
424 MutationRecord: typeof MutationRecord;
425
426 Headers: typeof Headers;
427 AbortController: typeof AbortController;
428 AbortSignal: typeof AbortSignal;
429
430 /* node_modules/jsdom/living/node-filter.js */
431 NodeFilter: typeof NodeFilter;
432
433 /* node_modules/jsdom/level2/style.js */
434 StyleSheet: typeof StyleSheet;
435 MediaList: typeof MediaList;
436 CSSStyleSheet: typeof CSSStyleSheet;
437 CSSRule: typeof CSSRule;
438 CSSStyleRule: typeof CSSStyleRule;
439 CSSMediaRule: typeof CSSMediaRule;
440 CSSImportRule: typeof CSSImportRule;
441 CSSStyleDeclaration: typeof CSSStyleDeclaration;
442 StyleSheetList: typeof StyleSheetList;
443
444 /* node_modules/jsdom/level3/xpath.js */
445 // XPathException: typeof XPathException;
446 XPathExpression: typeof XPathExpression;
447 XPathResult: typeof XPathResult;
448 XPathEvaluator: typeof XPathEvaluator;
449 }
450}