Make sure that it's possible to preventDefault natively-triggered (submit, focus, blur, click) events. Fixes #5695.

This commit is contained in:
jeresig 2010-01-25 13:45:07 -05:00
parent 323270f6dc
commit d24443fb55
2 changed files with 61 additions and 21 deletions

View File

@ -270,36 +270,48 @@ jQuery.event = {
handle.apply( elem, data ); handle.apply( elem, data );
} }
var nativeFn, nativeHandler; var parent = elem.parentNode || elem.ownerDocument;
// Trigger an inline bound script
try { try {
if ( !(elem && elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()]) ) { if ( !(elem && elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()]) ) {
nativeFn = elem[ type ]; if ( elem[ "on" + type ] && elem[ "on" + type ].apply( elem, data ) === false ) {
nativeHandler = elem[ "on" + type ]; event.result = false;
}
} }
// prevent IE from throwing an error for some elements with some event types, see #3533 // prevent IE from throwing an error for some elements with some event types, see #3533
} catch (e) {} } catch (e) {}
var isClick = jQuery.nodeName(elem, "a") && type === "click"; if ( !event.isPropagationStopped() && parent ) {
jQuery.event.trigger( event, data, parent, true );
// Trigger the native events (except for clicks on links) } else if ( !event.isDefaultPrevented() ) {
if ( !bubbling && nativeFn && !event.isDefaultPrevented() && !isClick ) { var target = event.target, old,
this.triggered = true; isClick = jQuery.nodeName(target, "a") && type === "click";
try {
elem[ type ]();
// prevent IE from throwing an error for some hidden elements
} catch (e) {}
// Handle triggering native .onfoo handlers if ( !isClick && !(target && target.nodeName && jQuery.noData[target.nodeName.toLowerCase()]) ) {
} else if ( nativeHandler && elem[ "on" + type ].apply( elem, data ) === false ) { try {
event.result = false; if ( target[ type ] ) {
} // Make sure that we don't accidentally re-trigger the onFOO events
old = target[ "on" + type ];
this.triggered = false; if ( old ) {
target[ "on" + type ] = null;
}
if ( !event.isPropagationStopped() ) { this.triggered = true;
var parent = elem.parentNode || elem.ownerDocument; target[ type ]();
if ( parent ) { }
jQuery.event.trigger( event, data, parent, true );
// prevent IE from throwing an error for some elements with some event types, see #3533
} catch (e) {}
if ( old ) {
target[ "on" + type ] = old;
}
this.triggered = false;
} }
} }
}, },

View File

@ -394,7 +394,7 @@ test("trigger() bubbling", function() {
}); });
test("trigger(type, [data], [fn])", function() { test("trigger(type, [data], [fn])", function() {
expect(12); expect(14);
var handler = function(event, a, b, c) { var handler = function(event, a, b, c) {
equals( event.type, "click", "check passed data" ); equals( event.type, "click", "check passed data" );
@ -439,6 +439,34 @@ test("trigger(type, [data], [fn])", function() {
pass = false; pass = false;
} }
ok( pass, "Trigger on a table with a colon in the even type, see #3533" ); ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
var form = jQuery("<form action=''></form>").appendTo("body");
// Make sure it can be prevented locally
form.submit(function(){
ok( true, "Local bind still works." );
return false;
});
// Trigger 1
form.trigger("submit");
form.unbind("submit");
jQuery(document).submit(function(){
ok( true, "Make sure bubble works up to document." );
return false;
});
// Trigger 1
form.trigger("submit");
jQuery(document).unbind("submit");
form.remove();
});
test("jQuery.Event.currentTarget", function(){
}); });
test("trigger(eventObject, [data], [fn])", function() { test("trigger(eventObject, [data], [fn])", function() {