From 90c907e8b65bc9d323863fc57389f2dca611292d Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Mon, 7 Nov 2011 11:07:36 -0500 Subject: [PATCH] Fix #10701, .preventDefault if an inline handler returns false. Baby unicorns are slapped each time you use inline handlers, so do it sparingly. --- src/event.js | 5 +++-- test/unit/event.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/event.js b/src/event.js index 0ccf23da3..9a1ac37d2 100644 --- a/src/event.js +++ b/src/event.js @@ -343,9 +343,10 @@ jQuery.event = { if ( handle ) { handle.apply( cur, data ); } + // Note that this is a bare JS function and not a jQuery handler handle = ontype && cur[ ontype ]; - if ( handle && jQuery.acceptData( cur ) ) { - handle.apply( cur, data ); + if ( handle && jQuery.acceptData( cur ) && handle.apply( cur, data ) === false ) { + event.preventDefault(); } if ( event.isPropagationStopped() ) { diff --git a/test/unit/event.js b/test/unit/event.js index 1305dd3ae..93049003a 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -2273,6 +2273,18 @@ test("Non DOM element events", function() { jQuery(o).trigger("nonelementobj"); }); +test("inline handler returning false stops default", function() { + expect(1); + + var markup = jQuery('
x
'); + markup.click(function(e) { + ok( e.isDefaultPrevented(), "inline handler prevented default"); + return false; + }); + markup.find("a").click(); + markup.off("click"); +}); + test("window resize", function() { expect(2);