UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

116 lines (107 loc) 3.4 kB
/** * Copyright © Volker Schukai and all contributing authors, {{copyRightYear}}. All rights reserved. * Node module: @schukai/monster * * This source code is licensed under the GNU Affero General Public License version 3 (AGPLv3). * The full text of the license can be found at: https://www.gnu.org/licenses/agpl-3.0.en.html * * For those who do not wish to adhere to the AGPLv3, a commercial license is available. * Acquiring a commercial license allows you to use this software without complying with the AGPLv3 terms. * For more information about purchasing a commercial license, please contact Volker Schukai. * * SPDX-License-Identifier: AGPL-3.0 */ import { extend } from "../../data/extend.mjs"; import { ATTRIBUTE_CLASS, ATTRIBUTE_ID, ATTRIBUTE_NONCE, ATTRIBUTE_SRC, ATTRIBUTE_TITLE, ATTRIBUTE_TYPE, TAG_SCRIPT, } from "../constants.mjs"; import { KEY_DOCUMENT, referenceSymbol, Resource } from "../resource.mjs"; import { instanceSymbol } from "../../constants.mjs"; export { Script }; /** * This class is used by the resource manager to embed scripts. * * @license AGPLv3 * @since 1.25.0 * @copyright Volker Schukai * @summary A Resource class */ class Script extends Resource { /** * @property {boolean} async=true {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-async} * @property {string} crossOrigin=anonymous {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-crossorigin} * @property {boolean} defer=false {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer} * @property {string} integrity {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-integrity} * @property {boolean} nomodule {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nomodule} * @property {string} nonce {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-nonce} * @property {string} referrerpolicy {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-referrerpolicy} * @property {string} type {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-type} */ get defaults() { return extend({}, super.defaults, { async: true, crossOrigin: "anonymous", defer: false, integrity: undefined, nomodule: false, nonce: undefined, referrerpolicy: undefined, type: "text/javascript", }); } /** * This method is called by the `instanceof` operator. * @return {symbol} * @since 2.1.0 */ static get [instanceSymbol]() { return Symbol.for("@schukai/monster/dom/resource/script"); } /** * * @return {Monster.DOM.Resource.Script} */ create() { createElement.call(this); return this; } /** * @return {string} */ static getURLAttribute() { return ATTRIBUTE_SRC; } } /** * @private * @return {Monster.DOM.Resource.Script} */ function createElement() { const document = this.getOption(KEY_DOCUMENT); this[referenceSymbol] = document.createElement(TAG_SCRIPT); for (const key of [ "crossOrigin", "defer", "async", "integrity", "nomodule", ATTRIBUTE_NONCE, "referrerpolicy", ATTRIBUTE_TYPE, ATTRIBUTE_SRC, ATTRIBUTE_ID, ATTRIBUTE_CLASS, ATTRIBUTE_TITLE, ]) { if (this.getOption(key) !== undefined) { this[referenceSymbol][key] = this.getOption(key); } } return this; }