aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorjeresig <jeresig@gmail.com>2011-01-20 11:58:44 -0500
committerjeresig <jeresig@gmail.com>2011-01-20 11:58:44 -0500
commite4d01688932960896419dc52ffd691cc4f9b3029 (patch)
tree968ea8ffd6f71d774fbab9583a3f385a8061a0cf /src
parent0f6c7830ce5bdd31588dccdc40b6254fb4a9100f (diff)
downloadjquery-e4d01688932960896419dc52ffd691cc4f9b3029.tar.gz
jquery-e4d01688932960896419dc52ffd691cc4f9b3029.zip
Bring jQuery('#id') and jQuery('body') logic back into core (while leaving it in Sizzle at the same time). Was causing too much of a performance hit to leave it all to Sizzle.
Diffstat (limited to 'src')
-rw-r--r--src/core.js75
1 files changed, 57 insertions, 18 deletions
diff --git a/src/core.js b/src/core.js
index f35e33c3e..6d333c59e 100644
--- a/src/core.js
+++ b/src/core.js
@@ -15,8 +15,9 @@ var jQuery = function( selector, context ) {
// A central reference to the root jQuery(document)
rootjQuery,
- // A simple way to check for HTML strings
- quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$/,
+ // A simple way to check for HTML strings or ID strings
+ // (both of which we optimize for)
+ quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
// Check if a string has a non-whitespace character in it
rnotwhite = /\S/,
@@ -92,32 +93,70 @@ jQuery.fn = jQuery.prototype = {
return this;
}
+ // The body element only exists once, optimize finding it
+ if ( selector === "body" && !context && document.body ) {
+ this.context = document;
+ this[0] = document.body;
+ this.selector = "body";
+ this.length = 1;
+ return this;
+ }
+
// Handle HTML strings
if ( typeof selector === "string" ) {
- // Are we dealing with HTML string
- if ( (match = quickExpr.exec( selector )) ) {
- context = context instanceof jQuery ? context[0] : context;
- doc = (context ? context.ownerDocument || context : document);
+ // Are we dealing with HTML string or an ID?
+ match = quickExpr.exec( selector );
+
+ // Verify a match, and that no context was specified for #id
+ if ( match && (match[1] || !context) ) {
+
+ // HANDLE: $(html) -> $(array)
+ if ( match[1] ) {
+ context = context instanceof jQuery ? context[0] : context;
+ doc = (context ? context.ownerDocument || context : document);
+
+ // If a single string is passed in and it's a single tag
+ // just do a createElement and skip the rest
+ ret = rsingleTag.exec( selector );
- // If a single string is passed in and it's a single tag
- // just do a createElement and skip the rest
- ret = rsingleTag.exec( selector );
+ if ( ret ) {
+ if ( jQuery.isPlainObject( context ) ) {
+ selector = [ document.createElement( ret[1] ) ];
+ jQuery.fn.attr.call( selector, context, true );
- if ( ret ) {
- if ( jQuery.isPlainObject( context ) ) {
- selector = [ document.createElement( ret[1] ) ];
- jQuery.fn.attr.call( selector, context, true );
+ } else {
+ selector = [ doc.createElement( ret[1] ) ];
+ }
} else {
- selector = [ doc.createElement( ret[1] ) ];
+ ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
+ selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
}
+ return jQuery.merge( this, selector );
+
+ // HANDLE: $("#id")
} else {
- ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
- selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
- }
+ elem = document.getElementById( match[2] );
+
+ // Check parentNode to catch when Blackberry 4.6 returns
+ // nodes that are no longer in the document #6963
+ if ( elem && elem.parentNode ) {
+ // Handle the case where IE and Opera return items
+ // by name instead of ID
+ if ( elem.id !== match[2] ) {
+ return rootjQuery.find( selector );
+ }
- return jQuery.merge( this, selector );
+ // Otherwise, we inject the element directly into the jQuery object
+ this.length = 1;
+ this[0] = elem;
+ }
+
+ this.context = document;
+ this.selector = selector;
+ return this;
+ }
// HANDLE: $(expr, $(...))
} else if ( !context || context.jquery ) {