Re-worked the logic for where .selector and .context are added for ID selectors (especially ones that aren't found). Fixes jQuery bug #3833.

This commit is contained in:
John Resig 2009-01-20 16:00:48 +00:00
parent 7647cebc39
commit 28a1f02223
2 changed files with 14 additions and 14 deletions

View file

@ -47,21 +47,17 @@ jQuery.fn = jQuery.prototype = {
else { else {
var elem = document.getElementById( match[3] ); var elem = document.getElementById( match[3] );
// Make sure an element was located
if ( elem ){
// Handle the case where IE and Opera return items // Handle the case where IE and Opera return items
// by name instead of ID // by name instead of ID
if ( elem.id != match[3] ) if ( elem && elem.id != match[3] )
return jQuery().find( selector ); return jQuery().find( selector );
// Otherwise, we inject the element directly into the jQuery object // Otherwise, we inject the element directly into the jQuery object
var ret = jQuery( elem ); var ret = jQuery( elem || [] );
ret.context = document; ret.context = document;
ret.selector = selector; ret.selector = selector;
return ret; return ret;
} }
selector = [];
}
// HANDLE: $(expr, [context]) // HANDLE: $(expr, [context])
// (which is just equivalent to: $(content).find(expr) // (which is just equivalent to: $(content).find(expr)

View file

@ -53,7 +53,7 @@ test("jQuery()", function() {
}); });
test("selector state", function() { test("selector state", function() {
expect(28); expect(30);
var test; var test;
@ -73,6 +73,10 @@ test("selector state", function() {
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document, "#main Context" ); equals( test.context, document, "#main Context" );
test = jQuery("#notfoundnono");
equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
equals( test.context, document, "#notfoundnono Context" );
test = jQuery("#main", document); test = jQuery("#main", document);
equals( test.selector, "#main", "#main Selector" ); equals( test.selector, "#main", "#main Selector" );
equals( test.context, document, "#main Context" ); equals( test.context, document, "#main Context" );