-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDOMTokenList.js
168 lines (150 loc) · 3.59 KB
/
DOMTokenList.js
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
'use strict';
/**
* DOMTokenList constructor
*
* @param {Element} element - DOM element
* @param {string} attribute - Attribute to create a token list for
* @constructor
*/
function DOMTokenList(element, attribute) {
this._getString = function () {
return element.getAttribute(attribute) || '';
};
this._setString = function (value) {
element.setAttribute(attribute, value);
};
fixIndex(this, getList(this));
}
DOMTokenList.prototype = {
/**
* Adds tokens to the token list
* @param {...string} tokens
*/
add: function add(tokens) {
var token;
var i = 0;
var len = arguments.length;
var list = getList(this);
var updated = false;
for (; i < len; i++) {
token = arguments[i];
validateToken(token);
if (list.indexOf(token) < 0) {
list.push(token);
updated = true;
}
}
if (updated) {
this._setString(list.join(' ').trim());
fixIndex(this, list);
}
},
/**
* @param {string} token
* @return {boolean}
*/
contains: function contains(token) {
validateToken(token);
return (getList(this)).indexOf(token) > -1;
},
/**
* Returns the token at a given index
* @param {number} index
* @return {string|null} - the token
*/
item: function item(index) {
return (getList(this))[index] || null;
},
/**
* @return {number} - length of the token list
*/
get length () {
return (getList(this)).length;
},
/**
* Removes tokens from the token list
* @param {...string} tokens
*/
remove: function remove(tokens) {
var index, token;
var i = 0;
var len = arguments.length;
var list = getList(this);
var updated = false;
for (; i < len; i++) {
token = arguments[i];
validateToken(token);
// remove multiple instances of the same class
while ((index = list.indexOf(token)) > -1) {
list.splice(index, 1);
updated = true;
}
}
if (updated) {
this._setString(list.join(' ').trim());
fixIndex(this, list);
}
},
/**
* Toggles a token's presence in the token list
* @param {string} token
* @param {boolean} force - true: always add; false: always remove
* @return {boolean} - true: the value was added to the token list
*/
toggle: function toggle(token, force) {
var hasToken = this.contains(token);
var method = hasToken ?
(force !== true && 'remove') :
(force !== false && 'add');
if (method) {
this[method](token);
}
return (typeof force == 'boolean' ? force : !hasToken);
},
/**
* @return {string} - value of the token list's associated attribute
*/
toString: function toString() {
return this._getString();
}
};
/**
* Ensure the token list is indexable
*
* @param {Object} instance
* @param {Array} list
*/
function fixIndex(instance, list) {
var len = list.length;
var i = 0;
for (; i < len; i++) { instance[i] = list[i]; }
delete instance[len];
}
/**
* Get the attribute's list of values
*
* @param {Object} instance
* @return {Array} - values
*/
function getList(instance) {
var str = instance._getString();
if (!str || str === '') {
return [];
} else {
return str.split(/\s+/);
}
}
/**
* @param {string} token
*/
function validateToken(token) {
if (token === '' || token === undefined) {
throw new Error(
'An invalid or illegal string was specified (DOM Exception 12)');
} else if (/\s+/.test(token)) {
throw new Error(
'InvalidCharacterError: String contains an invalid character ' +
'(DOM Exception 5)');
}
}
module.exports = DOMTokenList;