enable-window-document

1.19.0 • Public • Published

Enable window and document

This package enables the window and document globals for executing most browser JS. For fully simulating the browser, see the enable-browser-mode plugin.

Usage

/* [CommonJS] */
require('enable-window-document');

- or -

/* [ES6] */
import 'enable-window-document'

No variable assignment required, just call it! The variables are stored on the global object so you can refer to them as you normally would in browser JS.

Example

Won't work:

console.log(document.createElement('a'));

>   ReferenceError: document is not defined

Works like a charm:

require('enable-window-document');
console.log(document.createElement('a'));

>   HTMLAnchorElement {Symbol(impl): HTMLAnchorElementImpl}

Implementation

This package simply creates a blank JSDOM with a few lines of code, and stores the global window and document variables, which point to the empty DOM:

let JSDOM = require('jsdom'),
    DOM = new JSDOM.JSDOM(`<html><body></body></html>`, {
        url: 'https://localhost',
        resources: 'usable',
        runScripts: global.UNSAFE_MODE 
            ? 'dangerously' 
            : 'outside-only'
    });

global.window = DOM.window,
global.document = window.document;

The url param is set for compatibility reasons related to localStorage, and resources and runScripts are set by default so that external scripts work as expected.

Unsafe Mode

By default, JSDOM is called with runScripts: 'outside-only'. Set global.UNSAFE_MODE before your require('enable-window-document') call to enable dangerous mode and execute external scripts when added to DOM.

Package Sidebar

Install

npm i enable-window-document

Weekly Downloads

1

Version

1.19.0

License

SEE LICENSE IN .LICENSE/LICENSE

Unpacked Size

7.36 kB

Total Files

5

Last publish

Collaborators

  • ctjlewis