classlist.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Utilities for detecting, adding and removing classes. Prefer
  16. * this over goog.dom.classes for new code since it attempts to use classList
  17. * (DOMTokenList: http://dom.spec.whatwg.org/#domtokenlist) which is faster
  18. * and requires less code.
  19. *
  20. * Note: these utilities are meant to operate on HTMLElements
  21. * and may have unexpected behavior on elements with differing interfaces
  22. * (such as SVGElements).
  23. */
  24. goog.provide('goog.dom.classlist');
  25. goog.require('goog.array');
  26. /**
  27. * Override this define at build-time if you know your target supports it.
  28. * @define {boolean} Whether to use the classList property (DOMTokenList).
  29. */
  30. goog.define('goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST', false);
  31. /**
  32. * Gets an array-like object of class names on an element.
  33. * @param {Element} element DOM node to get the classes of.
  34. * @return {!IArrayLike<?>} Class names on {@code element}.
  35. */
  36. goog.dom.classlist.get = function(element) {
  37. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  38. return element.classList;
  39. }
  40. var className = element.className;
  41. // Some types of elements don't have a className in IE (e.g. iframes).
  42. // Furthermore, in Firefox, className is not a string when the element is
  43. // an SVG element.
  44. return goog.isString(className) && className.match(/\S+/g) || [];
  45. };
  46. /**
  47. * Sets the entire class name of an element.
  48. * @param {Element} element DOM node to set class of.
  49. * @param {string} className Class name(s) to apply to element.
  50. */
  51. goog.dom.classlist.set = function(element, className) {
  52. element.className = className;
  53. };
  54. /**
  55. * Returns true if an element has a class. This method may throw a DOM
  56. * exception for an invalid or empty class name if DOMTokenList is used.
  57. * @param {Element} element DOM node to test.
  58. * @param {string} className Class name to test for.
  59. * @return {boolean} Whether element has the class.
  60. */
  61. goog.dom.classlist.contains = function(element, className) {
  62. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  63. return element.classList.contains(className);
  64. }
  65. return goog.array.contains(goog.dom.classlist.get(element), className);
  66. };
  67. /**
  68. * Adds a class to an element. Does not add multiples of class names. This
  69. * method may throw a DOM exception for an invalid or empty class name if
  70. * DOMTokenList is used.
  71. * @param {Element} element DOM node to add class to.
  72. * @param {string} className Class name to add.
  73. */
  74. goog.dom.classlist.add = function(element, className) {
  75. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  76. element.classList.add(className);
  77. return;
  78. }
  79. if (!goog.dom.classlist.contains(element, className)) {
  80. // Ensure we add a space if this is not the first class name added.
  81. element.className +=
  82. element.className.length > 0 ? (' ' + className) : className;
  83. }
  84. };
  85. /**
  86. * Convenience method to add a number of class names at once.
  87. * @param {Element} element The element to which to add classes.
  88. * @param {IArrayLike<string>} classesToAdd An array-like object
  89. * containing a collection of class names to add to the element.
  90. * This method may throw a DOM exception if classesToAdd contains invalid
  91. * or empty class names.
  92. */
  93. goog.dom.classlist.addAll = function(element, classesToAdd) {
  94. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  95. goog.array.forEach(classesToAdd, function(className) {
  96. goog.dom.classlist.add(element, className);
  97. });
  98. return;
  99. }
  100. var classMap = {};
  101. // Get all current class names into a map.
  102. goog.array.forEach(goog.dom.classlist.get(element), function(className) {
  103. classMap[className] = true;
  104. });
  105. // Add new class names to the map.
  106. goog.array.forEach(
  107. classesToAdd, function(className) { classMap[className] = true; });
  108. // Flatten the keys of the map into the className.
  109. element.className = '';
  110. for (var className in classMap) {
  111. element.className +=
  112. element.className.length > 0 ? (' ' + className) : className;
  113. }
  114. };
  115. /**
  116. * Removes a class from an element. This method may throw a DOM exception
  117. * for an invalid or empty class name if DOMTokenList is used.
  118. * @param {Element} element DOM node to remove class from.
  119. * @param {string} className Class name to remove.
  120. */
  121. goog.dom.classlist.remove = function(element, className) {
  122. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  123. element.classList.remove(className);
  124. return;
  125. }
  126. if (goog.dom.classlist.contains(element, className)) {
  127. // Filter out the class name.
  128. element.className = goog.array
  129. .filter(
  130. goog.dom.classlist.get(element),
  131. function(c) { return c != className; })
  132. .join(' ');
  133. }
  134. };
  135. /**
  136. * Removes a set of classes from an element. Prefer this call to
  137. * repeatedly calling {@code goog.dom.classlist.remove} if you want to remove
  138. * a large set of class names at once.
  139. * @param {Element} element The element from which to remove classes.
  140. * @param {IArrayLike<string>} classesToRemove An array-like object
  141. * containing a collection of class names to remove from the element.
  142. * This method may throw a DOM exception if classesToRemove contains invalid
  143. * or empty class names.
  144. */
  145. goog.dom.classlist.removeAll = function(element, classesToRemove) {
  146. if (goog.dom.classlist.ALWAYS_USE_DOM_TOKEN_LIST || element.classList) {
  147. goog.array.forEach(classesToRemove, function(className) {
  148. goog.dom.classlist.remove(element, className);
  149. });
  150. return;
  151. }
  152. // Filter out those classes in classesToRemove.
  153. element.className =
  154. goog.array
  155. .filter(
  156. goog.dom.classlist.get(element),
  157. function(className) {
  158. // If this class is not one we are trying to remove,
  159. // add it to the array of new class names.
  160. return !goog.array.contains(classesToRemove, className);
  161. })
  162. .join(' ');
  163. };
  164. /**
  165. * Adds or removes a class depending on the enabled argument. This method
  166. * may throw a DOM exception for an invalid or empty class name if DOMTokenList
  167. * is used.
  168. * @param {Element} element DOM node to add or remove the class on.
  169. * @param {string} className Class name to add or remove.
  170. * @param {boolean} enabled Whether to add or remove the class (true adds,
  171. * false removes).
  172. */
  173. goog.dom.classlist.enable = function(element, className, enabled) {
  174. if (enabled) {
  175. goog.dom.classlist.add(element, className);
  176. } else {
  177. goog.dom.classlist.remove(element, className);
  178. }
  179. };
  180. /**
  181. * Adds or removes a set of classes depending on the enabled argument. This
  182. * method may throw a DOM exception for an invalid or empty class name if
  183. * DOMTokenList is used.
  184. * @param {!Element} element DOM node to add or remove the class on.
  185. * @param {?IArrayLike<string>} classesToEnable An array-like object
  186. * containing a collection of class names to add or remove from the element.
  187. * @param {boolean} enabled Whether to add or remove the classes (true adds,
  188. * false removes).
  189. */
  190. goog.dom.classlist.enableAll = function(element, classesToEnable, enabled) {
  191. var f = enabled ? goog.dom.classlist.addAll : goog.dom.classlist.removeAll;
  192. f(element, classesToEnable);
  193. };
  194. /**
  195. * Switches a class on an element from one to another without disturbing other
  196. * classes. If the fromClass isn't removed, the toClass won't be added. This
  197. * method may throw a DOM exception if the class names are empty or invalid.
  198. * @param {Element} element DOM node to swap classes on.
  199. * @param {string} fromClass Class to remove.
  200. * @param {string} toClass Class to add.
  201. * @return {boolean} Whether classes were switched.
  202. */
  203. goog.dom.classlist.swap = function(element, fromClass, toClass) {
  204. if (goog.dom.classlist.contains(element, fromClass)) {
  205. goog.dom.classlist.remove(element, fromClass);
  206. goog.dom.classlist.add(element, toClass);
  207. return true;
  208. }
  209. return false;
  210. };
  211. /**
  212. * Removes a class if an element has it, and adds it the element doesn't have
  213. * it. Won't affect other classes on the node. This method may throw a DOM
  214. * exception if the class name is empty or invalid.
  215. * @param {Element} element DOM node to toggle class on.
  216. * @param {string} className Class to toggle.
  217. * @return {boolean} True if class was added, false if it was removed
  218. * (in other words, whether element has the class after this function has
  219. * been called).
  220. */
  221. goog.dom.classlist.toggle = function(element, className) {
  222. var add = !goog.dom.classlist.contains(element, className);
  223. goog.dom.classlist.enable(element, className, add);
  224. return add;
  225. };
  226. /**
  227. * Adds and removes a class of an element. Unlike
  228. * {@link goog.dom.classlist.swap}, this method adds the classToAdd regardless
  229. * of whether the classToRemove was present and had been removed. This method
  230. * may throw a DOM exception if the class names are empty or invalid.
  231. *
  232. * @param {Element} element DOM node to swap classes on.
  233. * @param {string} classToRemove Class to remove.
  234. * @param {string} classToAdd Class to add.
  235. */
  236. goog.dom.classlist.addRemove = function(element, classToRemove, classToAdd) {
  237. goog.dom.classlist.remove(element, classToRemove);
  238. goog.dom.classlist.add(element, classToAdd);
  239. };