UNPKG

2.59 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.enableJSDOM = void 0;
19// eslint-disable-next-line import/no-extraneous-dependencies
20const jsdom_1 = require("jsdom");
21/**
22 * ```typescript
23 * const disableJSDOM = enableJSDOM();
24 * // actions require DOM
25 * disableJSDOM();
26 * ```
27 */
28function enableJSDOM() {
29 /* eslint-disable @typescript-eslint/no-explicit-any */
30 /* eslint-disable no-unused-expressions */
31 // do nothing if running in browser
32 try {
33 global;
34 }
35 catch (e) {
36 return () => { };
37 }
38 // no need to enable twice
39 if (typeof global['_disableJSDOM'] === 'function') {
40 return global['_disableJSDOM'];
41 }
42 const dom = new jsdom_1.JSDOM('<!doctype html><html><body></body></html>', {
43 url: 'http://localhost/'
44 });
45 global['document'] = dom.window.document;
46 global['window'] = dom.window;
47 global['navigator'] = { userAgent: 'node.js', platform: 'Mac' };
48 const toCleanup = [];
49 Object.getOwnPropertyNames(dom.window).forEach(property => {
50 if (!(property in global)) {
51 global[property] = dom.window[property];
52 toCleanup.push(property);
53 }
54 });
55 dom.window.document['queryCommandSupported'] = function () { };
56 const disableJSDOM = global['_disableJSDOM'] = () => {
57 let property;
58 while (property = toCleanup.pop()) {
59 delete global[property];
60 }
61 delete dom.window.document['queryCommandSupported'];
62 delete global['document'];
63 delete global['window'];
64 delete global['navigator'];
65 delete global['_disableJSDOM'];
66 };
67 return disableJSDOM;
68}
69exports.enableJSDOM = enableJSDOM;
70//# sourceMappingURL=jsdom.js.map
\No newline at end of file