Permalink
Browse files

Event: Cover invalid delegation selector edge cases

  • Loading branch information...
1 parent 7fd36ea commit e8825a529b97a27b8b2e40eaaa7773189642c772 @gibson042 gibson042 committed May 7, 2016
Showing with 15 additions and 11 deletions.
  1. +5 −3 src/event.js
  2. +10 −8 test/unit/event.js
View
@@ -1,13 +1,14 @@
define( [
"./core",
"./var/document",
+ "./var/documentElement",
"./var/rnotwhite",
"./var/slice",
"./data/var/dataPriv",
"./core/init",
"./selector"
-], function( jQuery, document, rnotwhite, slice, dataPriv ) {
+], function( jQuery, document, documentElement, rnotwhite, slice, dataPriv ) {
"use strict";
@@ -120,9 +121,10 @@ jQuery.event = {
selector = handleObjIn.selector;
}
- // If the selector is invalid, throw any exceptions at attach time
+ // Ensure that invalid selectors throw exceptions at attach time
+ // Evaluate against documentElement in case elem is a non-element node (e.g., document)
if ( selector ) {
- jQuery.find( selector, elem );
+ jQuery.find.matchesSelector( documentElement, selector );
}
// Make sure that the handler has a unique ID, used to find/remove it later
View
@@ -1289,17 +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 );
+QUnit.test( "Delegated events with malformed selectors (gh-3071)", function( assert ) {
+ assert.expect( 3 );
- assert.throws( function () {
- jQuery( "#qunit-fixture" ).on( "click", "div:not", function () { } );
- }, null, "malformed selector throws on attach" );
+ assert.throws( function() {
+ jQuery( "#foo" ).on( "click", ":not", function() {} );
+ }, "malformed selector throws on attach" );
- jQuery( "#qunit-fixture" ).click();
- assert.ok( true, "malformed selector does not throw on event" );
+ assert.throws( function() {
+ jQuery( "#foo" ).on( "click", "nonexistent:not", function() {} );
+ }, "short-circuitable malformed selector throws on attach" );
- jQuery( "#qunit-fixture" ).off( "click" );
+ jQuery( "#foo > :first-child" ).click();
+ assert.ok( true, "malformed selector does not throw on event" );
} );
QUnit.test( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function( assert ) {

0 comments on commit e8825a5

Please sign in to comment.