Skip to content

Commit a0955ce

Browse files
committed
Core: jQuery("#") is bad syntax
Fixes jquery#85 Closes jquery#95
1 parent bede1e6 commit a0955ce

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/core.js

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jQuery.fn.init = function( selector, context, rootjQuery ) {
1919
if ( match[ 3 ] ) {
2020
migrateWarn("$(html) HTML text after last tag is ignored");
2121
}
22+
2223
// Consistently reject any HTML-like string starting with a hash (#9521)
2324
// Note that this may break jQuery 1.6.x code that otherwise would work.
2425
if ( match[ 0 ].charAt( 0 ) === "#" ) {
@@ -37,6 +38,12 @@ jQuery.fn.init = function( selector, context, rootjQuery ) {
3738
}
3839
}
3940

41+
// jQuery( "#" ) is a bogus ID selector, but it returned an empty set before jQuery 3.0
42+
if ( selector === "#" ) {
43+
migrateWarn( "jQuery( '#' ) is not a valid selector" );
44+
selector = [];
45+
}
46+
4047
ret = oldInit.apply( this, arguments );
4148

4249
// Fill in selector and context properties so .live() works

test/core.js

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ test( "jQuery(html) loose rules", function() {
4747
}
4848
});
4949

50+
test( "jQuery( '#' )", function() {
51+
expect( 2 );
52+
53+
expectWarning( "Selector, through the jQuery constructor, nothing but hash", function() {
54+
var set = jQuery( "#" );
55+
equal( set.length, 0, "empty set" );
56+
});
57+
});
58+
5059
test( "selector state", function() {
5160
expect( 18 );
5261

0 commit comments

Comments
 (0)