Commit 93f95c96 authored by Dave Methvin's avatar Dave Methvin

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)
parent 503e5456
...@@ -14,7 +14,8 @@ var rootjQuery, ...@@ -14,7 +14,8 @@ var rootjQuery,
// A simple way to check for HTML strings // A simple way to check for HTML strings
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521) // Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
// Strict HTML recognition (#11290: must start with <) // 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 ) { init = jQuery.fn.init = function( selector, context ) {
var match, elem; var match, elem;
......
...@@ -57,10 +57,15 @@ test("jQuery()", function() { ...@@ -57,10 +57,15 @@ test("jQuery()", function() {
equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" ); equal( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" ); equal( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
equal( jQuery("").length, 0, "jQuery('') === jQuery([])" ); equal( jQuery("").length, 0, "jQuery('') === jQuery([])" );
equal( jQuery("#").length, 0, "jQuery('#') === jQuery([])" );
equal( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" ); 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 // 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)" ); equal( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment