X-Git-Url: http://git.asbjorn.biz/?p=jquery.git;a=blobdiff_plain;f=src%2Fcore.js;h=520db45ed6cc179ba99b40d57725e3abe17ddcd9;hp=eb31b2a40b478a1001b2314b8ff255ae885dd8b9;hb=9d306bd73bb47562cd52f0fc4cc158c534cfdfdf;hpb=f2b0c77dc84d3019db96de0c060207a5063f2055 diff --git a/src/core.js b/src/core.js index eb31b2a..520db45 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) ) { - // 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 ); + // HANDLE: $(html) -> $(array) + if ( match[1] ) { + 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 ); + + 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.clone(ret.fragment) : 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 ); + } + + // Otherwise, we inject the element directly into the jQuery object + this.length = 1; + this[0] = elem; + } - return jQuery.merge( this, selector ); + this.context = document; + this.selector = selector; + return this; + } // HANDLE: $(expr, $(...)) } else if ( !context || context.jquery ) { @@ -208,12 +247,14 @@ jQuery.fn = jQuery.prototype = { return jQuery.each( this, callback, args ); }, - ready: function() { + ready: function( fn ) { // Attach the listeners jQuery.bindReady(); - // Change ready & apply - return ( jQuery.fn.ready = readyList.done ).apply( this , arguments ); + // Add the callback + readyList.done( fn ); + + return this; }, eq: function( i ) { @@ -360,7 +401,7 @@ jQuery.extend({ } // If there are functions bound, to execute - readyList.resolveWith( document , [ jQuery ] ); + readyList.resolveWith( document, [ jQuery ] ); // Trigger any bound ready events if ( jQuery.fn.trigger ) { @@ -758,7 +799,6 @@ jQuery.extend({ // Create a simple deferred (one callbacks list) _Deferred: function() { - var // callbacks list callbacks = [], // stored [ context , args ] @@ -771,53 +811,45 @@ jQuery.extend({ deferred = { // done( f1, f2, ...) - done: function () { - - if ( ! cancelled ) { - + done: function() { + if ( !cancelled ) { var args = arguments, i, length, elem, type, _fired; - if ( fired ) { _fired = fired; fired = 0; } - - for ( i = 0, length = args.length ; i < length ; i++ ) { + for ( i = 0, length = args.length; i < length; i++ ) { elem = args[ i ]; type = jQuery.type( elem ); if ( type === "array" ) { - deferred.done.apply( deferred , elem ); + deferred.done.apply( deferred, elem ); } else if ( type === "function" ) { callbacks.push( elem ); } } - if ( _fired ) { - deferred.resolveWith( _fired[ 0 ] , _fired[ 1 ] ); + deferred.resolveWith( _fired[ 0 ], _fired[ 1 ] ); } } - return this; }, // resolve with given context and args - resolveWith: function( context , args ) { - if ( ! cancelled && ! fired && ! firing ) { - + resolveWith: function( context, args ) { + if ( !cancelled && !fired && !firing ) { firing = 1; - try { while( callbacks[ 0 ] ) { - callbacks.shift().apply( context , args ); + callbacks.shift().apply( context, args ); } } finally { - fired = [ context , args ]; + fired = [ context, args ]; firing = 0; } } @@ -826,7 +858,7 @@ jQuery.extend({ // resolve with this as context and given arguments resolve: function() { - deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments ); + deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this, arguments ); return this; }, @@ -847,17 +879,13 @@ jQuery.extend({ }, // Full fledged deferred (two callbacks list) - // Typical success/error system Deferred: function( func ) { - var deferred = jQuery._Deferred(), failDeferred = jQuery._Deferred(), promise; - // Add errorDeferred methods, then and promise - jQuery.extend( deferred , { - - then: function( doneCallbacks , failCallbacks ) { + jQuery.extend( deferred, { + then: function( doneCallbacks, failCallbacks ) { deferred.done( doneCallbacks ).fail( failCallbacks ); return this; }, @@ -867,8 +895,7 @@ jQuery.extend({ isRejected: failDeferred.isResolved, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object - // (i is used internally) - promise: function( obj , i ) { + promise: function( obj , i /* internal */ ) { if ( obj == null ) { if ( promise ) { return promise; @@ -881,20 +908,15 @@ jQuery.extend({ } return obj; } - } ); - // Make sure only one callback list will be used - deferred.then( failDeferred.cancel , deferred.cancel ); - + deferred.then( failDeferred.cancel, deferred.cancel ); // Unexpose cancel delete deferred.cancel; - // Call given func if any if ( func ) { - func.call( deferred , deferred ); + func.call( deferred, deferred ); } - return deferred; }, @@ -911,17 +933,14 @@ jQuery.extend({ if ( length > 1 ) { resolveArray = new Array( length ); jQuery.each( args, function( index, element, args ) { - jQuery.when( element ).done( function( value ) { + jQuery.when( element ).then( function( value ) { args = arguments; - resolveArray[ index ] = args.length > 1 ? slice.call( args , 0 ) : value; + resolveArray[ index ] = args.length > 1 ? slice.call( args, 0 ) : value; if( ! --length ) { deferred.resolveWith( promise, resolveArray ); } - }).fail( function() { - deferred.rejectWith( promise, arguments ); - }); - return !deferred.isRejected(); - }); + }, deferred.reject ); + } ); } else if ( deferred !== object ) { deferred.resolve( object ); }