From 7475d5debeb7c53158921ed40f6c2fdb25a2cc86 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Tue, 12 May 2015 15:30:05 +0300 Subject: [PATCH] Event: Remove fake originalEvent from jQuery.Event.simulate Fixes gh-2300 Closes gh-2303 --- src/event.js | 8 +++++-- test/index.html | 5 +++++ test/unit/event.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index 361a5489f..9bc875e24 100644 --- a/src/event.js +++ b/src/event.js @@ -610,10 +610,14 @@ jQuery.event = { event, { type: type, - isSimulated: true, - originalEvent: {} + isSimulated: true } ); + + // This prevents stopPropagation(), stopImmediatePropagation(), and preventDefault() from + // preventing default on the donor event. + delete e.originalEvent; + if ( bubble ) { jQuery.event.trigger( e, null, elem ); } else { diff --git a/test/index.html b/test/index.html index 9e232895d..d8ccdaa45 100644 --- a/test/index.html +++ b/test/index.html @@ -42,6 +42,11 @@
+
+
+ +
+

See this blog entry for more information.

diff --git a/test/unit/event.js b/test/unit/event.js index a84ec087a..981e6702f 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2671,6 +2671,60 @@ test( ".off() removes the expando when there's no more data", function() { } }); +test( "preventDefault() on focusin does not throw exception", function( assert ) { + expect( 1 ); + + var done = assert.async(), + input = jQuery( "" ).appendTo( "#form" ); + input + .on( "focusin", function( event ) { + var exceptionCaught; + + try { + event.preventDefault(); + } catch ( theException ) { + exceptionCaught = theException; + } + + assert.strictEqual( exceptionCaught, undefined, + "Preventing default on focusin throws no exception" ); + + done(); + } ) + .focus(); +} ); + +test( "jQuery.event.simulate() event has no originalEvent", function( assert ) { + expect( 1 ); + + var done = assert.async(), + input = jQuery( "" ) + .on( "click", function( event ) { + assert.strictEqual( "originalEvent" in event, false, + "originalEvent not present on simulated event" ); + done(); + } ); + + jQuery.event.simulate( "click", input[ 0 ], new jQuery.Event(), true ); +} ); + +test( "Donor event interference", function( assert ) { + assert.expect( 4 ); + + jQuery( "#donor-outer" ).on( "click", function() { + assert.ok( true, "click bubbled to outer div" ); + } ); + jQuery( "#donor-input" ).on( "click", function( event ) { + assert.ok( true, "got a click event from the input" ); + assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" ); + } ); + jQuery( "#donor-input" ).on( "change", function( event ) { + assert.ok( true, "got a change event from the input" ); + event.stopPropagation(); + } ); + jQuery( "#donor-input" )[0].click(); +} ); + // This tests are unreliable in Firefox if ( !(/firefox/i.test( window.navigator.userAgent )) ) { test( "Check order of focusin/focusout events", 2, function() {