Skip to content

Commit e8825a5

Browse files
committed
Event: Cover invalid delegation selector edge cases
Ref 7fd36ea
1 parent 7fd36ea commit e8825a5

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/event.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
define( [
22
"./core",
33
"./var/document",
4+
"./var/documentElement",
45
"./var/rnotwhite",
56
"./var/slice",
67
"./data/var/dataPriv",
78

89
"./core/init",
910
"./selector"
10-
], function( jQuery, document, rnotwhite, slice, dataPriv ) {
11+
], function( jQuery, document, documentElement, rnotwhite, slice, dataPriv ) {
1112

1213
"use strict";
1314

@@ -120,9 +121,10 @@ jQuery.event = {
120121
selector = handleObjIn.selector;
121122
}
122123

123-
// If the selector is invalid, throw any exceptions at attach time
124+
// Ensure that invalid selectors throw exceptions at attach time
125+
// Evaluate against documentElement in case elem is a non-element node (e.g., document)
124126
if ( selector ) {
125-
jQuery.find( selector, elem );
127+
jQuery.find.matchesSelector( documentElement, selector );
126128
}
127129

128130
// Make sure that the handler has a unique ID, used to find/remove it later

test/unit/event.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,17 +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 );
1292+
QUnit.test( "Delegated events with malformed selectors (gh-3071)", function( assert ) {
1293+
assert.expect( 3 );
12941294

1295-
assert.throws( function () {
1296-
jQuery( "#qunit-fixture" ).on( "click", "div:not", function () { } );
1297-
}, null, "malformed selector throws on attach" );
1295+
assert.throws( function() {
1296+
jQuery( "#foo" ).on( "click", ":not", function() {} );
1297+
}, "malformed selector throws on attach" );
12981298

1299-
jQuery( "#qunit-fixture" ).click();
1300-
assert.ok( true, "malformed selector does not throw on event" );
1299+
assert.throws( function() {
1300+
jQuery( "#foo" ).on( "click", "nonexistent:not", function() {} );
1301+
}, "short-circuitable malformed selector throws on attach" );
13011302

1302-
jQuery( "#qunit-fixture" ).off( "click" );
1303+
jQuery( "#foo > :first-child" ).click();
1304+
assert.ok( true, "malformed selector does not throw on event" );
13031305
} );
13041306

13051307
QUnit.test( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function( assert ) {

0 commit comments

Comments
 (0)