Skip to content

Commit 6df669f

Browse files
committed
Event: remove outdated originalEvent hack
Closes gh-2335 Ref 7475d5d
1 parent 7475d5d commit 6df669f

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

src/event.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -601,23 +601,29 @@ jQuery.event = {
601601
}
602602
},
603603

604+
// Piggyback on a donor event to simulate a different one
604605
simulate: function( type, elem, event, bubble ) {
605-
// Piggyback on a donor event to simulate a different one.
606-
// Fake originalEvent to avoid donor's stopPropagation, but if the
607-
// simulated event prevents default then we do the same on the donor.
608606
var e = jQuery.extend(
609607
new jQuery.Event(),
610608
event,
611609
{
612610
type: type,
613611
isSimulated: true
612+
// Previously, `originalEvent: {}` was set here, so stopPropagation call
613+
// would not be triggered on donor event, since in our own
614+
// jQuery.event.stopPropagation function we had a check for existence of
615+
// originalEvent.stopPropagation method, so, consequently it would be a noop.
616+
//
617+
// But now, this "simulate" function is used only for events
618+
// for which stopPropagation() is noop, so there is no need for that anymore.
619+
//
620+
// For the compat branch though, guard for "click" and "submit"
621+
// events is still used, but was moved to jQuery.event.stopPropagation function
622+
// because `originalEvent` should point to the original event for the constancy
623+
// with other events and for more focused logic
614624
}
615625
);
616626

617-
// This prevents stopPropagation(), stopImmediatePropagation(), and preventDefault() from
618-
// preventing default on the donor event.
619-
delete e.originalEvent;
620-
621627
if ( bubble ) {
622628
jQuery.event.trigger( e, null, elem );
623629
} else {

test/unit/event.js

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2694,35 +2694,68 @@ test( "preventDefault() on focusin does not throw exception", function( assert )
26942694
.focus();
26952695
} );
26962696

2697-
test( "jQuery.event.simulate() event has no originalEvent", function( assert ) {
2698-
expect( 1 );
2697+
test( "Donor event interference", function( assert ) {
2698+
assert.expect( 10 );
26992699

2700-
var done = assert.async(),
2701-
input = jQuery( "<input>" )
2702-
.on( "click", function( event ) {
2703-
assert.strictEqual( "originalEvent" in event, false,
2704-
"originalEvent not present on simulated event" );
2705-
done();
2706-
} );
2707-
2708-
jQuery.event.simulate( "click", input[ 0 ], new jQuery.Event(), true );
2709-
} );
2700+
var html = "<div id='donor-outer'>" +
2701+
"<form id='donor-form'>" +
2702+
"<input id='donor-input' type='radio' />" +
2703+
"</form>" +
2704+
"</div>";
27102705

2711-
test( "Donor event interference", function( assert ) {
2712-
assert.expect( 4 );
2706+
jQuery( "#qunit-fixture" ).append( html );
27132707

2714-
jQuery( "#donor-outer" ).on( "click", function() {
2708+
jQuery( "#donor-outer" ).on( "click", function( event ) {
27152709
assert.ok( true, "click bubbled to outer div" );
2710+
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
2711+
assert.equal( event.type, "click", "make sure event type is correct" );
27162712
} );
27172713
jQuery( "#donor-input" ).on( "click", function( event ) {
27182714
assert.ok( true, "got a click event from the input" );
27192715
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
2716+
assert.equal( event.type, "click", "make sure event type is correct" );
2717+
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
27202718
} );
27212719
jQuery( "#donor-input" ).on( "change", function( event ) {
2720+
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
2721+
assert.equal( event.type, "change", "make sure event type is correct" );
27222722
assert.ok( true, "got a change event from the input" );
27232723
event.stopPropagation();
27242724
} );
2725-
jQuery( "#donor-input" )[0].click();
2725+
jQuery( "#donor-input" )[ 0 ].click();
2726+
} );
2727+
2728+
test( "originalEvent property for Chrome, Safari and FF of simualted event", function( assert ) {
2729+
var userAgent = window.navigator.userAgent;
2730+
2731+
if ( !(/chrome/i.test( userAgent ) ||
2732+
/firefox/i.test( userAgent ) ||
2733+
/safari/i.test( userAgent ) ) ) {
2734+
assert.expect( 1 );
2735+
assert.ok( true, "Assertions should run only in Chrome, Safari and FF" );
2736+
return;
2737+
}
2738+
2739+
assert.expect( 4 );
2740+
2741+
var html = "<div id='donor-outer'>" +
2742+
"<form id='donor-form'>" +
2743+
"<input id='donor-input' type='radio' />" +
2744+
"</form>" +
2745+
"</div>";
2746+
2747+
jQuery( "#qunit-fixture" ).append( html );
2748+
2749+
jQuery( "#donor-outer" ).on( "focusin", function( event ) {
2750+
assert.ok( true, "focusin bubbled to outer div" );
2751+
assert.equal( event.originalEvent.type, "focus",
2752+
"make sure originalEvent type is correct" );
2753+
assert.equal( event.type, "focusin", "make sure type is correct" );
2754+
} );
2755+
jQuery( "#donor-input" ).on( "focus", function() {
2756+
assert.ok( true, "got a focus event from the input" );
2757+
} );
2758+
jQuery( "#donor-input" ).trigger( "focus" );
27262759
} );
27272760

27282761
// This tests are unreliable in Firefox

0 commit comments

Comments
 (0)