Skip to content

Event: Only attach events to objects that accept data - for real #4558

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

Merged
merged 1 commit into from
Dec 9, 2019

Conversation

mgol
Copy link
Member

@mgol mgol commented Dec 3, 2019

Summary

There was a check in jQuery.event.add that was supposed to make it a noop
for objects that don't accept data like text or comment nodes. The problem was
the check was incorrect: it assumed dataPriv.get( elem ) returns a falsy
value for an elem that doesn't accept data but that's not the case - we get
an empty object then. The check was changed to use acceptData directly.

Fixes gh-4397

It's funny that this has been the behavior for at least the past few years so this code has been wrong for a long time. 😱

We'll also need to CP that to 3.x-stable

+2 bytes

Checklist

@mgol mgol added this to the 4.0.0 milestone Dec 3, 2019
@mgol mgol self-assigned this Dec 3, 2019
@mgol mgol modified the milestones: 4.0.0, 3.5.0 Dec 3, 2019
@dmethvin
Copy link
Member

dmethvin commented Dec 3, 2019

Wow, this does go back a ways!

QUnit.test( ".on('focus', fn) on a text node doesn't throw", function( assert ) {
assert.expect( 1 );

jQuery( "<div></div>text<span></span>" )
Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer something more obvious here such as jQuery( document.createTextNode("text") ), but this works as long as the behavior doesn't change (do we test that, though?).

Copy link
Member Author

Choose a reason for hiding this comment

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

@gibson042 Your suggestion looks good to me. What do you mean by "as long as the behavior doesn't change", though? What behavior?

There was a check in jQuery.event.add that was supposed to make it a noop
for objects that don't accept data like text or comment nodes. The problem was
the check was incorrect: it assumed `dataPriv.get( elem )` returns a falsy
value for an `elem` that doesn't accept data but that's not the case - we get
an empty object then. The check was changed to use `acceptData` directly.

Fixes jquerygh-4397
@mgol mgol merged commit d5c505e into jquery:master Dec 9, 2019
@mgol mgol deleted the add-data-fix branch December 9, 2019 18:50
@mgol mgol removed the Needs review label Dec 9, 2019
mgol added a commit that referenced this pull request Dec 9, 2019
There was a check in jQuery.event.add that was supposed to make it a noop
for objects that don't accept data like text or comment nodes. The problem was
the check was incorrect: it assumed `dataPriv.get( elem )` returns a falsy
value for an `elem` that doesn't accept data but that's not the case - we get
an empty object then. The check was changed to use `acceptData` directly.

(cherry picked from d5c505e)

Fixes gh-4397
Closes gh-4558
@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

Bug with .focus and text nodes in 3.4
3 participants