Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/dom/classlist.js
4116 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Utilities for detecting, adding and removing classes. Prefer
9
* this over goog.dom.classes for new code since it attempts to use classList
10
* (DOMTokenList: http://dom.spec.whatwg.org/#domtokenlist) which is faster
11
* and requires less code.
12
*
13
* Note: these utilities are meant to operate on HTMLElements and SVGElements
14
* and may have unexpected behavior on elements with differing interfaces.
15
*/
16
17
18
goog.provide('goog.dom.classlist');
19
20
goog.require('goog.array');
21
22
23
/**
24
* Override this define at build-time if you know your target supports it.
25
* @define {boolean} Whether to use the classList property (DOMTokenList).
26
*/
27
goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST =
28
goog.define('goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST', false);
29
30
31
/**
32
* A wrapper which ensures correct functionality when interacting with
33
* SVGElements
34
* @param {?Element} element DOM node to get the class name of.
35
* @return {string}
36
* @private
37
*/
38
goog.dom.classlist.getClassName_ = function(element) {
39
'use strict';
40
// If className is an instance of SVGAnimatedString use getAttribute
41
return typeof element.className == 'string' ?
42
element.className :
43
element.getAttribute && element.getAttribute('class') || '';
44
};
45
46
47
/**
48
* Gets an array-like object of class names on an element.
49
* @param {Element} element DOM node to get the classes of.
50
* @return {!IArrayLike<?>} Class names on `element`.
51
*/
52
goog.dom.classlist.get = function(element) {
53
'use strict';
54
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
55
return element.classList;
56
}
57
58
return goog.dom.classlist.getClassName_(element).match(/\S+/g) || [];
59
};
60
61
62
/**
63
* Sets the entire class name of an element.
64
* @param {Element} element DOM node to set class of.
65
* @param {string} className Class name(s) to apply to element.
66
*/
67
goog.dom.classlist.set = function(element, className) {
68
'use strict';
69
// If className is an instance of SVGAnimatedString use setAttribute
70
if ((typeof element.className) == 'string') {
71
element.className = className;
72
return;
73
} else if (element.setAttribute) {
74
element.setAttribute('class', className);
75
}
76
};
77
78
79
/**
80
* Returns true if an element has a class. This method may throw a DOM
81
* exception for an invalid or empty class name if DOMTokenList is used.
82
* @param {Element} element DOM node to test.
83
* @param {string} className Class name to test for.
84
* @return {boolean} Whether element has the class.
85
*/
86
goog.dom.classlist.contains = function(element, className) {
87
'use strict';
88
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
89
return element.classList.contains(className);
90
}
91
return goog.array.contains(goog.dom.classlist.get(element), className);
92
};
93
94
95
/**
96
* Adds a class to an element. Does not add multiples of class names. This
97
* method may throw a DOM exception for an invalid or empty class name if
98
* DOMTokenList is used.
99
* @param {Element} element DOM node to add class to.
100
* @param {string} className Class name to add.
101
*/
102
goog.dom.classlist.add = function(element, className) {
103
'use strict';
104
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
105
element.classList.add(className);
106
return;
107
}
108
109
if (!goog.dom.classlist.contains(element, className)) {
110
// Ensure we add a space if this is not the first class name added.
111
var oldClassName = goog.dom.classlist.getClassName_(element);
112
goog.dom.classlist.set(
113
element,
114
oldClassName +
115
(oldClassName.length > 0 ? (' ' + className) : className));
116
}
117
};
118
119
120
/**
121
* Convenience method to add a number of class names at once.
122
* @param {Element} element The element to which to add classes.
123
* @param {IArrayLike<string>} classesToAdd An array-like object
124
* containing a collection of class names to add to the element.
125
* This method may throw a DOM exception if classesToAdd contains invalid
126
* or empty class names.
127
*/
128
goog.dom.classlist.addAll = function(element, classesToAdd) {
129
'use strict';
130
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
131
Array.prototype.forEach.call(classesToAdd, function(className) {
132
'use strict';
133
goog.dom.classlist.add(element, className);
134
});
135
return;
136
}
137
138
var classMap = {};
139
140
// Get all current class names into a map.
141
Array.prototype.forEach.call(
142
goog.dom.classlist.get(element), function(className) {
143
'use strict';
144
classMap[className] = true;
145
});
146
147
// Add new class names to the map.
148
Array.prototype.forEach.call(classesToAdd, function(className) {
149
'use strict';
150
classMap[className] = true;
151
});
152
153
// Flatten the keys of the map into the className.
154
var newClassName = '';
155
for (var className in classMap) {
156
newClassName += newClassName.length > 0 ? (' ' + className) : className;
157
}
158
goog.dom.classlist.set(element, newClassName);
159
};
160
161
162
/**
163
* Removes a class from an element. This method may throw a DOM exception
164
* for an invalid or empty class name if DOMTokenList is used.
165
* @param {Element} element DOM node to remove class from.
166
* @param {string} className Class name to remove.
167
*/
168
goog.dom.classlist.remove = function(element, className) {
169
'use strict';
170
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
171
element.classList.remove(className);
172
return;
173
}
174
175
if (goog.dom.classlist.contains(element, className)) {
176
// Filter out the class name.
177
goog.dom.classlist.set(
178
element,
179
Array.prototype.filter
180
.call(
181
goog.dom.classlist.get(element),
182
function(c) {
183
'use strict';
184
return c != className;
185
})
186
.join(' '));
187
}
188
};
189
190
191
/**
192
* Removes a set of classes from an element. Prefer this call to
193
* repeatedly calling `goog.dom.classlist.remove` if you want to remove
194
* a large set of class names at once.
195
* @param {Element} element The element from which to remove classes.
196
* @param {IArrayLike<string>} classesToRemove An array-like object
197
* containing a collection of class names to remove from the element.
198
* This method may throw a DOM exception if classesToRemove contains invalid
199
* or empty class names.
200
*/
201
goog.dom.classlist.removeAll = function(element, classesToRemove) {
202
'use strict';
203
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
204
Array.prototype.forEach.call(classesToRemove, function(className) {
205
'use strict';
206
goog.dom.classlist.remove(element, className);
207
});
208
return;
209
}
210
211
// Filter out those classes in classesToRemove.
212
goog.dom.classlist.set(
213
element,
214
Array.prototype.filter
215
.call(
216
goog.dom.classlist.get(element),
217
function(className) {
218
'use strict';
219
// If this class is not one we are trying to remove,
220
// add it to the array of new class names.
221
return !goog.array.contains(classesToRemove, className);
222
})
223
.join(' '));
224
};
225
226
227
/**
228
* Adds or removes a class depending on the enabled argument. This method
229
* may throw a DOM exception for an invalid or empty class name if DOMTokenList
230
* is used.
231
* @param {Element} element DOM node to add or remove the class on.
232
* @param {string} className Class name to add or remove.
233
* @param {boolean} enabled Whether to add or remove the class (true adds,
234
* false removes).
235
*/
236
goog.dom.classlist.enable = function(element, className, enabled) {
237
'use strict';
238
if (enabled) {
239
goog.dom.classlist.add(element, className);
240
} else {
241
goog.dom.classlist.remove(element, className);
242
}
243
};
244
245
246
/**
247
* Adds or removes a set of classes depending on the enabled argument. This
248
* method may throw a DOM exception for an invalid or empty class name if
249
* DOMTokenList is used.
250
* @param {!Element} element DOM node to add or remove the class on.
251
* @param {?IArrayLike<string>} classesToEnable An array-like object
252
* containing a collection of class names to add or remove from the element.
253
* @param {boolean} enabled Whether to add or remove the classes (true adds,
254
* false removes).
255
*/
256
goog.dom.classlist.enableAll = function(element, classesToEnable, enabled) {
257
'use strict';
258
var f = enabled ? goog.dom.classlist.addAll : goog.dom.classlist.removeAll;
259
f(element, classesToEnable);
260
};
261
262
263
/**
264
* Switches a class on an element from one to another without disturbing other
265
* classes. If the fromClass isn't removed, the toClass won't be added. This
266
* method may throw a DOM exception if the class names are empty or invalid.
267
* @param {Element} element DOM node to swap classes on.
268
* @param {string} fromClass Class to remove.
269
* @param {string} toClass Class to add.
270
* @return {boolean} Whether classes were switched.
271
*/
272
goog.dom.classlist.swap = function(element, fromClass, toClass) {
273
'use strict';
274
if (goog.dom.classlist.contains(element, fromClass)) {
275
goog.dom.classlist.remove(element, fromClass);
276
goog.dom.classlist.add(element, toClass);
277
return true;
278
}
279
return false;
280
};
281
282
283
/**
284
* Removes a class if an element has it, and adds it the element doesn't have
285
* it. Won't affect other classes on the node. This method may throw a DOM
286
* exception if the class name is empty or invalid.
287
* @param {Element} element DOM node to toggle class on.
288
* @param {string} className Class to toggle.
289
* @return {boolean} True if class was added, false if it was removed
290
* (in other words, whether element has the class after this function has
291
* been called).
292
*/
293
goog.dom.classlist.toggle = function(element, className) {
294
'use strict';
295
var add = !goog.dom.classlist.contains(element, className);
296
goog.dom.classlist.enable(element, className, add);
297
return add;
298
};
299
300
301
/**
302
* Adds and removes a class of an element. Unlike
303
* {@link goog.dom.classlist.swap}, this method adds the classToAdd regardless
304
* of whether the classToRemove was present and had been removed. This method
305
* may throw a DOM exception if the class names are empty or invalid.
306
*
307
* @param {Element} element DOM node to swap classes on.
308
* @param {string} classToRemove Class to remove.
309
* @param {string} classToAdd Class to add.
310
*/
311
goog.dom.classlist.addRemove = function(element, classToRemove, classToAdd) {
312
'use strict';
313
goog.dom.classlist.remove(element, classToRemove);
314
goog.dom.classlist.add(element, classToAdd);
315
};
316
317