Select Git revision
jsdom.mjs 2.45 KiB
'use strict';
import {extend} from "../../../application/source/data/extend.mjs";
import {getGlobal} from "../../../application/source/types/global.mjs";
export const isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
export const isNode = new Function("try {return this===global;}catch(e){return false;}");
/**
* this helper function creates the dom stack in the node environment
*
* @return {Promise<unknown>|Promise<void>}
*/
function initJSDOM(options) {
if (typeof window === "object" && window['DOMParser']) return Promise.resolve();
const g = getGlobal();
options = extend({}, {
pretendToBeVisual: true,
contentType: "text/html",
includeNodeLocations: true,
storageQuota: 10000000,
runScripts: "dangerously",
resources: "usable"
}, options || {})
return import("jsdom").then(({JSDOM}) => {
const {window} = new JSDOM(`<html>
<head>
</head>
<body>
<div id="mocks"></div>
</body>`, options);
g['window'] = window;
return new Promise(resolve =>
window.addEventListener("load", () => {
[
'self',
'HTMLCollection',
'NodeList',
'ElementInternals',
'HTMLDocument',
'HTMLFormElement',
'HTMLInputElement',
'HTMLSelectElement',
'HTMLTextAreaElement',
'document',
'Document',
'Node',
'ShadowRoot',
'EventTarget',
'Event',
'CustomEvent',
'Element',
'HTMLElement',
'HTMLDivElement',
'customElements',
'DocumentFragment',
'DOMParser',
'KeyboardEvent',
'CSSStyleSheet',
'HTMLScriptElement',
'MutationObserver',
'HTMLTemplateElement',
'XMLSerializer',
'NodeFilter',
'navigator',
'InputEvent',
'Blob',
'CustomEvent'
].forEach(key => {
g[key] = window[key]
});
resolve();
})
);
});
}
export {initJSDOM}