From 1fa1452f8b1c6d632b76478dccc92ba251e44f27 Mon Sep 17 00:00:00 2001 From: Dave Methvin Date: Thu, 12 Mar 2015 18:41:28 -0400 Subject: [PATCH] Core: jQuery("#") is bad syntax Closes #85 --- src/core.js | 7 +++++++ test/core.js | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/core.js b/src/core.js index 12235f03..388dbadd 100644 --- a/src/core.js +++ b/src/core.js @@ -19,6 +19,7 @@ jQuery.fn.init = function( selector, context, rootjQuery ) { if ( match[ 3 ] ) { migrateWarn("$(html) HTML text after last tag is ignored"); } + // Consistently reject any HTML-like string starting with a hash (#9521) // Note that this may break jQuery 1.6.x code that otherwise would work. if ( match[ 0 ].charAt( 0 ) === "#" ) { @@ -37,6 +38,12 @@ jQuery.fn.init = function( selector, context, rootjQuery ) { } } + // jQuery( "#" ) is a bogus ID selector, but it returned an empty set before jQuery 3.0 + if ( selector === "#" ) { + migrateWarn( "jQuery( '#' ) is not a valid selector" ); + selector = []; + } + ret = oldInit.apply( this, arguments ); // Fill in selector and context properties so .live() works diff --git a/test/core.js b/test/core.js index 70d35d35..dfc9613e 100644 --- a/test/core.js +++ b/test/core.js @@ -47,6 +47,15 @@ test( "jQuery(html) loose rules", function() { } }); +test( "jQuery( '#' )", function() { + expect( 2 ); + + expectWarning( "Selector, through the jQuery constructor, nothing but hash", function() { + var set = jQuery( "#" ); + equal( set.length, 0, "empty set" ); + }); +}); + test( "selector state", function() { expect( 18 );