-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
HTMLImageElement.ts
319 lines (285 loc) · 5.57 KB
/
HTMLImageElement.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import HTMLElement from '../html-element/HTMLElement.js';
import * as PropertySymbol from '../../PropertySymbol.js';
/**
* HTML Image Element.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement.
*/
export default class HTMLImageElement extends HTMLElement {
public [PropertySymbol.tagName] = 'IMG';
public [PropertySymbol.complete] = false;
public [PropertySymbol.naturalHeight] = 0;
public [PropertySymbol.naturalWidth] = 0;
public [PropertySymbol.loading] = 'auto';
public [PropertySymbol.x] = 0;
public [PropertySymbol.y] = 0;
public cloneNode: (deep?: boolean) => HTMLImageElement;
/**
* Returns complete.
*
* @returns Complete.
*/
public get complete(): boolean {
return this[PropertySymbol.complete];
}
/**
* Returns natural height.
*
* @returns Natural height.
*/
public get naturalHeight(): number {
return this[PropertySymbol.naturalHeight];
}
/**
* Returns natural width.
*
* @returns Natural width.
*/
public get naturalWidth(): number {
return this[PropertySymbol.naturalWidth];
}
/**
* Returns loading.
*
* @returns Loading.
*/
public get loading(): string {
const loading = this.getAttribute('loading');
return loading === 'eager' || loading === 'lazy' ? loading : 'auto';
}
/**
* Sets loading.
*
* @param loading Loading.
*/
public set loading(loading: string) {
this.setAttribute('loading', loading);
}
/**
* Returns x.
*/
public get x(): number {
return this[PropertySymbol.x];
}
/**
* Returns y.
*/
public get y(): number {
return this[PropertySymbol.y];
}
/**
* Returns decoding.
*
* @returns Decoding.
*/
public get decoding(): string {
return this.getAttribute('decoding') || 'auto';
}
/**
* Sets decoding.
*
* @param decoding Decoding.
*/
public set decoding(decoding: string) {
this.setAttribute('decoding', decoding);
}
/**
* Returns cross origin.
*
* @returns Cross origin.
*/
public get crossOrigin(): string | null {
return this.getAttribute('crossOrigin');
}
/**
* Sets cross origin.
*
* @param crossOrigin Cross origin.
*/
public set crossOrigin(crossOrigin: string | null) {
if (crossOrigin === 'anonymous' || crossOrigin === 'use-credentials') {
this.setAttribute('crossOrigin', crossOrigin);
}
}
/**
* Returns alt.
*
* @returns Alt.
*/
public get alt(): string {
return this.getAttribute('alt') || '';
}
/**
* Sets alt.
*
* @param alt Alt.
*/
public set alt(alt: string) {
this.setAttribute('alt', alt);
}
/**
* Returns current src.
*
* @returns Current src.
*/
public get currentSrc(): string {
return this.src;
}
/**
* Returns width.
*
* @returns Width.
*/
public get width(): number {
const width = this.getAttribute('width');
return width !== null ? Number(width) : 0;
}
/**
* Sets width.
*
* @param width Width.
*/
public set width(width: number) {
this.setAttribute('width', String(width));
}
/**
* Returns height.
*
* @returns Height.
*/
public get height(): number {
const height = this.getAttribute('height');
return height !== null ? Number(height) : 0;
}
/**
* Sets height.
*
* @param height Height.
*/
public set height(height: number) {
this.setAttribute('height', String(height));
}
/**
* Returns is map.
*
* @returns Is map.
*/
public get isMap(): boolean {
return this.getAttribute('ismap') !== null;
}
/**
* Sets is map.
*
* @param ismap Is map.
*/
public set isMap(isMap: boolean) {
if (!isMap) {
this.removeAttribute('ismap');
} else {
this.setAttribute('ismap', '');
}
}
/**
* Returns referrer policy.
*
* @returns Referrer policy.
*/
public get referrerPolicy(): string {
return this.getAttribute('referrerpolicy') || '';
}
/**
* Sets referrer policy.
*
* @param referrerPolicy Referrer policy.
*/
public set referrerPolicy(referrerPolicy: string) {
this.setAttribute('referrerpolicy', referrerPolicy);
}
/**
* Returns sizes.
*
* @returns Sizes.
*/
public get sizes(): string {
return this.getAttribute('sizes') || '';
}
/**
* Sets sizes.
*
* @param sizes Sizes.
*/
public set sizes(sizes: string) {
this.setAttribute('sizes', sizes);
}
/**
* Returns source.
*
* @returns Source.
*/
public get src(): string {
if (!this.hasAttribute('src')) {
return '';
}
try {
return new URL(this.getAttribute('src'), this[PropertySymbol.ownerDocument].location.href)
.href;
} catch (e) {
return this.getAttribute('src');
}
}
/**
* Sets source.
*
* @param src Source.
*/
public set src(src: string) {
this.setAttribute('src', src);
}
/**
* Returns srcset.
*
* @returns Source.
*/
public get srcset(): string {
return this.getAttribute('srcset') || '';
}
/**
* Sets src set.
*
* @param srcset Src set.
*/
public set srcset(srcset: string) {
this.setAttribute('srcset', srcset);
}
/**
* Returns use map.
*
* @returns Use map.
*/
public get useMap(): string {
return this.getAttribute('usemap') || '';
}
/**
* Sets is map.
*
* @param useMap Is map.
*/
public set useMap(useMap: string) {
this.setAttribute('usemap', useMap);
}
/**
* The decode() method of the HTMLImageElement interface returns a Promise that resolves when the image is decoded and it is safe to append the image to the DOM.
*
* @returns Promise.
*/
public decode(): Promise<void> {
return Promise.resolve();
}
/**
* @override
*/
public override [PropertySymbol.cloneNode](deep = false): HTMLImageElement {
return <HTMLImageElement>super[PropertySymbol.cloneNode](deep);
}
}