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

Commit 94efb79

Browse files
committed
Events: don't execute native stop(Immediate)Propagation from simulation
In Firefox, called `stop(Immediate)Propagation` methods, in capturing phase prevents receiving focus Fixes gh-3111
1 parent 69db408 commit 94efb79

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

src/event.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,14 @@ jQuery.Event.prototype = {
558558
isDefaultPrevented: returnFalse,
559559
isPropagationStopped: returnFalse,
560560
isImmediatePropagationStopped: returnFalse,
561+
isSimulated: false,
561562

562563
preventDefault: function() {
563564
var e = this.originalEvent;
564565

565566
this.isDefaultPrevented = returnTrue;
566567

567-
if ( e ) {
568+
if ( e && !this.isSimulated ) {
568569
e.preventDefault();
569570
}
570571
},
@@ -573,7 +574,7 @@ jQuery.Event.prototype = {
573574

574575
this.isPropagationStopped = returnTrue;
575576

576-
if ( e ) {
577+
if ( e && !this.isSimulated ) {
577578
e.stopPropagation();
578579
}
579580
},
@@ -582,7 +583,7 @@ jQuery.Event.prototype = {
582583

583584
this.isImmediatePropagationStopped = returnTrue;
584585

585-
if ( e ) {
586+
if ( e && !this.isSimulated ) {
586587
e.stopImmediatePropagation();
587588
}
588589

src/event/trigger.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,34 +150,18 @@ jQuery.extend( jQuery.event, {
150150
},
151151

152152
// Piggyback on a donor event to simulate a different one
153+
// Used only for `focus(in | out)` events
153154
simulate: function( type, elem, event ) {
154155
var e = jQuery.extend(
155156
new jQuery.Event(),
156157
event,
157158
{
158159
type: type,
159160
isSimulated: true
160-
161-
// Previously, `originalEvent: {}` was set here, so stopPropagation call
162-
// would not be triggered on donor event, since in our own
163-
// jQuery.event.stopPropagation function we had a check for existence of
164-
// originalEvent.stopPropagation method, so, consequently it would be a noop.
165-
//
166-
// But now, this "simulate" function is used only for events
167-
// for which stopPropagation() is noop, so there is no need for that anymore.
168-
//
169-
// For the compat branch though, guard for "click" and "submit"
170-
// events is still used, but was moved to jQuery.event.stopPropagation function
171-
// because `originalEvent` should point to the original event for the constancy
172-
// with other events and for more focused logic
173161
}
174162
);
175163

176164
jQuery.event.trigger( e, null, elem );
177-
178-
if ( e.isDefaultPrevented() ) {
179-
event.preventDefault();
180-
}
181165
}
182166

183167
} );

test/unit/event.js

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,6 +2790,81 @@ QUnit.test( "Donor event interference", function( assert ) {
27902790
jQuery( "#donor-input" )[ 0 ].click();
27912791
} );
27922792

2793+
QUnit.test(
2794+
"native stop(Immediate)Propagation/preventDefault methods shouldn't be called",
2795+
function( assert ) {
2796+
var userAgent = window.navigator.userAgent;
2797+
2798+
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
2799+
assert.expect( 1 );
2800+
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
2801+
return;
2802+
}
2803+
2804+
assert.expect( 3 );
2805+
2806+
var checker = {};
2807+
2808+
var html = "<div id='donor-outer'>" +
2809+
"<form id='donor-form'>" +
2810+
"<input id='donor-input' type='radio' />" +
2811+
"</form>" +
2812+
"</div>";
2813+
2814+
jQuery( "#qunit-fixture" ).append( html );
2815+
var outer = jQuery( "#donor-outer" );
2816+
2817+
outer
2818+
.on( "focusin", function( event ) {
2819+
checker.prevent = sinon.stub( event.originalEvent, "preventDefault" );
2820+
event.preventDefault();
2821+
} )
2822+
.on( "focusin", function( event ) {
2823+
checker.simple = sinon.stub( event.originalEvent, "stopPropagation" );
2824+
event.stopPropagation();
2825+
} )
2826+
.on( "focusin", function( event ) {
2827+
checker.immediate = sinon.stub( event.originalEvent, "stopImmediatePropagation" );
2828+
event.stopImmediatePropagation();
2829+
} );
2830+
2831+
jQuery( "#donor-input" ).trigger( "focus" );
2832+
assert.strictEqual( checker.simple.called, false );
2833+
assert.strictEqual( checker.immediate.called, false );
2834+
assert.strictEqual( checker.prevent.called, false );
2835+
2836+
// We need to "off" it, since yes QUnit always update the fixtures
2837+
// but "focus" event listener is attached to document for focus(in | out)
2838+
// event and document doesn't get cleared obviously :)
2839+
outer.off( "focusin" );
2840+
}
2841+
);
2842+
2843+
QUnit.test(
2844+
"isSimulated property always exist on event object",
2845+
function( assert ) {
2846+
var userAgent = window.navigator.userAgent;
2847+
2848+
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
2849+
assert.expect( 1 );
2850+
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
2851+
return;
2852+
}
2853+
2854+
assert.expect( 1 );
2855+
2856+
var element = jQuery( "<input/>" );
2857+
2858+
jQuery( "#qunit-fixture" ).append( element );
2859+
2860+
element.on( "focus", function( event ) {
2861+
assert.notOk( event.isSimulated );
2862+
} );
2863+
2864+
element.trigger( "focus" );
2865+
}
2866+
);
2867+
27932868
QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated event", function( assert ) {
27942869
var userAgent = window.navigator.userAgent;
27952870

@@ -2800,6 +2875,7 @@ QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated e
28002875
}
28012876

28022877
assert.expect( 4 );
2878+
var done = assert.async();
28032879

28042880
var html = "<div id='donor-outer'>" +
28052881
"<form id='donor-form'>" +
@@ -2808,19 +2884,26 @@ QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated e
28082884
"</div>";
28092885

28102886
jQuery( "#qunit-fixture" ).append( html );
2887+
var outer = jQuery( "#donor-outer" );
28112888

2812-
jQuery( "#donor-outer" ).on( "focusin", function( event ) {
2813-
assert.ok( true, "focusin bubbled to outer div" );
2814-
assert.equal( event.originalEvent.type, "focus",
2815-
"make sure originalEvent type is correct" );
2816-
assert.equal( event.type, "focusin", "make sure type is correct" );
2817-
} );
2889+
outer
2890+
.on( "focusin", function( event ) {
2891+
assert.ok( true, "focusin bubbled to outer div" );
2892+
assert.equal( event.originalEvent.type, "focus",
2893+
"make sure originalEvent type is correct" );
2894+
assert.equal( event.type, "focusin", "make sure type is correct" );
2895+
} );
28182896
jQuery( "#donor-input" ).on( "focus", function() {
28192897
assert.ok( true, "got a focus event from the input" );
2898+
done();
28202899
} );
28212900
jQuery( "#donor-input" ).trigger( "focus" );
2822-
} );
28232901

2902+
// We need to "off" it, since yes QUnit always update the fixtures
2903+
// but "focus" event listener is attached to document for focus(in | out)
2904+
// event and document doesn't get cleared obviously :)
2905+
outer.off( "focusin" );
2906+
} );
28242907

28252908
QUnit[ jQuery.fn.click ? "test" : "skip" ]( "trigger() shortcuts", function( assert ) {
28262909
assert.expect( 5 );

0 commit comments

Comments
 (0)