Skip to content
Snippets Groups Projects
Select Git revision
  • 6ea0ef5d371af8ee53bcc2e93f45f1b9b83e44e7
  • master default protected
  • 1.2.4
  • 1.2.3
  • 1.2.2
  • 1.2.1
  • 1.2.0
  • v1.1.0
8 results

target-jsdoc-build.mk

Blame
  • util.mjs 6.76 KiB
    /**
     * Copyright schukai GmbH and contributors 2023. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import { getGlobal } from "../types/global.mjs";
    import { validateString } from "../types/validate.mjs";
    
    export { getDocument, getWindow, getDocumentFragmentFromString, findElementWithIdUpwards, getContainingDocument };
    
    /**
     * This method fetches the document object
     *
     * In nodejs this functionality can be performed with [jsdom](https://www.npmjs.com/package/jsdom).
     *
     * ```
     * import {JSDOM} from "jsdom"
     * if (typeof window !== "object") {
     *    const {window} = new JSDOM('', {
     *        url: 'http://example.com/',
     *        pretendToBeVisual: true
     *    });
     *
     *    [
     *        'self',
     *        'document',
     *        'Document',
     *        'Node',
     *        'Element',
     *        'HTMLElement',
     *        'DocumentFragment',
     *        'DOMParser',
     *        'XMLSerializer',
     *        'NodeFilter',
     *        'InputEvent',
     *        'CustomEvent'
     *    ].forEach(key => (getGlobal()[key] = window[key]));
     * }
     * ```
     *
     * @returns {object}
     * @license AGPLv3
     * @since 1.6.0
     * @copyright schukai GmbH
     * @memberOf Monster.DOM
     * @throws {Error} not supported environment
     */
    function getDocument() {
        let document = getGlobal()?.["document"];
        if (typeof document !== "object") {
            throw new Error("not supported environment");
        }
    
        return document;
    }
    
    /**
     * This method fetches the window object
     *
     * In nodejs this functionality can be performed with [jsdom](https://www.npmjs.com/package/jsdom).
     *
     * ```
     * import {JSDOM} from "jsdom"
     * if (typeof window !== "object") {
     *    const {window} = new JSDOM('', {
     *        url: 'http://example.com/',
     *        pretendToBeVisual: true
     *    });