From 2bbc3d5860b81e128cd92f865673e10046caac7d Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Tue, 26 Feb 2013 12:19:04 -0500 Subject: [PATCH] Fix #13471. $().on(".xyz"...) should avoid later crash. If the event type is an empty string we end up hanging in .off() which makes for mighty hard debugging. Instead treat it as a no-op. Docs seem clear this is not allowed. --- src/event.js | 5 +++++ test/unit/event.js | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/event.js b/src/event.js index 4266a1ddb..8a52c60ea 100644 --- a/src/event.js +++ b/src/event.js @@ -66,6 +66,11 @@ jQuery.event = { tmp = rtypenamespace.exec( types[t] ) || []; type = origType = tmp[1]; namespaces = ( tmp[2] || "" ).split( "." ).sort(); + + // There *must* be a type, no attaching namespace-only handlers + if ( !type ) { + continue; + } // If event changes its type, use the special event handlers for the changed type special = jQuery.event.special[ type ] || {}; diff --git a/test/unit/event.js b/test/unit/event.js index 6be4eb8e7..97c29ffef 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -555,6 +555,22 @@ test("bind(), multi-namespaced events", function() { jQuery("#firstp").trigger("custom"); }); +test("namespace-only event binding is a no-op", function(){ + expect(2); + + jQuery("#firstp") + .on( ".whoops", function() { + ok( false, "called a namespace-only event" ); + }) + .on( "whoops", function() { + ok( true, "called whoops" ); + }) + .trigger("whoops") // 1 + .off(".whoops") + .trigger("whoops") // 2 + .off("whoops"); +}); + test("bind(), with same function", function() { expect(2); -- 2.39.5