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

Event: Make trigger(focus/blur/click) work with native handlers #5228

Merged
merged 1 commit into from Mar 27, 2023

Conversation

mgol
Copy link
Member

@mgol mgol commented Mar 25, 2023

Summary

In leverageNative, instead of calling event.stopImmediatePropagation() which would abort both native & jQuery handlers, set the wrapper's isImmediatePropagationStopped property to a function returning true. Since for each element + type pair jQuery attaches only one native handler, there is also only one wrapper jQuery event so this achieves the goal: on the target element jQuery handlers don't fire but native ones do.

Unfortunately, this workaround doesn't work for handlers on ancestors - since the native event is re-wrapped by a jQuery one on each level of the propagation, the only way to stop it for jQuery was to stop it for everyone via native stopPropagation(). This is not a problem for focus/blur which don't bubble, but it does also stop click on checkboxes and radios. We accept this limitation.

Fixes gh-5015

+7 bytes

All three added tests fail before the source changes in Chrome & IE 11 and pass with the source changes.

Checklist

  • New tests have been added to show the fix or feature works
  • Grunt build and unit tests pass locally with these changes
  • If needed, a docs issue/PR was created at https://github.com/jquery/api.jquery.com

@mgol mgol added Event Needs review Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Mar 25, 2023
@mgol mgol self-assigned this Mar 25, 2023
@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Mar 27, 2023
In `leverageNative`, instead of calling `event.stopImmediatePropagation()`
which would abort both native & jQuery handlers, set the wrapper's
`isImmediatePropagationStopped` property to a function returning `true`.
Since for each element + type pair jQuery attaches only one native handler,
there is also only one wrapper jQuery event so this achieves the goal:
on the target element jQuery handlers don't fire but native ones do.

Unfortunately, this workaround doesn't work for handlers on ancestors
- since the native event is re-wrapped by a jQuery one on each level of
the propagation, the only way to stop it for jQuery was to stop it for
everyone via native `stopPropagation()`. This is not a problem for
`focus`/`blur` which don't bubble, but it does also stop `click` on
checkboxes and radios. We accept this limitation.

Fixes jquerygh-5015
@mgol mgol added this to the 3.7.0 milestone Mar 27, 2023
@mgol mgol removed the Needs review label Mar 27, 2023
@mgol mgol merged commit 6ad3651 into jquery:main Mar 27, 2023
15 checks passed
@mgol mgol deleted the native-and-jquery-focus branch March 27, 2023 19:47
mgol added a commit that referenced this pull request Mar 27, 2023
In `leverageNative`, instead of calling `event.stopImmediatePropagation()`
which would abort both native & jQuery handlers, set the wrapper's
`isImmediatePropagationStopped` property to a function returning `true`.
Since for each element + type pair jQuery attaches only one native handler,
there is also only one wrapper jQuery event so this achieves the goal:
on the target element jQuery handlers don't fire but native ones do.

Unfortunately, this workaround doesn't work for handlers on ancestors
- since the native event is re-wrapped by a jQuery one on each level of
the propagation, the only way to stop it for jQuery was to stop it for
everyone via native `stopPropagation()`. This is not a problem for
`focus`/`blur` which don't bubble, but it does also stop `click` on
checkboxes and radios. We accept this limitation.

Fixes gh-5015
Closes gh-5228

(cherry picked from commit 6ad3651)
@mgol
Copy link
Member Author

mgol commented Mar 27, 2023

Landed on main in 6ad3651 & on 3.x-stable in 754108f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

jQuery 3.4.0+ silences native focus handlers attached after jQuery ones
2 participants