Skip to content

Core: jQuery("#") is bad syntax #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) === "#" ) {
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down