-
Notifications
You must be signed in to change notification settings - Fork 9
Conversation
*/ | ||
hasClass : function (obj, klass) { | ||
obj = utility.object(obj); | ||
if (obj instanceof Array) return obj.each(function (i) { element.hide(i); }); | ||
|
||
if (!(obj instanceof Element)) throw Error(label.error.invalidArguments); | ||
if (!(obj instanceof Element) || klass.explode(" ").length > 1) throw Error(label.error.invalidArguments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added test to ensure single class is passed. Multiple classes gave false positive.
e.g.
$("#id").hasClass("class class2") => false
good intention, not a fan of how you wrote it. |
arg = arg.explode(); | ||
|
||
// If classList API is supported | ||
if (client.supports.classList && arg !== "*") arg.each(function (cls) { add ? obj.classList.add(cls) : obj.classList.remove(cls); }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fallback for wildcard since there is no support to remove all classes and another loop didn't seem to make sense... there is an issue with the wildcard (existing) where removeClass("*") gives invalid argument error.
String("*").isEmpty() => true
I refactored to utilize classList, and loaded the appropriate shim from bootstrap(). When moving to new APIs, it should only branch logic when you can't shim the functionality. |
abaaso should also not try to sniff features and track since shims can be loaded after the test. |
fair explanation - I was messing around with the classList API and notice some significant performance gains - thought it was worth starting a discussion. I should have known you would get it implemented in a few hours... |
Basic performance test (call method x times) showed a lot of performance gain for addClass() and hasClass() while removeClass() didn't see much if any while using the classList API.
Supported browsers are pretty good (http://caniuse.com/classlist) but the the fallback is still in place so no harm.
I also added a "supports" object inside "client" for the feature detection test (instead of performing it every call). It made sense to me to have it in the client but I'd like your opinion...