Event: Evaluate delegate selectors at add time

This ensures that invalid selectors throw right away.

Fixes gh-3071
Closes gh-3097
This commit is contained in:
Felipe Sateler 2016-05-01 18:40:20 -03:00 committed by Richard Gibson
parent 931f45fc38
commit 7fd36ea145
2 changed files with 18 additions and 0 deletions

View File

@ -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++;

View File

@ -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 );