|
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const HTMLElement_1 = __importDefault(require("../html-element/HTMLElement"));
- const ScriptUtility_1 = __importDefault(require("./ScriptUtility"));
- /**
- * HTML Script Element.
- *
- * Reference:
- * https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement.
- */
- class HTMLScriptElement extends HTMLElement_1.default {
- constructor() {
- super(...arguments);
- this.onerror = null;
- this.onload = null;
- this._evaluateScript = true;
- }
- /**
- * Returns type.
- *
- * @returns Type.
- */
- get type() {
- return this.getAttributeNS(null, 'type') || '';
- }
- /**
- * Sets type.
- *
- * @param type Type.
- */
- set type(type) {
- this.setAttributeNS(null, 'type', type);
- }
- /**
- * Returns source.
- *
- * @returns Source.
- */
- get src() {
- return this.getAttributeNS(null, 'src') || '';
- }
- /**
- * Sets source.
- *
- * @param source Source.
- */
- set src(src) {
- this.setAttributeNS(null, 'src', src);
- }
- /**
- * Returns charset.
- *
- * @returns Charset.
- */
- get charset() {
- return this.getAttributeNS(null, 'charset') || '';
- }
- /**
- * Sets charset.
- *
- * @param charset Charset.
- */
- set charset(charset) {
- this.setAttributeNS(null, 'charset', charset);
- }
- /**
- * Returns lang.
- *
- * @returns Lang.
- */
- get lang() {
- return this.getAttributeNS(null, 'lang') || '';
- }
- /**
- * Sets lang.
- *
- * @param lang Lang.
- */
- set lang(lang) {
- this.setAttributeNS(null, 'lang', lang);
- }
- /**
- * Returns async.
- *
- * @returns Async.
- */
- get async() {
- return this.getAttributeNS(null, 'async') !== null;
- }
- /**
- * Sets async.
- *
- * @param async Async.
- */
- set async(async) {
- if (!async) {
- this.removeAttributeNS(null, 'async');
- }
- else {
- this.setAttributeNS(null, 'async', '');
- }
- }
- /**
- * Returns defer.
- *
- * @returns Defer.
- */
- get defer() {
- return this.getAttributeNS(null, 'defer') !== null;
- }
- /**
- * Sets defer.
- *
- * @param defer Defer.
- */
- set defer(defer) {
- if (!defer) {
- this.removeAttributeNS(null, 'defer');
- }
- else {
- this.setAttributeNS(null, 'defer', '');
- }
- }
- /**
- * Returns text.
- *
- * @returns Text.
- */
- get text() {
- return this.textContent;
- }
- /**
- * Sets text.
- *
- * @param text Text.
- */
- set text(text) {
- this.textContent = text;
- }
- /**
- * The setAttributeNode() method adds a new Attr node to the specified element.
- *
- * @override
- * @param attribute Attribute.
- * @returns Replaced attribute.
- */
- setAttributeNode(attribute) {
- const replacedAttribute = super.setAttributeNode(attribute);
- if (attribute.name === 'src' && attribute.value !== null && this.isConnected) {
- ScriptUtility_1.default.loadExternalScript(this);
- }
- return replacedAttribute;
- }
- /**
- * Clones a node.
- *
- * @override
- * @param [deep=false] "true" to clone deep.
- * @returns Cloned node.
- */
- cloneNode(deep = false) {
- return super.cloneNode(deep);
- }
- /**
- * @override
- */
- _connectToNode(parentNode = null) {
- const isConnected = this.isConnected;
- const isParentConnected = parentNode ? parentNode.isConnected : false;
- super._connectToNode(parentNode);
- if (isParentConnected && isConnected !== isParentConnected && this._evaluateScript) {
- const src = this.getAttributeNS(null, 'src');
- if (src !== null) {
- ScriptUtility_1.default.loadExternalScript(this);
- }
- else if (!this.ownerDocument.defaultView.happyDOM.settings.disableJavaScriptEvaluation) {
- const textContent = this.textContent;
- const type = this.getAttributeNS(null, 'type');
- if (textContent &&
- (type === null ||
- type === 'application/x-ecmascript' ||
- type === 'application/x-javascript' ||
- type.startsWith('text/javascript'))) {
- this.ownerDocument.defaultView.eval(textContent);
- }
- }
- }
- }
- }
- exports.default = HTMLScriptElement;
- //# sourceMappingURL=HTMLScriptElement.js.map
|