Moved unload abort code so that the event is only bound if the xhr transport is used...
[jquery.git] / src / core.js
index 57a42d0..b9e6d81 100644 (file)
@@ -56,10 +56,10 @@ var jQuery = function( selector, context ) {
 
        // For matching the engine and version of the browser
        browserMatch,
-       
+
        // Has the ready events already been bound?
        readyBound = false,
-       
+
        // The deferred used on DOM ready
        readyList,
 
@@ -73,12 +73,9 @@ var jQuery = function( selector, context ) {
        slice = Array.prototype.slice,
        trim = String.prototype.trim,
        indexOf = Array.prototype.indexOf,
-       
+
        // [[Class]] -> type pairs
-       class2type = {},
-       
-       // Marker for deferred
-       deferredMarker = [];
+       class2type = {};
 
 jQuery.fn = jQuery.prototype = {
        init: function( selector, context ) {
@@ -95,7 +92,7 @@ jQuery.fn = jQuery.prototype = {
                        this.length = 1;
                        return this;
                }
-               
+
                // The body element only exists once, optimize finding it
                if ( selector === "body" && !context && document.body ) {
                        this.context = document;
@@ -134,9 +131,9 @@ jQuery.fn = jQuery.prototype = {
                                                ret = jQuery.buildFragment( [ match[1] ], [ doc ] );
                                                selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
                                        }
-                                       
+
                                        return jQuery.merge( this, selector );
-                                       
+
                                // HANDLE: $("#id")
                                } else {
                                        elem = document.getElementById( match[2] );
@@ -229,7 +226,7 @@ jQuery.fn = jQuery.prototype = {
 
                if ( jQuery.isArray( elems ) ) {
                        push.apply( ret, elems );
-               
+
                } else {
                        jQuery.merge( ret, elems );
                }
@@ -255,15 +252,15 @@ jQuery.fn = jQuery.prototype = {
        each: function( callback, args ) {
                return jQuery.each( this, callback, args );
        },
-       
+
        ready: function() {
                // Attach the listeners
                jQuery.bindReady();
-               
+
                // Change ready & apply
-               return ( jQuery.fn.ready = readyList.then ).apply( this , arguments );
+               return ( jQuery.fn.ready = readyList.done ).apply( this , arguments );
        },
-       
+
        eq: function( i ) {
                return i === -1 ?
                        this.slice( i ) :
@@ -288,7 +285,7 @@ jQuery.fn = jQuery.prototype = {
                        return callback.call( elem, i, elem );
                }));
        },
-       
+
        end: function() {
                return this.prevObject || jQuery(null);
        },
@@ -377,14 +374,14 @@ jQuery.extend({
 
                return jQuery;
        },
-       
+
        // Is the DOM ready to be used? Set to true once it occurs.
        isReady: false,
 
        // A counter to track how many items to wait for before
        // the ready event fires. See #6781
        readyWait: 1,
-       
+
        // Handle when the DOM is ready
        ready: function( wait ) {
                // A third-party is pushing the ready event forwards
@@ -409,14 +406,14 @@ jQuery.extend({
 
                        // If there are functions bound, to execute
                        readyList.fire( document , [ jQuery ] );
-                       
+
                        // Trigger any bound ready events
                        if ( jQuery.fn.trigger ) {
                                jQuery( document ).trigger( "ready" ).unbind( "ready" );
                        }
                }
        },
-       
+
        bindReady: function() {
                if ( readyBound ) {
                        return;
@@ -435,7 +432,7 @@ jQuery.extend({
                if ( document.addEventListener ) {
                        // Use the handy event callback
                        document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
-                       
+
                        // A fallback to window.onload, that will always work
                        window.addEventListener( "load", jQuery.ready, false );
 
@@ -444,7 +441,7 @@ jQuery.extend({
                        // ensure firing before onload,
                        // maybe late but safe also for iframes
                        document.attachEvent("onreadystatechange", DOMContentLoaded);
-                       
+
                        // A fallback to window.onload, that will always work
                        window.attachEvent( "onload", jQuery.ready );
 
@@ -495,20 +492,20 @@ jQuery.extend({
                if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
                        return false;
                }
-               
+
                // Not own constructor property must be Object
                if ( obj.constructor &&
                        !hasOwn.call(obj, "constructor") &&
                        !hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
                        return false;
                }
-               
+
                // Own properties are enumerated firstly, so to speed up,
                // if last one is own, then all properties are own.
-       
+
                var key;
                for ( key in obj ) {}
-               
+
                return key === undefined || hasOwn.call( obj, key );
        },
 
@@ -518,11 +515,11 @@ jQuery.extend({
                }
                return true;
        },
-       
+
        error: function( msg ) {
                throw msg;
        },
-       
+
        parseJSON: function( data ) {
                if ( typeof data !== "string" || !data ) {
                        return null;
@@ -530,7 +527,7 @@ jQuery.extend({
 
                // Make sure leading/trailing whitespace is removed (IE can't handle it)
                data = jQuery.trim( data );
-               
+
                // Make sure the incoming data is actual JSON
                // Logic borrowed from http://json.org/json2.js
                if ( rvalidchars.test(data.replace(rvalidescape, "@")
@@ -546,7 +543,7 @@ jQuery.extend({
                        jQuery.error( "Invalid JSON: " + data );
                }
        },
-       
+
        // Cross-browser xml parsing
        // (xml & tmp used internally)
        parseXML: function( data , xml , tmp ) {
@@ -559,13 +556,13 @@ jQuery.extend({
                        xml.async = "false";
                        xml.loadXML( data );
                }
-               
+
                tmp = xml.documentElement;
-               
+
                if ( ! tmp || ! tmp.nodeName || tmp.nodeName === "parsererror" ) {
                        jQuery.error( "Invalid XML: " + data );
                }
-               
+
                return xml;
        },
 
@@ -694,7 +691,7 @@ jQuery.extend({
                        for ( var l = second.length; j < l; j++ ) {
                                first[ i++ ] = second[ j ];
                        }
-               
+
                } else {
                        while ( second[j] !== undefined ) {
                                first[ i++ ] = second[ j++ ];
@@ -775,7 +772,7 @@ jQuery.extend({
        // The value/s can be optionally by executed if its a function
        access: function( elems, key, value, exec, fn, pass ) {
                var length = elems.length;
-       
+
                // Setting many attributes
                if ( typeof key === "object" ) {
                        for ( var k in key ) {
@@ -783,19 +780,19 @@ jQuery.extend({
                        }
                        return elems;
                }
-       
+
                // Setting one attribute
                if ( value !== undefined ) {
                        // Optionally, function values get executed if exec is true
                        exec = !pass && exec && jQuery.isFunction(value);
-               
+
                        for ( var i = 0; i < length; i++ ) {
                                fn( elems[i], key, exec ? value.call( elems[i], i, fn( elems[i], key ) ) : value, pass );
                        }
-               
+
                        return elems;
                }
-       
+
                // Getting an attribute
                return length ? fn( elems[0], key ) : undefined;
        },
@@ -803,10 +800,10 @@ jQuery.extend({
        now: function() {
                return (new Date()).getTime();
        },
-       
+
        // Create a simple deferred (one callbacks list)
        _Deferred: function() {
-               
+
                var // callbacks list
                        callbacks = [],
                        // stored [ context , args ]
@@ -817,48 +814,48 @@ jQuery.extend({
                        cancelled,
                        // the deferred itself
                        deferred  = {
-                               
-                               // then( f1, f2, ...)
-                               then: function () {
-                                       
+
+                               // done( f1, f2, ...)
+                               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++ ) {
                                                        elem = args[ i ];
                                                        type = jQuery.type( elem );
                                                        if ( type === "array" ) {
-                                                               deferred.then.apply( deferred , elem );
+                                                               deferred.done.apply( deferred , elem );
                                                        } else if ( type === "function" ) {
                                                                callbacks.push( elem );
                                                        }
                                                }
-                                               
+
                                                if ( _fired ) {
                                                        deferred.fire( _fired[ 0 ] , _fired[ 1 ] );
                                                }
                                        }
-                                       
+
                                        return this;
                                },
-                               
+
                                // resolve with given context and args
                                fire: function( context , args ) {
                                        if ( ! cancelled && ! fired && ! firing ) {
-                                               
+
                                                firing = 1;
-                                               
+
                                                try {
                                                        while( callbacks[ 0 ] ) {
                                                                callbacks.shift().apply( context , args );
@@ -871,86 +868,82 @@ jQuery.extend({
                                        }
                                        return this;
                                },
-                               
+
                                // resolve with this as context and given arguments
                                resolve: function() {
-                                       deferred.fire( this , arguments );
+                                       deferred.fire( jQuery.isFunction( this.promise ) ? this.promise() : this , arguments );
                                        return this;
                                },
-                               
+
                                // Has this deferred been resolved?
                                isResolved: function() {
                                        return !!( firing || fired );
                                },
-                               
+
                                // Cancel
                                cancel: function() {
                                        cancelled = 1;
                                        callbacks = [];
                                        return this;
-                               },
-                               
-                               // Has this deferred been cancelled?
-                               isCancelled: function() {
-                                       return !!cancelled;
                                }
                        };
-               
-               // Add the deferred marker
-               deferred.then._ = deferredMarker;
-               
+
                return deferred;
        },
-       
+
        // Full fledged deferred (two callbacks list)
        // Typical success/error system
        Deferred: function( func ) {
-               
-               var errorDeferred = jQuery._Deferred(),
-                       deferred = jQuery._Deferred(),
-                       successCancel = deferred.cancel;
-                       
-               // Add errorDeferred methods and redefine cancel                        
+
+               var deferred = jQuery._Deferred(),
+                       failDeferred = jQuery._Deferred();
+
+               // Add errorDeferred methods and redefine cancel
                jQuery.extend( deferred , {
 
-                               fail: errorDeferred.then,
-                               fireReject: errorDeferred.fire,
-                               reject: errorDeferred.resolve,
-                               isRejected: errorDeferred.isResolved
+                       then: function( doneCallbacks , failCallbacks ) {
+                               deferred.done( doneCallbacks ).fail( failCallbacks );
+                               return this;
+                       },
+                       fail: failDeferred.done,
+                       fireReject: failDeferred.fire,
+                       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 done fail isResolved isRejected".split( " " ) , function( _ , method ) {
+                                       obj[ method ] = deferred[ method ];
+                               });
+                               obj.promise = function() {
+                                       return obj;
+                               };
+                               return obj;
+                       }
 
                } );
-               
-               // Remove cancel related
-               delete deferred.cancel;
-               delete deferred.isCancelled;
-               
+
                // Make sure only one callback list will be used
-               deferred.then( errorDeferred.cancel ).fail( successCancel );
-               
+               deferred.then( failDeferred.cancel , deferred.cancel );
+
+               // Unexpose cancel
+               delete deferred.cancel;
+
                // Call given func if any
                if ( func ) {
                        func.call( deferred , deferred );
                }
-               
+
                return deferred;
        },
 
-       // Check if an object is a deferred
-       isDeferred: function( object , method ) {
-               method = method || "then";
-               return !!( object && object[ method ] && object[ method ]._ === deferredMarker );
-       },
-       
        // Deferred helper
-       when: function( object , method ) {
-               method = method || "then";
-               object = jQuery.isDeferred( object , method ) ?
+       when: function( object ) {
+               object = object && jQuery.isFunction( object.promise ) ?
                        object :
                        jQuery.Deferred().resolve( object );
-               object.fail = object.fail || function() { return this; };
-               object[ method ] = object[ method ] || object.then;
-               object.then = object.then || object[ method ];
-               return object;
+               return object.promise();
        },
 
        // Use of jQuery.browser is frowned upon.
@@ -971,9 +964,7 @@ jQuery.extend({
 });
 
 // Create readyList deferred
-// also force $.fn.ready to be recognized as a defer
 readyList = jQuery._Deferred();
-jQuery.fn.ready._ = deferredMarker;
 
 // Populate the class2type map
 jQuery.each("Boolean Number String Function Array Date RegExp Object".split(" "), function(i, name) {