Skip to content

Commit 93f95c9

Browse files
committed
Core: Throw an error on $("#") rather than returning 0-length collection
Closes gh-1682 Thanks @goob for the issue report! (cherry picked from commit 80022c8)
1 parent 503e545 commit 93f95c9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/core/init.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ var rootjQuery,
1414
// A simple way to check for HTML strings
1515
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
1616
// Strict HTML recognition (#11290: must start with <)
17-
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]*))$/,
17+
// Shortcut simple #id case for speed
18+
rquickExpr = /^(?:\s*(<[\w\W]+>)[^>]*|#([\w-]+))$/,
1819

1920
init = jQuery.fn.init = function( selector, context ) {
2021
var match, elem;

test/unit/core.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ test("jQuery()", function() {
5757
equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
5858
equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
5959
equal( jQuery("").length, 0, "jQuery('') === jQuery([])" );
60-
equal( jQuery("#").length, 0, "jQuery('#') === jQuery([])" );
61-
6260
equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
6361

62+
// Invalid #id goes to Sizzle which will throw an error (gh-1682)
63+
try {
64+
jQuery( "#" );
65+
} catch ( e ) {
66+
ok( true, "Threw an error on #id with no id" );
67+
}
68+
6469
// can actually yield more than one, when iframes are included, the window is an array as well
6570
equal( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
6671

0 commit comments

Comments
 (0)