Skip to content

Commit 7fd36ea

Browse files
fsatelergibson042
authored andcommitted
Event: Evaluate delegate selectors at add time
This ensures that invalid selectors throw right away. Fixes jquerygh-3071 Closes jquerygh-3097
1 parent 931f45f commit 7fd36ea

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/event.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ jQuery.event = {
120120
selector = handleObjIn.selector;
121121
}
122122

123+
// If the selector is invalid, throw any exceptions at attach time
124+
if ( selector ) {
125+
jQuery.find( selector, elem );
126+
}
127+
123128
// Make sure that the handler has a unique ID, used to find/remove it later
124129
if ( !handler.guid ) {
125130
handler.guid = jQuery.guid++;

test/unit/event.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,19 @@ QUnit.test( "Delegated events in SVG (#10791; #13180)", function( assert ) {
12891289
jQuery( "#qunit-fixture" ).off( "click" );
12901290
} );
12911291

1292+
QUnit.test( "Delegated events with malformed selectors (#3071)", function( assert ) {
1293+
assert.expect( 2 );
1294+
1295+
assert.throws( function () {
1296+
jQuery( "#qunit-fixture" ).on( "click", "div:not", function () { } );
1297+
}, null, "malformed selector throws on attach" );
1298+
1299+
jQuery( "#qunit-fixture" ).click();
1300+
assert.ok( true, "malformed selector does not throw on event" );
1301+
1302+
jQuery( "#qunit-fixture" ).off( "click" );
1303+
} );
1304+
12921305
QUnit.test( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function( assert ) {
12931306
assert.expect( 5 );
12941307

0 commit comments

Comments
 (0)