From 93f95c966355f02b5695f50523f88687eadb4c81 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Wed, 3 Dec 2014 14:51:24 -0500 Subject: [PATCH] Core: Throw an error on $("#") rather than returning 0-length collection Closes gh-1682 Thanks @goob for the issue report! (cherry picked from commit 80022c81ce4a07a232afd3c580b0977555a2daec) --- src/core/init.js | 3 ++- test/unit/core.js | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/init.js b/src/core/init.js index 340e378b8..c0f50d0c0 100644 --- a/src/core/init.js +++ b/src/core/init.js @@ -14,7 +14,8 @@ var rootjQuery, // A simple way to check for HTML strings // Prioritize #id over to avoid XSS via location.hash (#9521) // Strict HTML recognition (#11290: must start with <) - rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/, + // Shortcut simple #id case for speed + rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/, init = jQuery.fn.init = function( selector, context ) { var match, elem; diff --git a/test/unit/core.js b/test/unit/core.js index 2fefb3725..9d02f1a86 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -57,10 +57,15 @@ test("jQuery()", function() { equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" ); equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" ); equal( jQuery("").length, 0, "jQuery('') === jQuery([])" ); - equal( jQuery("#").length, 0, "jQuery('#') === jQuery([])" ); - equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" ); + // Invalid #id goes to Sizzle which will throw an error (gh-1682) + try { + jQuery( "#" ); + } catch ( e ) { + ok( true, "Threw an error on #id with no id" ); + } + // can actually yield more than one, when iframes are included, the window is an array as well equal( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" ); -- 2.39.5