UNPKG

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