UNPKG

17.9 kBTypeScriptView Raw
1/// <reference types="node" />
2
3import { EventEmitter } from "events";
4import { ElementLocation } from "parse5";
5import { Context } from "vm";
6import * as tough from "tough-cookie";
7
8// Needed to allow adding properties to `DOMWindow` that are only supported
9// in newer TypeScript versions:
10// tslint:disable-next-line: no-declare-current-package no-single-declare-module
11declare module "jsdom" {
12 const toughCookie: typeof tough;
13 class CookieJar extends tough.CookieJar {}
14
15 interface AbortablePromise<T> extends Promise<T> {
16 abort(): void;
17 }
18
19 class JSDOM {
20 constructor(html?: string | Buffer | BinaryData, options?: ConstructorOptions);
21
22 static fromURL(url: string, options?: BaseOptions): Promise<JSDOM>;
23 static fromFile(url: string, options?: FileOptions): Promise<JSDOM>;
24 static fragment(html: string): DocumentFragment;
25
26 readonly window: DOMWindow;
27 readonly virtualConsole: VirtualConsole;
28 readonly cookieJar: CookieJar;
29
30 /**
31 * The serialize() method will return the HTML serialization of the document, including the doctype.
32 */
33 serialize(): string;
34
35 /**
36 * The nodeLocation() method will find where a DOM node is within the source document,
37 * returning the parse5 location info for the node.
38 *
39 * @throws {Error} If the JSDOM was not created with `includeNodeLocations`
40 */
41 nodeLocation(node: Node): ElementLocation | null;
42
43 /**
44 * The built-in `vm` module of Node.js is what underpins JSDOM's script-running magic.
45 * Some advanced use cases, like pre-compiling a script and then running it multiple
46 * times, benefit from using the `vm` module directly with a jsdom-created `Window`.
47 *
48 * @throws {TypeError}
49 * Note that this method will throw an exception if the `JSDOM` instance was created
50 * 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?: string | 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 interface VirtualConsoleSendToOptions {
160 omitJSDOMErrors: boolean;
161 }
162
163 interface ReconfigureSettings {
164 windowTop?: DOMWindow | undefined;
165 url?: string | undefined;
166 }
167
168 interface FetchOptions {
169 cookieJar?: CookieJar | undefined;
170 referrer?: string | undefined;
171 accept?: string | undefined;
172 element?: HTMLScriptElement | HTMLLinkElement | HTMLIFrameElement | HTMLImageElement | undefined;
173 }
174
175 interface ResourceLoaderConstructorOptions {
176 strictSSL?: boolean | undefined;
177 proxy?: string | undefined;
178 userAgent?: string | undefined;
179 }
180
181 interface DOMWindow extends Omit<Window, "top" | "self" | "window"> {
182 [key: string]: any;
183
184 /* node_modules/jsdom/browser/Window.js */
185 Window: typeof Window;
186 readonly top: DOMWindow;
187 readonly self: DOMWindow;
188 readonly window: DOMWindow;
189
190 /* ECMAScript Globals */
191 globalThis: DOMWindow;
192 readonly ["Infinity"]: number;
193 readonly ["NaN"]: number;
194 readonly undefined: undefined;
195
196 eval(script: string): any;
197 parseInt(s: string, radix?: number): number;
198 parseFloat(string: string): number;
199 isNaN(number: number): boolean;
200 isFinite(number: number): boolean;
201 decodeURI(encodedURI: string): string;
202 decodeURIComponent(encodedURIComponent: string): string;
203 encodeURI(uri: string): string;
204 encodeURIComponent(uriComponent: string | number | boolean): string;
205 escape(string: string): string;
206 unescape(string: string): string;
207
208 Array: typeof Array;
209 ArrayBuffer: typeof ArrayBuffer;
210 Atomics: typeof Atomics;
211 BigInt: typeof BigInt;
212 BigInt64Array: typeof BigInt64Array;
213 BigUint64Array: typeof BigUint64Array;
214 Boolean: typeof Boolean;
215 DataView: typeof DataView;
216 Date: typeof Date;
217 Error: typeof Error;
218 EvalError: typeof EvalError;
219 Float32Array: typeof Float32Array;
220 Float64Array: typeof Float64Array;
221 Function: typeof Function;
222 Int16Array: typeof Int16Array;
223 Int32Array: typeof Int32Array;
224 Int8Array: typeof Int8Array;
225 Intl: typeof Intl;
226 JSON: typeof JSON;
227 Map: typeof Map;
228 Math: typeof Math;
229 Number: typeof Number;
230 Object: typeof Object;
231 Promise: typeof Promise;
232 Proxy: typeof Proxy;
233 RangeError: typeof RangeError;
234 ReferenceError: typeof ReferenceError;
235 Reflect: typeof Reflect;
236 RegExp: typeof RegExp;
237 Set: typeof Set;
238 SharedArrayBuffer: typeof SharedArrayBuffer;
239 String: typeof String;
240 Symbol: typeof Symbol;
241 SyntaxError: typeof SyntaxError;
242 TypeError: typeof TypeError;
243 URIError: typeof URIError;
244 Uint16Array: typeof Uint16Array;
245 Uint32Array: typeof Uint32Array;
246 Uint8Array: typeof Uint8Array;
247 Uint8ClampedArray: typeof Uint8ClampedArray;
248 WeakMap: typeof WeakMap;
249 WeakSet: typeof WeakSet;
250 WebAssembly: typeof WebAssembly;
251
252 /* node_modules/jsdom/living/interfaces.js */
253 DOMException: typeof DOMException;
254
255 URL: typeof URL;
256 URLSearchParams: typeof URLSearchParams;
257
258 EventTarget: typeof EventTarget;
259
260 NamedNodeMap: typeof NamedNodeMap;
261 Node: typeof Node;
262 Attr: typeof Attr;
263 Element: typeof Element;
264 DocumentFragment: typeof DocumentFragment;
265 DOMImplementation: typeof DOMImplementation;
266 Document: typeof Document;
267 HTMLDocument: typeof HTMLDocument;
268 XMLDocument: typeof XMLDocument;
269 CharacterData: typeof CharacterData;
270 Text: typeof Text;
271 CDATASection: typeof CDATASection;
272 ProcessingInstruction: typeof ProcessingInstruction;
273 Comment: typeof Comment;
274 DocumentType: typeof DocumentType;
275 NodeList: typeof NodeList;
276 HTMLCollection: typeof HTMLCollection;
277 HTMLOptionsCollection: typeof HTMLOptionsCollection;
278 DOMStringMap: typeof DOMStringMap;
279 DOMTokenList: typeof DOMTokenList;
280
281 StyleSheetList: typeof StyleSheetList;
282
283 HTMLElement: typeof HTMLElement;
284 HTMLHeadElement: typeof HTMLHeadElement;
285 HTMLTitleElement: typeof HTMLTitleElement;
286 HTMLBaseElement: typeof HTMLBaseElement;
287 HTMLLinkElement: typeof HTMLLinkElement;
288 HTMLMetaElement: typeof HTMLMetaElement;
289 HTMLStyleElement: typeof HTMLStyleElement;
290 HTMLBodyElement: typeof HTMLBodyElement;
291 HTMLHeadingElement: typeof HTMLHeadingElement;
292 HTMLParagraphElement: typeof HTMLParagraphElement;
293 HTMLHRElement: typeof HTMLHRElement;
294 HTMLPreElement: typeof HTMLPreElement;
295 HTMLUListElement: typeof HTMLUListElement;
296 HTMLOListElement: typeof HTMLOListElement;
297 HTMLLIElement: typeof HTMLLIElement;
298 HTMLMenuElement: typeof HTMLMenuElement;
299 HTMLDListElement: typeof HTMLDListElement;
300 HTMLDivElement: typeof HTMLDivElement;
301 HTMLAnchorElement: typeof HTMLAnchorElement;
302 HTMLAreaElement: typeof HTMLAreaElement;
303 HTMLBRElement: typeof HTMLBRElement;
304 HTMLButtonElement: typeof HTMLButtonElement;
305 HTMLCanvasElement: typeof HTMLCanvasElement;
306 HTMLDataElement: typeof HTMLDataElement;
307 HTMLDataListElement: typeof HTMLDataListElement;
308 HTMLDetailsElement: typeof HTMLDetailsElement;
309 HTMLDialogElement: {
310 new(): HTMLDialogElement;
311 readonly prototype: HTMLDialogElement;
312 };
313 HTMLDirectoryElement: typeof HTMLDirectoryElement;
314 HTMLFieldSetElement: typeof HTMLFieldSetElement;
315 HTMLFontElement: typeof HTMLFontElement;
316 HTMLFormElement: typeof HTMLFormElement;
317 HTMLHtmlElement: typeof HTMLHtmlElement;
318 HTMLImageElement: typeof HTMLImageElement;
319 HTMLInputElement: typeof HTMLInputElement;
320 HTMLLabelElement: typeof HTMLLabelElement;
321 HTMLLegendElement: typeof HTMLLegendElement;
322 HTMLMapElement: typeof HTMLMapElement;
323 HTMLMarqueeElement: typeof HTMLMarqueeElement;
324 HTMLMediaElement: typeof HTMLMediaElement;
325 HTMLMeterElement: typeof HTMLMeterElement;
326 HTMLModElement: typeof HTMLModElement;
327 HTMLOptGroupElement: typeof HTMLOptGroupElement;
328 HTMLOptionElement: typeof HTMLOptionElement;
329 HTMLOutputElement: typeof HTMLOutputElement;
330 HTMLPictureElement: typeof HTMLPictureElement;
331 HTMLProgressElement: typeof HTMLProgressElement;
332 HTMLQuoteElement: typeof HTMLQuoteElement;
333 HTMLScriptElement: typeof HTMLScriptElement;
334 HTMLSelectElement: typeof HTMLSelectElement;
335 HTMLSlotElement: typeof HTMLSlotElement;
336 HTMLSourceElement: typeof HTMLSourceElement;
337 HTMLSpanElement: typeof HTMLSpanElement;
338 HTMLTableCaptionElement: typeof HTMLTableCaptionElement;
339 HTMLTableCellElement: typeof HTMLTableCellElement;
340 HTMLTableColElement: typeof HTMLTableColElement;
341 HTMLTableElement: typeof HTMLTableElement;
342 HTMLTimeElement: typeof HTMLTimeElement;
343 HTMLTableRowElement: typeof HTMLTableRowElement;
344 HTMLTableSectionElement: typeof HTMLTableSectionElement;
345 HTMLTemplateElement: typeof HTMLTemplateElement;
346 HTMLTextAreaElement: typeof HTMLTextAreaElement;
347 HTMLUnknownElement: typeof HTMLUnknownElement;
348 HTMLFrameElement: typeof HTMLFrameElement;
349 HTMLFrameSetElement: typeof HTMLFrameSetElement;
350 HTMLIFrameElement: typeof HTMLIFrameElement;
351 HTMLEmbedElement: typeof HTMLEmbedElement;
352 HTMLObjectElement: typeof HTMLObjectElement;
353 HTMLParamElement: typeof HTMLParamElement;
354 HTMLVideoElement: typeof HTMLVideoElement;
355 HTMLAudioElement: typeof HTMLAudioElement;
356 HTMLTrackElement: typeof HTMLTrackElement;
357
358 SVGElement: typeof SVGElement;
359 SVGGraphicsElement: typeof SVGGraphicsElement;
360 SVGSVGElement: typeof SVGSVGElement;
361 SVGTitleElement: typeof SVGTitleElement;
362 SVGAnimatedString: typeof SVGAnimatedString;
363 SVGNumber: typeof SVGNumber;
364 SVGStringList: typeof SVGStringList;
365
366 Event: typeof Event;
367 CloseEvent: typeof CloseEvent;
368 CustomEvent: typeof CustomEvent;
369 MessageEvent: typeof MessageEvent;
370 ErrorEvent: typeof ErrorEvent;
371 HashChangeEvent: typeof HashChangeEvent;
372 PopStateEvent: typeof PopStateEvent;
373 StorageEvent: typeof StorageEvent;
374 ProgressEvent: typeof ProgressEvent;
375 PageTransitionEvent: typeof PageTransitionEvent;
376
377 UIEvent: typeof UIEvent;
378 FocusEvent: typeof FocusEvent;
379 MouseEvent: typeof MouseEvent;
380 KeyboardEvent: typeof KeyboardEvent;
381 TouchEvent: typeof TouchEvent;
382 CompositionEvent: typeof CompositionEvent;
383 WheelEvent: typeof WheelEvent;
384
385 BarProp: typeof BarProp;
386 Location: typeof Location;
387 History: typeof History;
388 Screen: typeof Screen;
389 Performance: typeof Performance;
390 Navigator: typeof Navigator;
391
392 PluginArray: typeof PluginArray;
393 MimeTypeArray: typeof MimeTypeArray;
394 Plugin: typeof Plugin;
395 MimeType: typeof MimeType;
396
397 FileReader: typeof FileReader;
398 Blob: typeof Blob;
399 File: typeof File;
400 FileList: typeof FileList;
401 ValidityState: typeof ValidityState;
402
403 DOMParser: typeof DOMParser;
404 XMLSerializer: typeof XMLSerializer;
405
406 FormData: typeof FormData;
407 XMLHttpRequestEventTarget: typeof XMLHttpRequestEventTarget;
408 XMLHttpRequestUpload: typeof XMLHttpRequestUpload;
409 XMLHttpRequest: typeof XMLHttpRequest;
410 WebSocket: typeof WebSocket;
411
412 NodeFilter: typeof NodeFilter;
413 NodeIterator: typeof NodeIterator;
414 TreeWalker: typeof TreeWalker;
415
416 AbstractRange: typeof AbstractRange;
417 Range: typeof Range;
418 StaticRange: typeof StaticRange;
419 Selection: typeof Selection;
420
421 Storage: typeof Storage;
422
423 CustomElementRegistry: typeof CustomElementRegistry;
424 ShadowRoot: typeof ShadowRoot;
425
426 MutationObserver: typeof MutationObserver;
427 MutationRecord: typeof MutationRecord;
428
429 Headers: typeof Headers;
430 AbortController: typeof AbortController;
431 AbortSignal: typeof AbortSignal;
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
443 /* node_modules/jsdom/level3/xpath.js */
444 // XPathException: typeof XPathException;
445 XPathExpression: typeof XPathExpression;
446 XPathResult: typeof XPathResult;
447 XPathEvaluator: typeof XPathEvaluator;
448 }
449}