Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add goog.dom.classlist.addRemove and a couple minor improvements.

R=arv
DELTA=40  (37 added, 0 deleted, 3 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=5738


git-svn-id: http://closure-library.googlecode.com/svn/trunk@2265 0b95b8e8-c90f-11de-9d4f-f947ee5921c8
  • Loading branch information...
commit e718e8cd1cee95baaeae45f30c71d4a34c39ba8c 1 parent efc5519
jeffschiller@google.com authored
View
6 closure/goog/dom/classes.js
@@ -13,7 +13,11 @@
// limitations under the License.
/**
- * @fileoverview Utilities for adding, removing and setting classes.
+ * @fileoverview Utilities for adding, removing and setting classes. Prefer
+ * {@link goog.dom.classlist} over these utilities since goog.dom.classlist
+ * conforms closer to the semantics of Element.classList, is faster (uses
+ * native methods rather than parsing strings on every call) and compiles
+ * to smaller code as a result.
*
* Note: these utilities are meant to operate on HTMLElements and
* will not work on elements with differing interfaces (such as SVGElements).
View
20 closure/goog/dom/classlist.js
@@ -164,8 +164,8 @@ goog.dom.classlist.enable = function(element, className, enabled) {
*/
goog.dom.classlist.swap = function(element, fromClass, toClass) {
if (goog.dom.classlist.contains(element, fromClass)) {
- goog.dom.classlist.enable(element, fromClass, false);
- goog.dom.classlist.enable(element, toClass, true);
+ goog.dom.classlist.remove(element, fromClass);
+ goog.dom.classlist.add(element, toClass);
return true;
}
return false;
@@ -187,3 +187,19 @@ goog.dom.classlist.toggle = function(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);
+};
View
17 closure/goog/dom/classlist_test.js
@@ -165,3 +165,20 @@ function testToggle() {
assertTrue('Return value should have been true', ret);
}
+function testAddRemoveString() {
+ var el = document.createElement('div');
+ el.className = 'A';
+
+ classlist.addRemove(el, 'A', 'B');
+ assertEquals('B', el.className);
+
+ classlist.addRemove(el, 'Z', 'C');
+ assertEquals('B C', el.className);
+
+ classlist.addRemove(el, 'C', 'D');
+ assertEquals('B D', el.className);
+
+ classlist.addRemove(el, 'D', 'B');
+ assertEquals('B', el.className);
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.