Skip to content
Snippets Groups Projects
Select Git revision
  • 1ee0e0f05c6c00e5400b0ff14316d2dec3bd8635
  • master default protected
  • 0.5.9
  • 0.5.8
  • 0.5.7
  • 0.5.6
  • 0.5.5
  • 0.5.4
  • 0.5.3
  • 0.5.2
  • 0.5.1
  • 0.5.0
  • 0.4.17
  • 0.4.16
  • 0.4.15
  • 0.4.14
  • 0.4.13
  • 0.4.12
  • 0.4.11
  • 0.4.10
  • 0.4.9
  • 0.4.8
22 results

scannerc.go

Blame
  • jsdom.mjs 2.94 KiB
    'use strict';
    
    import {extend} from "../../source/data/extend.mjs";
    import {getGlobal} from "../../source/types/global.mjs";
    //import Storage from "dom-storage";
    
    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;}");
    
    let JSDOMExport = null;
    
    /**
     * 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}) => {
            JSDOMExport = 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", () => {
    
                    [
                        'Blob',
                        'CSSStyleSheet',
                        'customElements',
                        'CustomEvent',
                        'document',
                        'Document',
                        'DocumentFragment',
                        'DOMParser',
                        'Element',
                        'ElementInternals',
                        'Event',
                        'EventTarget',
                        'getComputedStyle',
                        'HTMLButtonElement',
                        'HTMLCollection',
                        'HTMLDivElement',
                        'HTMLDocument',
                        'HTMLElement',
                        'HTMLFormElement',
                        'HTMLInputElement',
                        'HTMLScriptElement',
                        'HTMLSelectElement',
                        'HTMLTemplateElement',
                        'HTMLTextAreaElement',
                        'InputEvent',
                        'KeyboardEvent',
                        'MutationObserver',
                        'navigator',
                        'Node',
                        'NodeFilter',
                        'NodeList',
                        'self',
                        'ShadowRoot',
                        'XMLSerializer',
                    ].forEach(key => {
                        g[key] = window[key]
                    });
    
    
                    import("dom-storage").then(({default: Storage}) => {
    
    
                        g.localStorage = new Storage(null, {strict: true});
                        g.sessionStorage = new Storage(null, {strict: true});
    
                        window['localStorage'] = g.localStorage;
                        window['sessionStorage'] = g.sessionStorage;
                    });
    
                    resolve(g);
    
                })
            );
    
    
        });
    }
    
    export {initJSDOM, JSDOMExport}