-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
HTMLButtonElement.ts
86 lines (75 loc) · 1.57 KB
/
HTMLButtonElement.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
import HTMLElement from '../html-element/HTMLElement';
import IHTMLButtonElement from './IHTMLButtonElement';
const BUTTON_TYPES = ['submit', 'reset', 'button', 'menu'];
/**
We can improve performance a bit if we make the types as a constant.
* HTML Button Element.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLButtonElement.
*/
export default class HTMLButtonElement extends HTMLElement implements IHTMLButtonElement {
/**
* Returns value.
*
* @returns Value.
*/
public get value(): string {
return this.getAttributeNS(null, 'value');
}
/**
* Sets value.
*
* @param value Value.
*/
public set value(value: string) {
this.setAttributeNS(null, 'value', value);
}
/**
* Returns disabled.
*
* @returns Disabled.
*/
public get disabled(): boolean {
return this.getAttributeNS(null, 'disabled') !== null;
}
/**
* Sets disabled.
*
* @param disabled Disabled.
*/
public set disabled(disabled: boolean) {
if (!disabled) {
this.removeAttributeNS(null, 'disabled');
} else {
this.setAttributeNS(null, 'disabled', '');
}
}
/**
* Returns type
*
* @returns Type
*/
public get type(): string {
return this._sanitizeType(this.getAttributeNS(null, 'type'));
}
/**
* Sets type
*
* @param v Type
*/
public set type(v: string) {
this.setAttributeNS(null, 'type', this.sanitizeType(v));
}
/**
*
* @param type
*/
protected _sanitizeType(type: string): string {
type = (type && type.toLowerCase()) || 'submit';
if (! BUTTON_TYPES.includes(type)) {
type = 'submit';
}
return type;
}
}