Skip to content

Commit

Permalink
Events: don't execute native stop(Immediate)Propagation from simulation
Browse files Browse the repository at this point in the history
In Firefox, called `stop(Immediate)Propagation` methods,
in capturing phase prevents receiving focus

Fixes jquerygh-3111
  • Loading branch information
markelog committed May 16, 2016
1 parent 95c7ab6 commit 8057932
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/event.js
Expand Up @@ -558,6 +558,7 @@ jQuery.Event.prototype = {
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse,
executeOriginalPropagation: true,

preventDefault: function() {
var e = this.originalEvent;
Expand All @@ -573,7 +574,7 @@ jQuery.Event.prototype = {

this.isPropagationStopped = returnTrue;

if ( e ) {
if ( e && this.executeOriginalPropagation ) {
e.stopPropagation();
}
},
Expand All @@ -582,7 +583,7 @@ jQuery.Event.prototype = {

this.isImmediatePropagationStopped = returnTrue;

if ( e ) {
if ( e && this.executeOriginalPropagation ) {
e.stopImmediatePropagation();
}

Expand Down
22 changes: 8 additions & 14 deletions src/event/trigger.js
Expand Up @@ -150,26 +150,20 @@ jQuery.extend( jQuery.event, {
},

// Piggyback on a donor event to simulate a different one
// Used only for `focus(in | out)` events
simulate: function( type, elem, event ) {
var e = jQuery.extend(
new jQuery.Event(),
event,
{
type: type,
isSimulated: true

// Previously, `originalEvent: {}` was set here, so stopPropagation call
// would not be triggered on donor event, since in our own
// jQuery.event.stopPropagation function we had a check for existence of
// originalEvent.stopPropagation method, so, consequently it would be a noop.
//
// But now, this "simulate" function is used only for events
// for which stopPropagation() is noop, so there is no need for that anymore.
//
// For the compat branch though, guard for "click" and "submit"
// events is still used, but was moved to jQuery.event.stopPropagation function
// because `originalEvent` should point to the original event for the constancy
// with other events and for more focused logic
isSimulated: true,

// Support: Firefox <= 46
// In Firefox, called `stop(Immediate)Propagation` methods, in
// capturing phase, prevents receiving focus,
// so we stop only jQuery propagation not the native one instead
executeOriginalPropagation: false
}
);

Expand Down
40 changes: 40 additions & 0 deletions test/unit/event.js
Expand Up @@ -2821,6 +2821,46 @@ QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated e
jQuery( "#donor-input" ).trigger( "focus" );
} );

QUnit.test(
"navite stop(Immediate)Propagation methods should not be called from simulated event",
function( assert ) {
var userAgent = window.navigator.userAgent;

if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
assert.expect( 1 );
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
return;
}

assert.expect( 2 );

var checker = {};

var html = "<div id='donor-outer'>" +
"<form id='donor-form'>" +
"<input id='donor-input' type='radio' />" +
"</form>" +
"</div>";

jQuery( "#qunit-fixture" ).append( html );

jQuery( "#donor-outer" )
.on( "focusin", function( event ) {
checker.simple = sinon.stub( event.originalEvent, "stopPropagation" );
event.stopPropagation();
} )
.on( "focusin", function( event ) {
checker.immediate = sinon.stub( event.originalEvent, "stopImmediatePropagation" );
event.stopImmediatePropagation();
} );


jQuery( "#donor-input" ).trigger( "focus" );
assert.strictEqual( checker.simple.called, false );
assert.strictEqual( checker.immediate.called, false );
}
);


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

0 comments on commit 8057932

Please sign in to comment.