X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fcore.js;h=eb31b2a40b478a1001b2314b8ff255ae885dd8b9;hb=f2b0c77dc84d3019db96de0c060207a5063f2055;hp=77f9fcebd17110b06235c4e054f59fbc9674435c;hpb=47e4cc1a0f3f26cf86d485fd53066503c4272e6b;p=jquery.git diff --git a/src/core.js b/src/core.js index 77f9fce..eb31b2a 100644 --- a/src/core.js +++ b/src/core.js @@ -3,7 +3,7 @@ var jQuery = (function() { // Define a local copy of jQuery var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' - return new jQuery.fn.init( selector, context ); + return new jQuery.fn.init( selector, context, rootjQuery ); }, // Map over jQuery in case of overwrite @@ -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, @@ -78,7 +76,8 @@ var jQuery = function( selector, context ) { class2type = {}; jQuery.fn = jQuery.prototype = { - init: function( selector, context ) { + constructor: jQuery, + init: function( selector, context, rootjQuery ) { var match, elem, ret, doc; // Handle $(""), $(null), or $(undefined) @@ -93,75 +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) ) { + // Are we dealing with HTML string + if ( (match = quickExpr.exec( selector )) ) { + context = context instanceof jQuery ? context[0] : context; + doc = (context ? context.ownerDocument || context : document); - // HANDLE: $(html) -> $(array) - if ( match[1] ) { - 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 ); - - } 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 ? ret.fragment.cloneNode(true) : 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, $(...)) @@ -171,7 +126,7 @@ jQuery.fn = jQuery.prototype = { // HANDLE: $(expr, context) // (which is just equivalent to: $(context).find(expr) } else { - return jQuery( context ).find( selector ); + return this.constructor( context ).find( selector ); } // HANDLE: $(function) @@ -222,7 +177,7 @@ jQuery.fn = jQuery.prototype = { // (returning the new matched element set) pushStack: function( elems, name, selector ) { // Build a new jQuery matched element set - var ret = jQuery(); + var ret = this.constructor(); if ( jQuery.isArray( elems ) ) { push.apply( ret, elems ); @@ -258,7 +213,7 @@ jQuery.fn = jQuery.prototype = { jQuery.bindReady(); // Change ready & apply - return ( jQuery.fn.ready = readyList.complete ).apply( this , arguments ); + return ( jQuery.fn.ready = readyList.done ).apply( this , arguments ); }, eq: function( i ) { @@ -287,7 +242,7 @@ jQuery.fn = jQuery.prototype = { }, end: function() { - return this.prevObject || jQuery(null); + return this.prevObject || this.constructor(null); }, // For internal use only. @@ -405,7 +360,7 @@ jQuery.extend({ } // If there are functions bound, to execute - readyList.fire( document , [ jQuery ] ); + readyList.resolveWith( document , [ jQuery ] ); // Trigger any bound ready events if ( jQuery.fn.trigger ) { @@ -578,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; @@ -815,8 +770,8 @@ jQuery.extend({ // the deferred itself deferred = { - // complete( f1, f2, ...) - complete: function () { + // done( f1, f2, ...) + done: function () { if ( ! cancelled ) { @@ -836,14 +791,14 @@ jQuery.extend({ elem = args[ i ]; type = jQuery.type( elem ); if ( type === "array" ) { - deferred.complete.apply( deferred , elem ); + deferred.done.apply( deferred , elem ); } else if ( type === "function" ) { callbacks.push( elem ); } } if ( _fired ) { - deferred.fire( _fired[ 0 ] , _fired[ 1 ] ); + deferred.resolveWith( _fired[ 0 ] , _fired[ 1 ] ); } } @@ -851,7 +806,7 @@ jQuery.extend({ }, // resolve with given context and args - fire: function( context , args ) { + resolveWith: function( context , args ) { if ( ! cancelled && ! fired && ! firing ) { firing = 1; @@ -871,7 +826,7 @@ jQuery.extend({ // resolve with this as context and given arguments resolve: function() { - deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments ); + deferred.resolveWith( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments ); return this; }, @@ -896,29 +851,34 @@ jQuery.extend({ Deferred: function( func ) { var deferred = jQuery._Deferred(), - failDeferred = jQuery._Deferred(); + failDeferred = jQuery._Deferred(), + promise; - // Add errorDeferred methods and redefine cancel + // Add errorDeferred methods, then and promise jQuery.extend( deferred , { - then: function( completeCallbacks , failCallbacks ) { - deferred.complete( completeCallbacks ).fail( failCallbacks ); + then: function( doneCallbacks , failCallbacks ) { + deferred.done( doneCallbacks ).fail( failCallbacks ); return this; }, - fail: failDeferred.complete, - fireReject: failDeferred.fire, + fail: failDeferred.done, + rejectWith: failDeferred.resolveWith, reject: failDeferred.resolve, isRejected: failDeferred.isResolved, // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object - promise: function( obj ) { - obj = obj || {}; - jQuery.each( "then complete fail isResolved isRejected".split( " " ) , function( _ , method ) { - obj[ method ] = deferred[ method ]; - }); - obj.promise = function() { - return obj; - }; + // (i is used internally) + promise: function( obj , i ) { + if ( obj == null ) { + if ( promise ) { + return promise; + } + promise = obj = {}; + } + i = promiseMethods.length; + while( i-- ) { + obj[ promiseMethods[ i ] ] = deferred[ promiseMethods[ i ] ]; + } return obj; } @@ -940,10 +900,32 @@ jQuery.extend({ // Deferred helper when: function( object ) { - object = object && jQuery.isFunction( object.promise ) ? - object : - jQuery.Deferred().resolve( object ); - return object.promise(); + var args = arguments, + length = args.length, + deferred = length <= 1 && object && jQuery.isFunction( object.promise ) ? + object : + jQuery.Deferred(), + promise = deferred.promise(), + resolveArray; + + if ( length > 1 ) { + resolveArray = new Array( length ); + jQuery.each( args, function( index, element, args ) { + jQuery.when( element ).done( function( value ) { + args = arguments; + 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(); + }); + } else if ( deferred !== object ) { + deferred.resolve( object ); + } + return promise; }, // Use of jQuery.browser is frowned upon. @@ -960,6 +942,25 @@ jQuery.extend({ return { browser: match[1] || "", version: match[2] || "0" }; }, + subclass: function(){ + function jQuerySubclass( selector, context ) { + return new jQuerySubclass.fn.init( selector, context ); + } + jQuerySubclass.superclass = this; + jQuerySubclass.fn = jQuerySubclass.prototype = this(); + jQuerySubclass.fn.constructor = jQuerySubclass; + jQuerySubclass.subclass = this.subclass; + jQuerySubclass.fn.init = function init( selector, context ) { + if (context && context instanceof jQuery && !(context instanceof jQuerySubclass)){ + context = jQuerySubclass(context); + } + return jQuery.fn.init.call( this, selector, context, rootjQuerySubclass ); + }; + jQuerySubclass.fn.init.prototype = jQuerySubclass.fn; + var rootjQuerySubclass = jQuerySubclass(document); + return jQuerySubclass; + }, + browser: {} }); @@ -988,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]+$/; }