Fix typo in regex tweak from previous commit.
[jquery.git] / src / core.js
index 25ef22d..f35e33c 100644 (file)
@@ -15,16 +15,11 @@ var jQuery = function( selector, context ) {
        // A central reference to the root jQuery(document)
        rootjQuery,
 
-       // A simple way to check for HTML strings or ID strings
-       // (both of which we optimize for)
-       quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]+)$)/,
-
-       // Is it a simple selector
-       isSimple = /^.[^:#\[\.,]*$/,
+       // A simple way to check for HTML strings
+       quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$/,
 
        // Check if a string has a non-whitespace character in it
        rnotwhite = /\S/,
-       rwhite = /\s/,
 
        // Used for trimming whitespace
        trimLeft = /^\s+/,
@@ -63,6 +58,9 @@ var jQuery = function( selector, context ) {
        // The deferred used on DOM ready
        readyList,
 
+       // Promise methods
+       promiseMethods = "then done fail isResolved isRejected promise".split( " " ),
+
        // The ready event handler
        DOMContentLoaded,
 
@@ -94,76 +92,31 @@ 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 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 );
+                       // Are we dealing with HTML string
+                       if ( (match = quickExpr.exec( selector )) ) {
+                               context = context instanceof jQuery ? context[0] : context;
+                               doc = (context ? context.ownerDocument || context : document);
 
-                                       if ( ret ) {
-                                               if ( jQuery.isPlainObject( context ) ) {
-                                                       selector = [ document.createElement( ret[1] ) ];
-                                                       jQuery.fn.attr.call( selector, context, true );
+                               // 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 );
 
-                                               } else {
-                                                       selector = [ doc.createElement( ret[1] ) ];
-                                               }
+                               if ( ret ) {
+                                       if ( jQuery.isPlainObject( context ) ) {
+                                               selector = [ document.createElement( ret[1] ) ];
+                                               jQuery.fn.attr.call( selector, context, true );
 
                                        } else {
-                                               ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
-                                               selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
+                                               selector = [ doc.createElement( ret[1] ) ];
                                        }
 
-                                       return jQuery.merge( this, selector );
-
-                               // HANDLE: $("#id")
                                } else {
-                                       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 );
-                                               }
-
-                                               // Otherwise, we inject the element directly into the jQuery object
-                                               this.length = 1;
-                                               this[0] = elem;
-                                       }
-
-                                       this.context = document;
-                                       this.selector = selector;
-                                       return this;
+                                       ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
+                                       selector = (ret.cacheable ? jQuery(ret.fragment).clone()[0] : ret.fragment).childNodes;
                                }
 
-                       // HANDLE: $("TAG")
-                       } else if ( !context && !rnonword.test( selector ) ) {
-                               this.selector = selector;
-                               this.context = document;
-                               selector = document.getElementsByTagName( selector );
                                return jQuery.merge( this, selector );
 
                        // HANDLE: $(expr, $(...))
@@ -580,7 +533,7 @@ jQuery.extend({
 
                        script.type = "text/javascript";
 
-                       if ( jQuery.support.scriptEval ) {
+                       if ( jQuery.support.scriptEval() ) {
                                script.appendChild( document.createTextNode( data ) );
                        } else {
                                script.text = data;
@@ -914,16 +867,18 @@ jQuery.extend({
                        isRejected: failDeferred.isResolved,
                        // Get a promise for this deferred
                        // If obj is provided, the promise aspect is added to the object
-                       promise: function( obj ) {
+                       // (i is used internally)
+                       promise: function( obj , i ) {
                                if ( obj == null ) {
                                        if ( promise ) {
                                                return promise;
                                        }
                                        promise = obj = {};
                                }
-                               jQuery.each( "then done fail isResolved isRejected promise".split( " " ) , function( _ , method ) {
-                                       obj[ method ] = deferred[ method ];
-                               });
+                               i = promiseMethods.length;
+                               while( i-- ) {
+                                       obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ];
+                               }
                                return obj;
                        }
 
@@ -1034,9 +989,8 @@ if ( indexOf ) {
        };
 }
 
-// Verify that \s matches non-breaking spaces
-// (IE fails on this test)
-if ( !rwhite.test( "\xA0" ) ) {
+// IE doesn't match non-breaking spaces with \s
+if ( rnotwhite.test( "\xA0" ) ) {
        trimLeft = /^[\s\xA0]+/;
        trimRight = /[\s\xA0]+$/;
 }