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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Utilities for detecting, adding and removing classes. Prefer
* this over goog.dom.classes for new code since it attempts to use classList
* (DOMTokenList: http://dom.spec.whatwg.org/#domtokenlist) which is faster
* and requires less code.
*
* Note: these utilities are meant to operate on HTMLElements
* and may have unexpected behavior on elements with differing interfaces
* (such as SVGElements).
*/
goog.provide('goog.dom.classlist');
goog.require('goog.array');
/**
* Override this define at build-time if you know your target supports it.
* @define {boolean} Whether to use the classList property (DOMTokenList).
*/
goog.define('goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST', false);
/**
* Gets an array-like object of class names on an element.
* @param {Element} element DOM node to get the classes of.
* @return {!IArrayLike<?>} Class names on {@code element}.
*/
goog.dom.classlist.get = function(element) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
return element.classList;
}
var className = element.className;
// Some types of elements don't have a className in IE (e.g. iframes).
// Furthermore, in Firefox, className is not a string when the element is
// an SVG element.
return goog.isString(className) && className.match(/\S+/g) || [];
};
/**
* Sets the entire class name of an element.
* @param {Element} element DOM node to set class of.
* @param {string} className Class name(s) to apply to element.
*/
goog.dom.classlist.set = function(element, className) {
element.className = className;
};
/**
* Returns true if an element has a class. This method may throw a DOM
* exception for an invalid or empty class name if DOMTokenList is used.
* @param {Element} element DOM node to test.
* @param {string} className Class name to test for.
* @return {boolean} Whether element has the class.
*/
goog.dom.classlist.contains = function(element, className) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
return element.classList.contains(className);
}
return goog.array.contains(goog.dom.classlist.get(element), className);
};
/**
* Adds a class to an element. Does not add multiples of class names. This
* method may throw a DOM exception for an invalid or empty class name if
* DOMTokenList is used.
* @param {Element} element DOM node to add class to.
* @param {string} className Class name to add.
*/
goog.dom.classlist.add = function(element, className) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
element.classList.add(className);
return;
}
if (!goog.dom.classlist.contains(element, className)) {
// Ensure we add a space if this is not the first class name added.
element.className +=
element.className.length > 0 ? (' ' + className) : className;
}
};
/**
* Convenience method to add a number of class names at once.
* @param {Element} element The element to which to add classes.
* @param {IArrayLike<string>} classesToAdd An array-like object
* containing a collection of class names to add to the element.
* This method may throw a DOM exception if classesToAdd contains invalid
* or empty class names.
*/
goog.dom.classlist.addAll = function(element, classesToAdd) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
goog.array.forEach(classesToAdd, function(className) {
goog.dom.classlist.add(element, className);
});
return;
}
var classMap = {};
// Get all current class names into a map.
goog.array.forEach(goog.dom.classlist.get(element), function(className) {
classMap[className] = true;
});
// Add new class names to the map.
goog.array.forEach(
classesToAdd, function(className) { classMap[className] = true; });
// Flatten the keys of the map into the className.
element.className = '';
for (var className in classMap) {
element.className +=
element.className.length > 0 ? (' ' + className) : className;
}
};
/**
* Removes a class from an element. This method may throw a DOM exception
* for an invalid or empty class name if DOMTokenList is used.
* @param {Element} element DOM node to remove class from.
* @param {string} className Class name to remove.
*/
goog.dom.classlist.remove = function(element, className) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
element.classList.remove(className);
return;
}
if (goog.dom.classlist.contains(element, className)) {
// Filter out the class name.
element.className = goog.array
.filter(
goog.dom.classlist.get(element),
function(c) { return c != className; })
.join(' ');
}
};
/**
* Removes a set of classes from an element. Prefer this call to
* repeatedly calling {@code goog.dom.classlist.remove} if you want to remove
* a large set of class names at once.
* @param {Element} element The element from which to remove classes.
* @param {IArrayLike<string>} classesToRemove An array-like object
* containing a collection of class names to remove from the element.
* This method may throw a DOM exception if classesToRemove contains invalid
* or empty class names.
*/
goog.dom.classlist.removeAll = function(element, classesToRemove) {
if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
goog.array.forEach(classesToRemove, function(className) {
goog.dom.classlist.remove(element, className);
});
return;
}
// Filter out those classes in classesToRemove.
element.className =
goog.array
.filter(
goog.dom.classlist.get(element),
function(className) {
// If this class is not one we are trying to remove,
// add it to the array of new class names.
return !goog.array.contains(classesToRemove, className);
})
.join(' ');
};
/**
* Adds or removes a class depending on the enabled argument. This method
* may throw a DOM exception for an invalid or empty class name if DOMTokenList
* is used.
* @param {Element} element DOM node to add or remove the class on.
* @param {string} className Class name to add or remove.
* @param {boolean} enabled Whether to add or remove the class (true adds,
* false removes).
*/
goog.dom.classlist.enable = function(element, className, enabled) {
if (enabled) {
goog.dom.classlist.add(element, className);
} else {
goog.dom.classlist.remove(element, className);
}
};
/**
* Adds or removes a set of classes depending on the enabled argument. This
* method may throw a DOM exception for an invalid or empty class name if
* DOMTokenList is used.
* @param {!Element} element DOM node to add or remove the class on.
* @param {?IArrayLike<string>} classesToEnable An array-like object
* containing a collection of class names to add or remove from the element.
* @param {boolean} enabled Whether to add or remove the classes (true adds,
* false removes).
*/
goog.dom.classlist.enableAll = function(element, classesToEnable, enabled) {
var f = enabled ? goog.dom.classlist.addAll : goog.dom.classlist.removeAll;
f(element, classesToEnable);
};
/**
* Switches a class on an element from one to another without disturbing other
* classes. If the fromClass isn't removed, the toClass won't be added. This
* method may throw a DOM exception if the class names are empty or invalid.
* @param {Element} element DOM node to swap classes on.
* @param {string} fromClass Class to remove.
* @param {string} toClass Class to add.
* @return {boolean} Whether classes were switched.
*/
goog.dom.classlist.swap = function(element, fromClass, toClass) {
if (goog.dom.classlist.contains(element, fromClass)) {
goog.dom.classlist.remove(element, fromClass);
goog.dom.classlist.add(element, toClass);
return true;
}
return false;
};
/**
* Removes a class if an element has it, and adds it the element doesn't have
* it. Won't affect other classes on the node. This method may throw a DOM
* exception if the class name is empty or invalid.
* @param {Element} element DOM node to toggle class on.
* @param {string} className Class to toggle.
* @return {boolean} True if class was added, false if it was removed
* (in other words, whether element has the class after this function has
* been called).
*/
goog.dom.classlist.toggle = function(element, className) {
var add = !goog.dom.classlist.contains(element, className);
goog.dom.classlist.enable(element, className, add);
return add;
};
/**
* Adds and removes a class of an element. Unlike
* {@link goog.dom.classlist.swap}, this method adds the classToAdd regardless
* of whether the classToRemove was present and had been removed. This method
* may throw a DOM exception if the class names are empty or invalid.
*
* @param {Element} element DOM node to swap classes on.
* @param {string} classToRemove Class to remove.
* @param {string} classToAdd Class to add.
*/
goog.dom.classlist.addRemove = function(element, classToRemove, classToAdd) {
goog.dom.classlist.remove(element, classToRemove);
goog.dom.classlist.add(element, classToAdd);
};
|