Skip to content

Commit 92a92be

Browse files
committed
Fix #11049. Let bubbling submit be cancellable in oldIE.
1 parent 619f0d9 commit 92a92be

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

src/event.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,19 @@ jQuery.event = {
391391
delegateCount = handlers.delegateCount,
392392
args = [].slice.call( arguments, 0 ),
393393
run_all = !event.exclusive && !event.namespace,
394+
special = jQuery.event.special[ event.type ] || {},
394395
handlerQueue = [],
395396
i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related;
396397

397398
// Use the fix-ed jQuery.Event rather than the (read-only) native event
398399
args[0] = event;
399400
event.delegateTarget = this;
400401

402+
// Call the preDispatch hook for the mapped type, and let it bail if desired
403+
if ( special.preDispatch && special.preDispatch.call( this, event ) === false ) {
404+
return;
405+
}
406+
401407
// Determine handlers that should run if there are delegated events
402408
// Avoid non-left-click bubbling in Firefox (#3861)
403409
if ( delegateCount && !(event.button && event.type === "click") ) {
@@ -407,7 +413,7 @@ jQuery.event = {
407413
jqcur.context = this.ownerDocument || this;
408414

409415
for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
410-
416+
411417
// Don't process events on disabled elements (#6911, #8165)
412418
if ( cur.disabled !== true ) {
413419
selMatch = {};
@@ -467,6 +473,11 @@ jQuery.event = {
467473
}
468474
}
469475

476+
// Call the postDispatch hook for the mapped type
477+
if ( special.postDispatch ) {
478+
special.postDispatch.call( this, event );
479+
}
480+
470481
return event.result;
471482
},
472483

@@ -758,16 +769,23 @@ if ( !jQuery.support.submitBubbles ) {
758769
form = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.form : undefined;
759770
if ( form && !form._submit_attached ) {
760771
jQuery.event.add( form, "submit._submit", function( event ) {
761-
// If form was submitted by the user, bubble the event up the tree
762-
if ( this.parentNode && !event.isTrigger ) {
763-
jQuery.event.simulate( "submit", this.parentNode, event, true );
764-
}
772+
event._submit_bubble = true;
765773
});
766774
form._submit_attached = true;
767775
}
768776
});
769777
// return undefined since we don't need an event listener
770778
},
779+
780+
postDispatch: function( event ) {
781+
// If form was submitted by the user, bubble the event up the tree
782+
if ( event._submit_bubble ) {
783+
delete event._submit_bubble;
784+
if ( this.parentNode && !event.isTrigger ) {
785+
jQuery.event.simulate( "submit", this.parentNode, event, true );
786+
}
787+
}
788+
},
771789

772790
teardown: function() {
773791
// Only need this for delegated form submit events

test/unit/event.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,10 +1216,10 @@ test("Delegated events in SVG (#10791)", function() {
12161216
test("Delegated events in forms (#10844; #11145; #8165)", function() {
12171217
expect(3);
12181218

1219-
// Aliases names like "id" cause havoc
1219+
// Alias names like "id" cause havoc
12201220
var form = jQuery(
12211221
'<form id="myform">'+
1222-
'<input type="text" name="id" value="secret agent man" />'+
1222+
'<input type="text" name="id" value="secret agent man" />'+
12231223
'</form>'
12241224
)
12251225
.on( "submit", function( event ) {
@@ -1259,6 +1259,44 @@ test("Delegated events in forms (#10844; #11145; #8165)", function() {
12591259
form.remove();
12601260
});
12611261

1262+
test("Submit event can be stopped (#11049)", function() {
1263+
expect(1);
1264+
1265+
// Since we manually bubble in IE, make sure inner handlers get a chance to cancel
1266+
var form = jQuery(
1267+
'<form id="myform">'+
1268+
'<input type="text" name="sue" value="bawls" />'+
1269+
'<input type="submit" />'+
1270+
'</form>'
1271+
)
1272+
.appendTo("body");
1273+
1274+
jQuery( "body" )
1275+
.on( "submit", function() {
1276+
ok( true, "submit bubbled on first handler" );
1277+
return false;
1278+
})
1279+
.find( "#myform input[type=submit]" )
1280+
.each( function(){ this.click(); } )
1281+
.end()
1282+
.on( "submit", function() {
1283+
ok( false, "submit bubbled on second handler" );
1284+
return false;
1285+
})
1286+
.find( "#myform input[type=submit]" )
1287+
.each( function(){
1288+
jQuery( this.form ).on( "submit", function( e ) {
1289+
e.preventDefault();
1290+
e.stopPropagation();
1291+
});
1292+
this.click();
1293+
})
1294+
.end()
1295+
.off( "submit" );
1296+
1297+
form.remove();
1298+
});
1299+
12621300
test("jQuery.Event( type, props )", function() {
12631301

12641302
expect(5);
@@ -2664,7 +2702,7 @@ test("clone() delegated events (#11076)", function() {
26642702
clicked = function( event ) {
26652703
counter[ jQuery(this).text().replace(/\s+/, "") ]++;
26662704
},
2667-
table =
2705+
table =
26682706
jQuery( "<table><tr><td>center</td><td>fold</td></tr></table>" )
26692707
.on( "click", "tr", clicked )
26702708
.on( "click", "td:first-child", clicked )

0 commit comments

Comments
 (0)