UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

99 lines • 3.61 kB
// ***************************************************************************** // Copyright (C) 2018 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** // eslint-disable-next-line import/no-extraneous-dependencies import { JSDOM } from 'jsdom'; /** * ```typescript * const disableJSDOM = enableJSDOM(); * // actions require DOM * disableJSDOM(); * ``` */ export function enableJSDOM() { /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable no-unused-expressions */ // do nothing if running in browser try { global; } catch (e) { return () => { }; } // no need to enable twice if (typeof global['_disableJSDOM'] === 'function') { return global['_disableJSDOM']; } const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/' }); global['document'] = dom.window.document; global['window'] = dom.window; try { global['navigator'] = { userAgent: 'node.js', platform: 'Mac' }; } catch (e) { // node 21+ already has a navigator object } const toCleanup = []; Object.getOwnPropertyNames(dom.window).forEach(property => { if (!(property in global)) { global[property] = dom.window[property]; toCleanup.push(property); } }); dom.window.document['queryCommandSupported'] = function () { }; dom.window.matchMedia = () => ({ matches: false, media: '', // eslint-disable-next-line no-null/no-null onchange: null, addListener: () => undefined, removeListener: () => undefined, addEventListener: () => undefined, removeEventListener: () => undefined, dispatchEvent: () => false }); const disableJSDOM = global['_disableJSDOM'] = () => { let property; while (property = toCleanup.pop()) { delete global[property]; } delete dom.window.document['queryCommandSupported']; delete global['document']; delete global['window']; delete global['navigator']; delete global['_disableJSDOM']; }; return disableJSDOM; } /** * Enables React's act test environment and restores the previous value when disposed. */ export function enableReactActEnvironment() { const reactActEnvironment = globalThis; const hadReactActEnvironment = Object.prototype.hasOwnProperty.call(reactActEnvironment, 'IS_REACT_ACT_ENVIRONMENT'); const previousReactActEnvironment = reactActEnvironment.IS_REACT_ACT_ENVIRONMENT; reactActEnvironment.IS_REACT_ACT_ENVIRONMENT = true; return () => { if (hadReactActEnvironment) { reactActEnvironment.IS_REACT_ACT_ENVIRONMENT = previousReactActEnvironment; } else { delete reactActEnvironment.IS_REACT_ACT_ENVIRONMENT; } }; } //# sourceMappingURL=jsdom.js.map