Skip to content
Browse files

Add goog.dom.classlist.removeAll() so that we can efficiently replace…

… calls to goog.dom.classes.remove(element, classNames...)

Older browsers that don't support classList would have worse performance in repeated calls to goog.dom.classlist.remove() than in goog.dom.classes.remove(...).

R=gboyer
DELTA=67  (67 added, 0 deleted, 0 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=5789


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2286 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
1 parent fe6d691 commit 88756c2aabde89638562045249a868a7cf620e0d jeffschiller@google.com committed Nov 8, 2012
Showing with 67 additions and 0 deletions.
  1. +27 −0 closure/goog/dom/classlist.js
  2. +40 −0 closure/goog/dom/classlist_test.js
View
27 closure/goog/dom/classlist.js
@@ -135,6 +135,33 @@ goog.dom.classlist.remove = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
/**
+ * 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 {goog.array.ArrayLike.<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 classNames contains invalid
+ * or empty class names.
+ */
+goog.dom.classlist.removeAll = goog.dom.classlist.NATIVE_DOM_TOKEN_LIST_ ?
+ function(element, classesToRemove) {
+ goog.array.forEach(classesToRemove,
+ goog.partial(goog.dom.classlist.remove, element));
+ } :
+ function(element, classesToRemove) {
+ // 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.
View
40 closure/goog/dom/classlist_test.js
@@ -93,6 +93,46 @@ function testRemoveCaseSensitive() {
assertEquals('A B C', el.className);
}
+function testRemoveAll() {
+ var elem = document.createElement('div');
+ elem.className = 'foo bar baz';
+
+ goog.dom.classlist.removeAll(elem, ['bar', 'foo']);
+ assertFalse(goog.dom.classlist.contains(elem, 'foo'));
+ assertFalse(goog.dom.classlist.contains(elem, 'bar'));
+ assertTrue(goog.dom.classlist.contains(elem, 'baz'));
+}
+
+function testRemoveAllOne() {
+ var elem = document.createElement('div');
+ elem.className = 'foo bar baz';
+
+ goog.dom.classlist.removeAll(elem, ['bar']);
+ assertFalse(goog.dom.classlist.contains(elem, 'bar'));
+ assertTrue(goog.dom.classlist.contains(elem, 'foo'));
+ assertTrue(goog.dom.classlist.contains(elem, 'baz'));
+}
+
+function testRemoveAllSomeNotPresent() {
+ var elem = document.createElement('div');
+ elem.className = 'foo bar baz';
+
+ goog.dom.classlist.removeAll(elem, ['a', 'bar']);
+ assertTrue(goog.dom.classlist.contains(elem, 'foo'));
+ assertFalse(goog.dom.classlist.contains(elem, 'bar'));
+ assertTrue(goog.dom.classlist.contains(elem, 'baz'));
+}
+
+function testRemoveAllCaseSensitive() {
+ var elem = document.createElement('div');
+ elem.className = 'foo bar baz';
+
+ goog.dom.classlist.removeAll(elem, ['BAR', 'foo']);
+ assertFalse(goog.dom.classlist.contains(elem, 'foo'));
+ assertTrue(goog.dom.classlist.contains(elem, 'bar'));
+ assertTrue(goog.dom.classlist.contains(elem, 'baz'));
+}
+
function testEnable() {
var el = goog.dom.getElement('p1');
classlist.set(el, 'SOMECLASS FIRST');

0 comments on commit 88756c2

Please sign in to comment.
Something went wrong with that request. Please try again.