]> source.dussan.org Git - jquery.git/commitdiff
Event: remove outdated originalEvent hack
authorOleg Gaidarenko <markelog@gmail.com>
Wed, 20 May 2015 15:09:46 +0000 (18:09 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Fri, 29 May 2015 17:32:59 +0000 (20:32 +0300)
Closes gh-2335
Ref 7475d5debeb7c53158921ed40f6c2fdb25a2cc86

src/event.js
test/unit/event.js

index 9bc875e24c4edda29e59262171127648a6b18f19..8722268a806f7aeb60acdf45c7213cc90f3f2d32 100644 (file)
@@ -601,23 +601,29 @@ jQuery.event = {
                }
        },
 
+       // Piggyback on a donor event to simulate a different one
        simulate: function( type, elem, event, bubble ) {
-               // Piggyback on a donor event to simulate a different one.
-               // Fake originalEvent to avoid donor's stopPropagation, but if the
-               // simulated event prevents default then we do the same on the donor.
                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
                        }
                );
 
-               // 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 {
index 981e6702fc6091c48b80bf3bdfa9258bf4e9523f..ba3d9481a7f6c14b558ddd29082c05793a808499 100644 (file)
@@ -2694,35 +2694,68 @@ test( "preventDefault() on focusin does not throw exception", function( assert )
                        .focus();
 } );
 
-test( "jQuery.event.simulate() event has no originalEvent", function( assert ) {
-       expect( 1 );
+test( "Donor event interference", function( assert ) {
+       assert.expect( 10 );
 
-       var done = assert.async(),
-               input = jQuery( "<input>" )
-               .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 );
-} );
+       var html = "<div id='donor-outer'>" +
+               "<form id='donor-form'>" +
+                       "<input id='donor-input' type='radio' />" +
+               "</form>" +
+       "</div>";
 
-test( "Donor event interference", function( assert ) {
-       assert.expect( 4 );
+       jQuery( "#qunit-fixture" ).append( html );
 
-       jQuery( "#donor-outer" ).on( "click", function() {
+       jQuery( "#donor-outer" ).on( "click", function( event ) {
                assert.ok( true, "click bubbled to outer div" );
+               assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
+               assert.equal( event.type, "click", "make sure event type is correct" );
        } );
        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" );
+               assert.equal( event.type, "click", "make sure event type is correct" );
+               assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
        } );
        jQuery( "#donor-input" ).on( "change", function( event ) {
+               assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
+               assert.equal( event.type, "change", "make sure event type is correct" );
                assert.ok( true, "got a change event from the input" );
                event.stopPropagation();
        } );
-       jQuery( "#donor-input" )[0].click();
+       jQuery( "#donor-input" )[ 0 ].click();
+} );
+
+test( "originalEvent property for Chrome, Safari and FF of simualted event", function( assert ) {
+       var userAgent = window.navigator.userAgent;
+
+       if ( !(/chrome/i.test( userAgent ) ||
+              /firefox/i.test( userAgent ) ||
+              /safari/i.test( userAgent ) ) ) {
+               assert.expect( 1 );
+               assert.ok( true, "Assertions should run only in Chrome, Safari and FF" );
+               return;
+       }
+
+       assert.expect( 4 );
+
+       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 ) {
+               assert.ok( true, "focusin bubbled to outer div" );
+               assert.equal( event.originalEvent.type, "focus",
+                            "make sure originalEvent type is correct" );
+               assert.equal( event.type, "focusin", "make sure type is correct" );
+       } );
+       jQuery( "#donor-input" ).on( "focus", function() {
+               assert.ok( true, "got a focus event from the input" );
+       } );
+       jQuery( "#donor-input" ).trigger( "focus" );
 } );
 
 // This tests are unreliable in Firefox