8000 Event: Simulate focus/blur in IE via focusin/focusout by mgol · Pull Request #5223 · jquery/jquery · GitHub
[go: up one dir, main page]

Skip to content

Event: Simulate focus/blur in IE via focusin/focusout #5223

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 5 commits into from
Mar 27, 2023

Conversation

mgol
Copy link
Member
@mgol mgol commented Mar 13, 2023

Summary

In IE (all versions), focus & blur handlers are fired asynchronously
but focusin & focusout are run synchronously. In other browsers, all
those handlers are fired synchronously.

Simulate focus via focusin & blur via focusout in IE to
avoid these issues.

Fixes gh-4856
Fixes gh-4859
Fixes gh-4950

+30 bytes

3.x-stable version of this PR: #5224

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 13, 2023
@mgol mgol self-assigned this Mar 13, 2023
Copy link
Member
@timmywil timmywil left a comment

Choose a reason for hiding this comment

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

Looks good for 4.0. We will not be guaranteeing order anymore, but #4300 points out that everyone has opinions on what the order should be and modern browsers don't even follow the spec.

@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Mar 13, 2023
@mgol mgol requested review from dmethvin and gibson042 March 13, 2023 22:58
@mgol mgol force-pushed the ie-focus-simulation branch from b6be579 to 8cc5ff1 Compare March 13, 2023 23:56
@mgol
Copy link
Member Author
8000
mgol commented Mar 13, 2023

The 3.x-stable version of the PR: #5224.

@mgol
Copy link
Member Author
mgol commented Mar 14, 2023

@timmywil I added a few commits on top of the original PR so I'm re-requesting a review from you.

@mgol mgol force-pushed the ie-focus-simulation branch 4 times, most recently from 7d67295 to 79daba0 Compare March 15, 2023 11:22
this
)
} );
dataPriv.set( this, type, jQuery.event.trigger(
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 One thing to note - while #4350 which added this wrapping was mainly aimed at IE with its async focus handlers, there has been a report about this also affecting Chrome somehow:
#4350 (comment)

I'm not sure if we can do anything about it due to the lack of data, though.

@mgol
Copy link
Member Author
mgol commented Mar 17, 2023

It looks like this also fixes #4950; I added a test for this in a separate commit & verified it fails on main.

Copy link
Member
@dmethvin dmethvin left a comment

Choose a reason for hiding this comment

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

👏 👏 👏

@mgol
Copy link
Member Author
mgol commented Mar 17, 2023

This also seems to fix one heavily upvoted select2 issue: select2/select2#5993.

In IE (all versions), `focus` & `blur` handlers are fired asynchronously
but `focusin` & `focusout` are run synchronously. In other browsers, all
those handlers are fired synchronously.

Simulate `focus` via `focusin` & `blur` via `focusout` in IE to
avoid these issues.

Fixes jquerygh-4856
Fixes jquerygh-4859
Fixes jquerygh-4950
mgol and others added 4 commits March 21, 2023 00:51
Tests added:
* sequences triggering focus and/or blur (authored by Richard Gibson)
* focus does not bubble
* some test stability fixes

Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
With IE now using `focusin` to simulate `focus` and `focusout`
to simulate `blur`, we don't have to deal with async events
in `leverageNative`.
Now that jQuery focus/blur events in IE are not async, we can
simplify some tests.
Focusing an input with `display: none` no longer prevents it from
being focused when it gets shown later.
@mgol mgol removed the Needs review label Mar 27, 2023
@mgol mgol merged commit ce60d31 into jquery:main Mar 27, 2023
@mgol mgol deleted the ie-focus-simulation branch March 27, 2023 19:22
mgol added a commit that referenced this pull request Mar 27, 2023
In IE (all versions), `focus` & `blur` handlers are fired asynchronously
but `focusin` & `focusout` are run synchronously. In other browsers, all
those handlers are fired synchronously. Asynchronous behavior of these
handlers in IE caused issues for IE (gh-4856, gh-4859).

We now simulate `focus` via `focusin` & `blur` via `focusout` in IE to avoid
these issues. This also let us simplify some tests.

This commit also simplifies `leverageNative` - with IE now using `focusin`
to simulate `focus` and `focusout` to simulate `blur`, we don't have to deal
with async events in `leverageNative`. This also fixes broken `focus` triggers
after first triggering it on a hidden element - previously, `leverageNative`
assumed that the native `focus` handler not firing after calling the native 
`focus` method meant it would be handled later, asynchronously, which
was not the case (gh-4950).

To preserve relative `focusin`/`focus` & `focusout`/`blur` event order
guaranteed on the 3.x branch, attach a single handler for both events in IE.

A side effect of this is that to reduce size the `event/focusin` module
no longer exists and it's impossible to disable the `focusin` patch
in modern browsers via the jQuery custom build system.

Fixes gh-4856
Fixes gh-4859
Fixes gh-4950
Ref gh-5223
Closes gh-5224

Co-authored-by: Richard Gibson <richard.gibson@gmail.com>
@mgol mgol added this to the 4.0.0 milestone Mar 27, 2023
mgol added a commit to mgol/jquery that referenced this pull request Mar 30, 2023
Previously, when `leverageNative` handled async events, there was
a case where an empty placeholder object was set as a result.
Covering both such an object and `false` required a `length` check.
However, this is not necessary since jquerygh-5223 and the check was
already simplified in other places; this one was missed.

Ref jquerygh-5223
mgol added a commit that referenced this pull request Apr 3, 2023
Previously, when `leverageNative` handled async events, there was
a case where an empty placeholder object was set as a result.
Covering both such an object and `false` required a `length` check.
However, this is not necessary since gh-5223 and the check was
already simplified in other places; this one was missed.

Closes gh-5236
Ref gh-5223
mgol added a commit that referenced this pull request Apr 3, 2023
Previously, when `leverageNative` handled async events, there was
a case where an empty placeholder object was set as a result.
Covering both such an object and `false` required a `length` check.
However, this is not necessary since gh-5223 and the check was
already simplified in other places; this one was missed.

Closes gh-5236
Ref gh-5223

(cherry picked from commit dfe212d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 participants
0