Skip to content

Conversation

@fsateler
Copy link
Contributor

@fsateler fsateler commented May 2, 2016

Summary

This ensures events with invalid selectors throw at attach time instead
of event time. This is done by instantiating a Sizzle for the selector.

jQuery.find() is used instead of jQuery.find.matchesSelector (as suggested in #3071 (comment)) , because matchesSelector requires a DOMElement, which won't be if the element is actually document.

Fixes: #3071

Checklist

Mark an [x] for completed items, if you're not sure leave them unchecked and we can assist.

Thanks! Bots and humans will be around shortly to check it out.

This ensures events with invalid selectors throw at attach time instead
of event time.

Fixes: jquery#3071

// If the selector is invalid, throw any exceptions at attach time
if ( selector ) {
jQuery.find( selector, elem );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may or may not require a context elem.ownerDocument. If it's just testing validity, perhaps not, but can anyone think of a use case where the document would affect validity?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None that I can think of. The only case I can think of that breaks is if a selector extension was added after the event was attached here, but before an event was fired. That's probably not common?

Copy link
Member

@dmethvin dmethvin May 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note that this is inside jQuery.event.add() and the API surface is jQuery.fn.on(). If someone does $("tr").on("mouseover" ...) on a table with 100 rows it will check the selector 100 times. The parsed selector is cached and the selection set is small obviously, but is there a downside to moving this up to function on()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only problem I see is that one would have to replicate the selector parameter parsing. Maybe a better approach would be to check if there is already an event handler with such a selector in the handlers list?

I may be wrong, but I think event handlers with selectors usually attach to a single object, as the whole point is to have to avoid listing all possible elements (which may be a lot, or not exist yet).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fsateler You are right, it won't be common for people to make a call to attach delegated events on a lot of elements at once. Also, this change does have the potential to throw errors where they did not before, in the case where the selector was invalid but no event ever occurred. We learned in gh-2824 that even obscure cases like that can cause trouble. So even if we moved this to on() it should be executed only if there is at least one element.

In short, I think you have this check in a good place.

@dmethvin
Copy link
Member

dmethvin commented May 6, 2016

@fsateler can you make that assert.throws tweak to the tests? I think this is good to land otherwise.

Anyone have thoughts on whether and how this should be documented? I am thinking it doesn't need docs, this behavior may have been what everyone expected anyway.

@fsateler
Copy link
Contributor Author

fsateler commented May 6, 2016

Yes, I've just been busy during this week with $work. I'll push the change during the weekend.

@fsateler
Copy link
Contributor Author

fsateler commented May 6, 2016

Updated for the throws change.

@gibson042 gibson042 closed this in 7fd36ea May 7, 2016
@gibson042
Copy link
Member

Thanks, @fsateler! As it turns out, matchesSelector was necessary after all.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Event handlers with invalid selectors should be caught at attach time

5 participants