UNPKG

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