1 | const afterChanges = dtl => {
|
2 | const el = dtl._ownerElement;
|
3 | const attr = el.getAttributeNode('class');
|
4 | if (attr) {
|
5 | if (attr.value !== dtl.value) {
|
6 | attr.value = dtl.value;
|
7 | }
|
8 | } else if (dtl.value) {
|
9 | el.setAttribute('class', dtl.value);
|
10 | }
|
11 | };
|
12 |
|
13 |
|
14 | module.exports = class DOMTokenList extends Array {
|
15 |
|
16 | constructor(ownerElement) {
|
17 | super();
|
18 | this._ownerElement = ownerElement;
|
19 | }
|
20 |
|
21 | item(i) {
|
22 | return this[i];
|
23 | }
|
24 |
|
25 | contains(token) {
|
26 | return this.includes(token);
|
27 | }
|
28 |
|
29 | add(...tokens) {
|
30 | this.splice(0, this.length, ...new Set(this.concat(tokens)));
|
31 | afterChanges(this);
|
32 | }
|
33 |
|
34 | remove(...tokens) {
|
35 | this.push(...this.splice(0, this.length)
|
36 | .filter(token => !tokens.includes(token)));
|
37 | afterChanges(this);
|
38 | }
|
39 |
|
40 | replace(token, newToken) {
|
41 | const i = this.indexOf(token);
|
42 | if (i < 0) this.add(newToken);
|
43 | else this[i] = newToken;
|
44 | afterChanges(this);
|
45 | }
|
46 |
|
47 | toggle(token, force) {
|
48 | let result = false;
|
49 | if (this.contains(token)) {
|
50 | if (force) result = true;
|
51 | else this.remove(token);
|
52 | } else {
|
53 | if (arguments.length < 2 || force) {
|
54 | result = true;
|
55 | this.add(token);
|
56 | }
|
57 | }
|
58 | return result;
|
59 | }
|
60 |
|
61 | get value() {
|
62 | return this.join(' ');
|
63 | }
|
64 |
|
65 | set value(className) {
|
66 | this.splice(0, this.length);
|
67 | this.add(...String(className || '').trim().split(/\s+/));
|
68 | afterChanges(this);
|
69 | }
|
70 |
|
71 | };
|