import HTMLElement from '../html-element/HTMLElement.js';
import HTMLScriptElementUtility from './HTMLScriptElementUtility.js';
/**
* HTML Script Element.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement.
*/
export default class HTMLScriptElement extends HTMLElement {
constructor() {
super(...arguments);
this.onerror = null;
this.onload = null;
this._evaluateScript = true;
}
/**
* Returns type.
*
* @returns Type.
*/
get type() {
return this.getAttribute('type') || '';
}
/**
* Sets type.
*
* @param type Type.
*/
set type(type) {
this.setAttribute('type', type);
}
/**
* Returns source.
*
* @returns Source.
*/
get src() {
return this.getAttribute('src') || '';
}
/**
* Sets source.
*
* @param source Source.
*/
set src(src) {
this.setAttribute('src', src);
}
/**
* Returns charset.
*
* @returns Charset.
*/
get charset() {
return this.getAttribute('charset') || '';
}
/**
* Sets charset.
*
* @param charset Charset.
*/
set charset(charset) {
this.setAttribute('charset', charset);
}
/**
* Returns lang.
*
* @returns Lang.
*/
get lang() {
return this.getAttribute('lang') || '';
}
/**
* Sets lang.
*
* @param lang Lang.
*/
set lang(lang) {
this.setAttribute('lang', lang);
}
/**
* Returns async.
*
* @returns Async.
*/
get async() {
return this.getAttribute('async') !== null;
}
/**
* Sets async.
*
* @param async Async.
*/
set async(async) {
if (!async) {
this.removeAttribute('async');
}
else {
this.setAttribute('async', '');
}
}
/**
* Returns defer.
*
* @returns Defer.
*/
get defer() {
return this.getAttribute('defer') !== null;
}
/**
* Sets defer.
*
* @param defer Defer.
*/
set defer(defer) {
if (!defer) {
this.removeAttribute('defer');
}
else {
this.setAttribute('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) {
HTMLScriptElementUtility.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.getAttribute('src');
if (src !== null) {
HTMLScriptElementUtility.loadExternalScript(this);
}
else if (!this.ownerDocument.defaultView.happyDOM.settings.disableJavaScriptEvaluation) {
const textContent = this.textContent;
const type = this.getAttribute('type');
if (textContent &&
(type === null ||
type === 'application/x-ecmascript' ||
type === 'application/x-javascript' ||
type.startsWith('text/javascript'))) {
HTMLScriptElementUtility.eval(this, textContent);
}
}
}
}
}
//# sourceMappingURL=HTMLScriptElement.js.map