🌐 AI搜索 & 代理 主页
Skip to content

Commit ac380e3

Browse files
committed
Event: Simplify 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`.
1 parent 12ff6a7 commit ac380e3

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

src/event.js

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import jQuery from "./core.js";
2-
import document from "./var/document.js";
32
import documentElement from "./var/documentElement.js";
43
import rnothtmlwhite from "./var/rnothtmlwhite.js";
54
import rcheckableType from "./var/rcheckableType.js";
@@ -22,16 +21,6 @@ function returnFalse() {
2221
return false;
2322
}
2423

25-
// Support: IE <=9 - 11+
26-
// focus() and blur() are asynchronous, except when they are no-op.
27-
// So expect focus to be synchronous when the element is already active,
28-
// and blur to be synchronous when the element is not already active.
29-
// (focus and blur are always synchronous in other supported browsers,
30-
// this just defines when we can count on it).
31-
function expectSync( elem, type ) {
32-
return ( elem === document.activeElement ) === ( type === "focus" );
33-
}
34-
3524
function on( elem, types, selector, data, fn, one ) {
3625
var origFn, type;
3726

@@ -460,7 +449,7 @@ jQuery.event = {
460449
el.click && nodeName( el, "input" ) ) {
461450

462451
// dataPriv.set( el, "click", ... )
463-
leverageNative( el, "click", returnTrue );
452+
leverageNative( el, "click", true );
464453
}
465454

466455
// Return false to allow normal processing in the caller
@@ -512,10 +501,10 @@ jQuery.event = {
512501
// synthetic events by interrupting progress until reinvoked in response to
513502
// *native* events that it fires directly, ensuring that state changes have
514503
// already occurred before other listeners are invoked.
515-
function leverageNative( el, type, expectSync ) {
504+
function leverageNative( el, type, isSetup ) {
516505

517-
// Missing expectSync indicates a trigger call, which must force setup through jQuery.event.add
518-
if ( !expectSync ) {
506+
// Missing `isSetup` indicates a trigger call, which must force setup through jQuery.event.add
507+
if ( !isSetup ) {
519508
if ( dataPriv.get( el, type ) === undefined ) {
520509
jQuery.event.add( el, type, returnTrue );
521510
}
@@ -527,15 +516,13 @@ function leverageNative( el, type, expectSync ) {
527516
jQuery.event.add( el, type, {
528517
namespace: false,
529518
handler: function( event ) {
530-
var notAsync, result,
519+
var result,
531520
saved = dataPriv.get( this, type );
532521

533522
if ( ( event.isTrigger & 1 ) && this[ type ] ) {
534523

535524
// Interrupt processing of the outer synthetic .trigger()ed event
536-
// Saved data should be false in such cases, but might be a leftover capture object
537-
// from an async native handler (gh-4350)
538-
if ( !saved.length ) {
525+
if ( !saved ) {
539526

540527
// Store arguments for use when handling the inner native event
541528
// There will always be at least one argument (an event object), so this array
@@ -544,28 +531,17 @@ function leverageNative( el, type, expectSync ) {
544531
dataPriv.set( this, type, saved );
545532

546533
// Trigger the native event and capture its result
547-
// Support: IE <=9 - 11+
548-
// focus() and blur() are asynchronous
549-
notAsync = expectSync( this, type );
550534
this[ type ]();
551535
result = dataPriv.get( this, type );
552-
if ( saved !== result || notAsync ) {
553-
dataPriv.set( this, type, false );
554-
} else {
555-
result = {};
556-
}
536+
dataPriv.set( this, type, false );
537+
557538
if ( saved !== result ) {
558539

559540
// Cancel the outer synthetic event
560541
event.stopImmediatePropagation();
561542
event.preventDefault();
562543

563-
// Support: Chrome 86+
564-
// In Chrome, if an element having a focusout handler is blurred by
565-
// clicking outside of it, it invokes the handler synchronously. If
566-
// that handler calls `.remove()` on the element, the data is cleared,
567-
// leaving `result` undefined. We need to guard against this.
568-
return result && result.value;
544+
return result;
569545
}
570546

571547
// If this is an inner synthetic event for an event with a bubbling surrogate
@@ -583,16 +559,14 @@ function leverageNative( el, type, expectSync ) {
583559
} else if ( saved.length ) {
584560

585561
// ...and capture the result
586-
dataPriv.set( this, type, {
587-
value: jQuery.event.trigger(
588-
589-
// Support: IE <=9 - 11+
590-
// Extend with the prototype to reset the above stopImmediatePropagation()
591-
jQuery.extend( saved[ 0 ], jQuery.Event.prototype ),
592-
saved.slice( 1 ),
593-
this
594-
)
595-
} );
562+
dataPriv.set( this, type, jQuery.event.trigger(
563+
564+
// Support: IE <=9 - 11+
565+
// Extend with the prototype to reset the above stopImmediatePropagation()
566+
jQuery.extend( saved[ 0 ], jQuery.Event.prototype ),
567+
saved.slice( 1 ),
568+
this
569+
) );
596570

597571
// Abort handling of the native event
598572
event.stopImmediatePropagation();
@@ -756,7 +730,7 @@ jQuery.each( { focus: "focusin", blur: "focusout" }, function( type, delegateTyp
756730
// Claim the first handler
757731
// dataPriv.set( this, "focus", ... )
758732
// dataPriv.set( this, "blur", ... )
759-
leverageNative( this, type, expectSync );
733+
leverageNative( this, type, true );
760734

761735
if ( isIE ) {
762736
this.addEventListener( delegateType, focusMappedHandler );

0 commit comments

Comments
 (0)