From 7fd36ea145a11d5896de6d064b546b1c57a83f34 Mon Sep 17 00:00:00 2001 From: Felipe Sateler Date: Sun, 1 May 2016 18:40:20 -0300 Subject: [PATCH] Event: Evaluate delegate selectors at add time This ensures that invalid selectors throw right away. Fixes gh-3071 Closes gh-3097 --- src/event.js | 5 +++++ test/unit/event.js | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/event.js b/src/event.js index ffaec578d..3f1c5f31a 100644 --- a/src/event.js +++ b/src/event.js @@ -120,6 +120,11 @@ jQuery.event = { selector = handleObjIn.selector; } + // If the selector is invalid, throw any exceptions at attach time + if ( selector ) { + jQuery.find( selector, elem ); + } + // Make sure that the handler has a unique ID, used to find/remove it later if ( !handler.guid ) { handler.guid = jQuery.guid++; diff --git a/test/unit/event.js b/test/unit/event.js index 236214730..6b4af04b9 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -1289,6 +1289,19 @@ QUnit.test( "Delegated events in SVG (#10791; #13180)", function( assert ) { jQuery( "#qunit-fixture" ).off( "click" ); } ); +QUnit.test( "Delegated events with malformed selectors (#3071)", function( assert ) { + assert.expect( 2 ); + + assert.throws( function () { + jQuery( "#qunit-fixture" ).on( "click", "div:not", function () { } ); + }, null, "malformed selector throws on attach" ); + + jQuery( "#qunit-fixture" ).click(); + assert.ok( true, "malformed selector does not throw on event" ); + + jQuery( "#qunit-fixture" ).off( "click" ); +} ); + QUnit.test( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function( assert ) { assert.expect( 5 );