/
DOMTokenList.ts
222 lines (194 loc) · 5.58 KB
/
DOMTokenList.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
import DOMException from '../exception/DOMException';
import Element from '../nodes/element/Element';
import IDOMTokenList from './IDOMTokenList';
/**
* DOM Token List.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList.
*/
export default class DOMTokenList implements IDOMTokenList {
public readonly length = 0;
private _ownerElement: Element;
private _attributeName: string;
/**
* Constructor.
*
* @param ownerElement Owner element.
* @param attributeName Attribute name.
*/
constructor(ownerElement: Element, attributeName) {
this._ownerElement = ownerElement;
this._attributeName = attributeName;
this._updateIndices();
}
/**
* Set value.
*
* @param value Value.
*/
public set value(value: string) {
this._ownerElement.setAttributeNS(null, this._attributeName, value);
}
/**
* Get value.
*/
public get value(): string {
return this._ownerElement.getAttributeNS(null, this._attributeName);
}
/**
* Get ClassName.
*
* @param index Index.
* */
public item(index: number | string): string {
index = typeof index === 'number' ? index : 0;
return index >= 0 && this[index] ? this[index] : null;
}
/**
* Replace Token.
*
* @param token Token.
* @param newToken NewToken.
*/
public replace(token: string, newToken: string): boolean {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
const list = attr ? attr.split(' ') : [];
const index = list.indexOf(token);
if (index === -1) {
return false;
}
list[index] = newToken;
this._ownerElement.setAttributeNS(null, this._attributeName, list.join(' '));
return true;
}
/**
* Supports.
*
* @param token Token.
*/
public supports(token: string): boolean {
// TODO: Only implemented for classList, which does not have any supported tokens
throw new DOMException(
`Failed to execute '${token}' on 'DOMTokenList': DOMTokenList has no supported tokens.`,
'TypeError'
);
}
/**
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
*/
public values(): IterableIterator<string> {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
return (attr ? attr.split(' ') : []).values();
}
/**
* Returns an iterator, allowing you to go through all key/value pairs contained in this object.
*/
public entries(): IterableIterator<[number, string]> {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
return (attr ? attr.split(' ') : []).entries();
}
/**
* Executes a provided callback function once for each DOMTokenList element.
*
* @param callback
* @param thisArg
*/
public forEach(callback: (currentValue, currentIndex, listObj) => void, thisArg?: this): void {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
return (attr ? attr.split(' ') : []).forEach(callback, thisArg);
}
/**
* Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
*
*/
public keys(): IterableIterator<number> {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
return (attr ? attr.split(' ') : []).keys();
}
/**
* Adds tokens.
*
* @param tokens Tokens.
*/
public add(...tokens: string[]): void {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
const list = attr ? attr.split(' ') : [];
for (const token of tokens) {
const index = list.indexOf(token);
if (index === -1) {
list.push(token);
} else {
list[index] = token;
}
}
this._ownerElement.setAttributeNS(null, this._attributeName, list.join(' '));
}
/**
* Removes tokens.
*
* @param tokens Tokens.
*/
public remove(...tokens: string[]): void {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
const list = attr ? attr.split(' ') : [];
for (const token of tokens) {
const index = list.indexOf(token);
if (index !== -1) {
list.splice(index, 1);
}
}
this._ownerElement.setAttributeNS(null, this._attributeName, list.join(' '));
}
/**
* Check if the list contains a class.
*
* @param className Class name.
* @returns TRUE if it contains.
*/
public contains(className: string): boolean {
const attr = this._ownerElement.getAttributeNS(null, this._attributeName);
return (attr ? attr.split(' ') : []).includes(className);
}
/**
* Toggle a class name.
*
* @param token A string representing the class name you want to toggle.
* @param [force] If included, turns the toggle into a one way-only operation. If set to `false`, then class name will only be removed, but not added. If set to `true`, then class name will only be added, but not removed.
* @returns A boolean value, `true` or `false`, indicating whether class name is in the list after the call or not.
*/
public toggle(token: string, force?: boolean): boolean {
let shouldAdd: boolean;
if (force !== undefined) {
shouldAdd = force;
} else {
shouldAdd = !this.contains(token);
}
if (shouldAdd) {
this.add(token);
return true;
}
this.remove(token);
return false;
}
/**
* Updates indices.
*/
public _updateIndices(): void {
const attr = this._ownerElement.getAttribute('class');
const list = attr ? Array.from(new Set(attr.split(' '))) : [];
for (let i = list.length - 1, max = this.length; i < max; i++) {
delete this[i];
}
for (let i = 0, max = list.length; i < max; i++) {
this[i] = list[i];
}
(<number>this.length) = list.length;
}
/**
* Returns DOMTokenList value.
*/
public toString(): string {
return this.value || '';
}
}