Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing filter pseudo functions for use with elements directly #3046

Closed
hexalys opened this issue Apr 7, 2016 · 8 comments
Closed

Exposing filter pseudo functions for use with elements directly #3046

hexalys opened this issue Apr 7, 2016 · 8 comments

Comments

@hexalys
Copy link

hexalys commented Apr 7, 2016

Very often I am wanting to get access to common pseudo methods such as:
:focus, :selected, :checked or :empty etc. for a single target element in an event callback.

For that, I currently have two options:

  1. Either write my own code (duplicating jQuery's failsafe methods). Which seems redundant.
  2. Wrap the element into jQuery and use the $.fn.is(:peudo) selector.

Both are however wasteful of resources. Given that those methods (taking a single element argument), are already in the code base. They have a legit case to be used directly. But they are not exposed...

Could they be exposed to an $.is set of functions as well. It would allow simple efficient checks like $.is.focus(el) without having to go through creating a new object and runs regex exprs needlessly?

@hexalys hexalys changed the title Exposing element based pseudo filters for use with element Exposing filter pseudo functions for use with elements directly Apr 7, 2016
@dmethvin
Copy link
Member

dmethvin commented Apr 7, 2016

The pseudo selectors are in jQuery.expr.pseudos so they're accessible and you can add your own, we just haven't ever documented the specifics of which selectors might be there by default. In reality I doubt that $(elem).is(":even") is any kind of performance bottleneck.

@hexalys
Copy link
Author

hexalys commented Apr 8, 2016

@dmethvin Ah ok. I didn't realize they were accessible. Sorry for not catching that.

And sure. It's not that it's a performance bottlenecks per se. It just feels silly to rewrite the same code if already available. And in a vanilla js context, having to use the wrapper outside of jQuery's scope is unnecessary, and at least, feels out of context.

So it is safe to write a plugin/helper function using the $.expr.filters object?

@dmethvin
Copy link
Member

dmethvin commented Apr 8, 2016

For your own use? Probably so, but I'll let some others think about it. As a public plugin you'd need to think about things like whether people are using the alternate selector engine or whether some of the pseudos might be deprecated in the future.

@hexalys
Copy link
Author

hexalys commented Apr 8, 2016

Yeah, for my own use. Given the extensible API. It's manageable, even if removed.
What I am getting at, is a simple short syntactic sugar base along those lines:

$.is = $.has = function(elem, pseudo){
  return $.expr.filters[pseudo](elem);
}

Which gives: $.has(el, 'focus'); or $.is(this, 'selected'); ('this' being event.currentTarget)
Not sure if that appeals to others, but I find this more elegant along with the associated perf benefits.

Also, there are 3 filter objects, which are essentially the same:
:, pseudos and filters

Any one best to use, for future-proof compatibility?

@mgol
Copy link
Member

mgol commented Apr 8, 2016

@hexalys See #2073. pseudos is the recommended one.

@hexalys
Copy link
Author

hexalys commented Apr 9, 2016

Slightly as an aside to this, but on the same topic:

jQuery.attr being exposed as well. Why isn't $.attr(this, name) a recommended and/or documented method to simply read element attributes in a similar context?

@dmethvin
Copy link
Member

Getting a DOM element into a jQuery object is one of the fastest paths in the code, just a couple of ifs. IMO there's no reason to short circuit it further and clutter the docs to provide micro-optimizations.

Since jQuery.expr.pseudos is exposed I guess we can close this? I don't think there's anything to do on the code side at least.

@timmywil
Copy link
Member

Agreed, I don't see us adding this to the API. However, this might be a good candidate for a plugin.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

4 participants