-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
HTMLScriptElement.ts
211 lines (188 loc) · 4.08 KB
/
HTMLScriptElement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import Attr from '../../attribute/Attr';
import HTMLElement from '../html-element/HTMLElement';
import IHTMLScriptElement from './IHTMLScriptElement';
import ScriptUtility from './ScriptUtility';
import Event from '../../event/Event';
import ErrorEvent from '../../event/events/ErrorEvent';
import INode from '../../nodes/node/INode';
/**
* HTML Script Element.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement.
*/
export default class HTMLScriptElement extends HTMLElement implements IHTMLScriptElement {
public onerror: (event: ErrorEvent) => void = null;
public onload: (event: Event) => void = null;
public _evaluateScript = true;
/**
* Returns type.
*
* @returns Type.
*/
public get type(): string {
return this.getAttributeNS(null, 'type') || '';
}
/**
* Sets type.
*
* @param type Type.
*/
public set type(type: string) {
this.setAttributeNS(null, 'type', type);
}
/**
* Returns source.
*
* @returns Source.
*/
public get src(): string {
return this.getAttributeNS(null, 'src') || '';
}
/**
* Sets source.
*
* @param source Source.
*/
public set src(src: string) {
this.setAttributeNS(null, 'src', src);
}
/**
* Returns charset.
*
* @returns Charset.
*/
public get charset(): string {
return this.getAttributeNS(null, 'charset') || '';
}
/**
* Sets charset.
*
* @param charset Charset.
*/
public set charset(charset: string) {
this.setAttributeNS(null, 'charset', charset);
}
/**
* Returns lang.
*
* @returns Lang.
*/
public get lang(): string {
return this.getAttributeNS(null, 'lang') || '';
}
/**
* Sets lang.
*
* @param lang Lang.
*/
public set lang(lang: string) {
this.setAttributeNS(null, 'lang', lang);
}
/**
* Returns async.
*
* @returns Async.
*/
public get async(): boolean {
return this.getAttributeNS(null, 'async') !== null;
}
/**
* Sets async.
*
* @param async Async.
*/
public set async(async: boolean) {
if (!async) {
this.removeAttributeNS(null, 'async');
} else {
this.setAttributeNS(null, 'async', '');
}
}
/**
* Returns defer.
*
* @returns Defer.
*/
public get defer(): boolean {
return this.getAttributeNS(null, 'defer') !== null;
}
/**
* Sets defer.
*
* @param defer Defer.
*/
public set defer(defer: boolean) {
if (!defer) {
this.removeAttributeNS(null, 'defer');
} else {
this.setAttributeNS(null, 'defer', '');
}
}
/**
* Returns text.
*
* @returns Text.
*/
public get text(): string {
return this.textContent;
}
/**
* Sets text.
*
* @param text Text.
*/
public set text(text: string) {
this.textContent = text;
}
/**
* The setAttributeNode() method adds a new Attr node to the specified element.
*
* @override
* @param attribute Attribute.
* @returns Replaced attribute.
*/
public setAttributeNode(attribute: Attr): Attr {
const replacedAttribute = super.setAttributeNode(attribute);
if (attribute.name === 'src' && attribute.value !== null && this.isConnected) {
ScriptUtility.loadExternalScript(this);
}
return replacedAttribute;
}
/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): IHTMLScriptElement {
return <IHTMLScriptElement>super.cloneNode(deep);
}
/**
* @override
*/
public _connectToNode(parentNode: INode = null): void {
const isConnected = this.isConnected;
const isParentConnected = parentNode ? parentNode.isConnected : false;
super._connectToNode(parentNode);
if (isConnected !== isParentConnected && this._evaluateScript) {
const src = this.getAttributeNS(null, 'src');
if (src !== null) {
ScriptUtility.loadExternalScript(this);
} else {
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);
}
}
}
}
}