'use strict';
/**
* @author schukai GmbH
*/
import {Monster, getGlobal} from "../types/global.js";
import {validateString} from "../types/validate.js";
/**
* this method fetches the document object
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(Monster.DOM.getDocument())
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {getDocument} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(getDocument())
* </script>
* ```
*
* 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}
* @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
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(Monster.DOM.getWindow())
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {getWindow} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(getWindow(null))
* </script>
* ```
*
* 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
* });
*
* getGlobal()['window']=window;
*
* [
* 'self',
* 'document',
* 'Document',
* 'Node',
* 'Element',
* 'HTMLElement',
* 'DocumentFragment',
* 'DOMParser',
* 'XMLSerializer',
* 'NodeFilter',
* 'InputEvent',
* 'CustomEvent'
* ].forEach(key => (getGlobal()[key] = window[key]));
* }
* ```
*
* @returns {object}
* @since 1.6.0
* @copyright schukai GmbH
* @memberOf Monster/DOM
* @throws {Error} not supported environment
*/
function getWindow() {
let window = getGlobal()?.['window'];
if (typeof window !== 'object') {
throw new Error("not supported environment")
}
return window;
}
/**
*
*
* this method fetches the document object
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(Monster.DOM.getDocumentFragmentFromString())
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {getDocumentFragmentFromString} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.6.0/dist/modules/dom/util.js';
* console.log(getDocumentFragmentFromString('<div></div>'))
* </script>
* ```
*
* 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 {DocumentFragment}
* @since 1.6.0
* @copyright schukai GmbH
* @memberOf Monster/DOM
* @throws {Error} not supported environment
* @throws {TypeError} value is not a string
*/
function getDocumentFragmentFromString(html) {
validateString(html);
const document = getDocument();
const template = document.createElement('template');
template.innerHTML = html;
return template.content;
}
Monster.assignToNamespace('Monster.DOM', getWindow, getDocument, getDocumentFragmentFromString);
export {Monster, getWindow, getDocument, getDocumentFragmentFromString}