Created
April 17, 2013 19:05
-
-
Save shaneriley/5406862 to your computer and use it in GitHub Desktop.
DOMTokenList Prototype Extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage: `dom_element.classList.addAll(["foo", "bar"], "baz")` and `dom_element.classList.removeAll("foo", ["bar", "baz"])` | |
// See http://jsbin.com/obenin for demo | |
(function() { | |
var args; | |
var flattenArgs = function(collection) { | |
if (typeof collection === "string") { | |
args.push(collection); | |
return; | |
} | |
collection = Array.prototype.slice.call(collection); | |
collection.forEach(function(arg) { | |
if (typeof arg === "string") { | |
args.push(arg); | |
return; | |
} | |
if (typeof arg === "object" && arg.slice) { | |
flattenArgs(arg); | |
} | |
}); | |
}; | |
var changeAll = function(list, method) { | |
var t = this; | |
args = []; | |
flattenArgs(list); | |
args.forEach(function(val) { | |
t[method](val); | |
}); | |
}; | |
DOMTokenList.prototype.addAll = function() { | |
changeAll.call(this, arguments, "add"); | |
}; | |
DOMTokenList.prototype.removeAll = function() { | |
changeAll.call(this, arguments, "remove"); | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment