Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Using classList API when available #20

Closed
wants to merge 2 commits into from

Conversation

fabien-d
Copy link

@fabien-d fabien-d commented Aug 9, 2012

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...

*/
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);
Copy link
Author

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

@avoidwork
Copy link
Owner

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); });
Copy link
Author

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

@avoidwork avoidwork closed this Aug 9, 2012
@avoidwork
Copy link
Owner

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.

@avoidwork
Copy link
Owner

abaaso should also not try to sniff features and track since shims can be loaded after the test.

@fabien-d
Copy link
Author

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...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants