Skip to content

Commit

Permalink
Fixes #7922. Copy the donor event when simulating a bubbling submit i…
Browse files Browse the repository at this point in the history
…n IE so that we don't accidentally stop propagation on it. Remove a bunch of return statements that could also cancel the event. DRY out the liveFired change from #6359 by moving it to the trigger() function.
  • Loading branch information
dmethvin authored and jitter committed Feb 15, 2011
1 parent 1ddfdab commit 12c0e1a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/event.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -709,8 +709,7 @@ if ( !jQuery.support.submitBubbles ) {
type = elem.type; type = elem.type;


if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) { if ( (type === "submit" || type === "image") && jQuery( elem ).closest("form").length ) {
e.liveFired = undefined; trigger( "submit", this, arguments );
return trigger( "submit", this, arguments );
} }
}); });


Expand All @@ -719,8 +718,7 @@ if ( !jQuery.support.submitBubbles ) {
type = elem.type; type = elem.type;


if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) { if ( (type === "text" || type === "password") && jQuery( elem ).closest("form").length && e.keyCode === 13 ) {
e.liveFired = undefined; trigger( "submit", this, arguments );
return trigger( "submit", this, arguments );
} }
}); });


Expand Down Expand Up @@ -783,7 +781,7 @@ if ( !jQuery.support.changeBubbles ) {
if ( data != null || val ) { if ( data != null || val ) {
e.type = "change"; e.type = "change";
e.liveFired = undefined; e.liveFired = undefined;
return jQuery.event.trigger( e, arguments[1], elem ); jQuery.event.trigger( e, arguments[1], elem );
} }
}; };


Expand All @@ -797,7 +795,7 @@ if ( !jQuery.support.changeBubbles ) {
var elem = e.target, type = elem.type; var elem = e.target, type = elem.type;


if ( type === "radio" || type === "checkbox" || elem.nodeName.toLowerCase() === "select" ) { if ( type === "radio" || type === "checkbox" || elem.nodeName.toLowerCase() === "select" ) {
return testChange.call( this, e ); testChange.call( this, e );
} }
}, },


Expand All @@ -809,7 +807,7 @@ if ( !jQuery.support.changeBubbles ) {
if ( (e.keyCode === 13 && elem.nodeName.toLowerCase() !== "textarea") || if ( (e.keyCode === 13 && elem.nodeName.toLowerCase() !== "textarea") ||
(e.keyCode === 32 && (type === "checkbox" || type === "radio")) || (e.keyCode === 32 && (type === "checkbox" || type === "radio")) ||
type === "select-multiple" ) { type === "select-multiple" ) {
return testChange.call( this, e ); testChange.call( this, e );
} }
}, },


Expand Down Expand Up @@ -848,8 +846,18 @@ if ( !jQuery.support.changeBubbles ) {
} }


function trigger( type, elem, args ) { function trigger( type, elem, args ) {
args[0].type = type; // Piggyback on a donor event to simulate a different one.
return jQuery.event.handle.apply( elem, args ); // Fake originalEvent to avoid donor's stopPropagation, but if the
// simulated event prevents default then we do the same on the donor.
// Don't pass args or remember liveFired; they apply to the donor event.
var event = jQuery.extend( {}, args[ 0 ] );
event.type = type;
event.originalEvent = {};
event.liveFired = undefined;
jQuery.event.handle.call( elem, event );
if ( event.isDefaultPrevented() ) {
args[ 0 ].preventDefault();
}
} }


// Create "bubbling" focus and blur events // Create "bubbling" focus and blur events
Expand Down
11 changes: 11 additions & 0 deletions test/unit/event.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1455,6 +1455,8 @@ test("live with change", function(){
}); });


test("live with submit", function() { test("live with submit", function() {
expect(5);

var count1 = 0, count2 = 0; var count1 = 0, count2 = 0;


jQuery("#testForm").live("submit", function(ev) { jQuery("#testForm").live("submit", function(ev) {
Expand All @@ -1471,7 +1473,16 @@ test("live with submit", function() {
equals( count1, 1, "Verify form submit." ); equals( count1, 1, "Verify form submit." );
equals( count2, 1, "Verify body submit." ); equals( count2, 1, "Verify body submit." );


jQuery("#testForm input[name=sub1]").live("click", function(ev) {
ok( true, "cancelling submit still calls click handler" );
});

jQuery("#testForm input[name=sub1]")[0].click();
equals( count1, 2, "Verify form submit." );
equals( count2, 2, "Verify body submit." );

jQuery("#testForm").die("submit"); jQuery("#testForm").die("submit");
jQuery("#testForm input[name=sub1]").die("click");
jQuery("body").die("submit"); jQuery("body").die("submit");
}); });


Expand Down

0 comments on commit 12c0e1a

Please sign in to comment.