diff options
author | Oleg Gaidarenko <markelog@gmail.com> | 2015-08-16 09:59:58 +0300 |
---|---|---|
committer | Oleg Gaidarenko <markelog@gmail.com> | 2015-09-07 20:03:50 +0300 |
commit | 10fdad742a2a6aa9f0e00b3e04fc5264797c53c7 (patch) | |
tree | 6b6d7b1375ff88f4bebbfa0703f7b595eb485df9 /src/ajax.js | |
parent | 7aa46e0df8a673e6b00550bbbbed21eed50108b7 (diff) | |
download | jquery-10fdad742a2a6aa9f0e00b3e04fc5264797c53c7.tar.gz jquery-10fdad742a2a6aa9f0e00b3e04fc5264797c53c7.zip |
Build: Update jscs and lint files
Fixes gh-2056
Diffstat (limited to 'src/ajax.js')
-rw-r--r-- | src/ajax.js | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/src/ajax.js b/src/ajax.js index 9c1d6ca85..84ddc304a 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -1,4 +1,4 @@ -define([ +define( [ "./core", "./var/document", "./var/rnotwhite", @@ -15,6 +15,7 @@ var rhash = /#.*$/, rts = /([?&])_=[^&]*/, rheaders = /^(.*?):[ \t]*([^\r\n]*)$/mg, + // #7653, #8125, #8152: local protocol detection rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/, rnoContent = /^(?:GET|HEAD)$/, @@ -61,16 +62,18 @@ function addToPrefiltersOrTransports( structure ) { dataTypes = dataTypeExpression.toLowerCase().match( rnotwhite ) || []; if ( jQuery.isFunction( func ) ) { + // For each dataType in the dataTypeExpression - while ( (dataType = dataTypes[i++]) ) { + while ( ( dataType = dataTypes[ i++ ] ) ) { + // Prepend if requested - if ( dataType[0] === "+" ) { + if ( dataType[ 0 ] === "+" ) { dataType = dataType.slice( 1 ) || "*"; - (structure[ dataType ] = structure[ dataType ] || []).unshift( func ); + ( structure[ dataType ] = structure[ dataType ] || [] ).unshift( func ); // Otherwise append } else { - (structure[ dataType ] = structure[ dataType ] || []).push( func ); + ( structure[ dataType ] = structure[ dataType ] || [] ).push( func ); } } } @@ -97,7 +100,7 @@ function inspectPrefiltersOrTransports( structure, options, originalOptions, jqX } else if ( seekingTransport ) { return !( selected = dataTypeOrTransport ); } - }); + } ); return selected; } @@ -113,7 +116,7 @@ function ajaxExtend( target, src ) { for ( key in src ) { if ( src[ key ] !== undefined ) { - ( flatOptions[ key ] ? target : ( deep || (deep = {}) ) )[ key ] = src[ key ]; + ( flatOptions[ key ] ? target : ( deep || ( deep = {} ) ) )[ key ] = src[ key ]; } } if ( deep ) { @@ -137,7 +140,7 @@ function ajaxHandleResponses( s, jqXHR, responses ) { while ( dataTypes[ 0 ] === "*" ) { dataTypes.shift(); if ( ct === undefined ) { - ct = s.mimeType || jqXHR.getResponseHeader("Content-Type"); + ct = s.mimeType || jqXHR.getResponseHeader( "Content-Type" ); } } @@ -155,9 +158,10 @@ function ajaxHandleResponses( s, jqXHR, responses ) { if ( dataTypes[ 0 ] in responses ) { finalDataType = dataTypes[ 0 ]; } else { + // Try convertible dataTypes for ( type in responses ) { - if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) { + if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[ 0 ] ] ) { finalDataType = type; break; } @@ -165,6 +169,7 @@ function ajaxHandleResponses( s, jqXHR, responses ) { firstDataType = type; } } + // Or just use first one finalDataType = finalDataType || firstDataType; } @@ -186,6 +191,7 @@ function ajaxHandleResponses( s, jqXHR, responses ) { function ajaxConvert( s, response, jqXHR, isSuccess ) { var conv2, current, conv, tmp, prev, converters = {}, + // Work with a copy of dataTypes in case we need to modify it for conversion dataTypes = s.dataTypes.slice(); @@ -238,6 +244,7 @@ function ajaxConvert( s, response, jqXHR, isSuccess ) { conv = converters[ prev + " " + tmp[ 0 ] ] || converters[ "* " + tmp[ 0 ] ]; if ( conv ) { + // Condense equivalence converters if ( conv === true ) { conv = converters[ conv2 ]; @@ -257,7 +264,7 @@ function ajaxConvert( s, response, jqXHR, isSuccess ) { if ( conv !== true ) { // Unless errors are allowed to bubble, catch and return them - if ( conv && s[ "throws" ] ) { + if ( conv && s[ "throws" ] ) { // jscs:ignore requireDotNotation response = conv( response ); } else { try { @@ -277,7 +284,7 @@ function ajaxConvert( s, response, jqXHR, isSuccess ) { return { state: "success", data: response }; } -jQuery.extend({ +jQuery.extend( { // Counter for holding the number of active queries active: 0, @@ -382,40 +389,55 @@ jQuery.extend({ options = options || {}; var transport, + // URL without anti-cache param cacheURL, + // Response headers responseHeadersString, responseHeaders, + // timeout handle timeoutTimer, + // Url cleanup var urlAnchor, + // To know if global events are to be dispatched fireGlobals, + // Loop variable i, + // Create the final options object s = jQuery.ajaxSetup( {}, options ), + // Callbacks context callbackContext = s.context || s, + // Context for global events is callbackContext if it is a DOM node or jQuery collection globalEventContext = s.context && ( callbackContext.nodeType || callbackContext.jquery ) ? jQuery( callbackContext ) : jQuery.event, + // Deferreds deferred = jQuery.Deferred(), - completeDeferred = jQuery.Callbacks("once memory"), + completeDeferred = jQuery.Callbacks( "once memory" ), + // Status-dependent callbacks statusCode = s.statusCode || {}, + // Headers (they are sent all at once) requestHeaders = {}, requestHeadersNames = {}, + // The jqXHR state state = 0, + // Default abort message strAbort = "canceled", + // Fake xhr jqXHR = { readyState: 0, @@ -426,8 +448,8 @@ jQuery.extend({ if ( state === 2 ) { if ( !responseHeaders ) { responseHeaders = {}; - while ( (match = rheaders.exec( responseHeadersString )) ) { - responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; + while ( ( match = rheaders.exec( responseHeadersString ) ) ) { + responseHeaders[ match[ 1 ].toLowerCase() ] = match[ 2 ]; } } match = responseHeaders[ key.toLowerCase() ]; @@ -464,10 +486,12 @@ jQuery.extend({ if ( map ) { if ( state < 2 ) { for ( code in map ) { + // Lazy-add the new callback in a way that preserves old ones statusCode[ code ] = [ statusCode[ code ], map[ code ] ]; } } else { + // Execute the appropriate callbacks jqXHR.always( map[ jqXHR.status ] ); } @@ -510,12 +534,14 @@ jQuery.extend({ // IE throws exception if url is malformed, e.g. http://example.com:80x/ try { urlAnchor.href = s.url; + // Support: IE8-11+ // Anchor's host property isn't correctly set when s.url is relative urlAnchor.href = urlAnchor.href; s.crossDomain = originAnchor.protocol + "//" + originAnchor.host !== urlAnchor.protocol + "//" + urlAnchor.host; } catch ( e ) { + // If there is an error parsing the URL, assume it is crossDomain, // it can be rejected by the transport if it is invalid s.crossDomain = true; @@ -541,7 +567,7 @@ jQuery.extend({ // Watch for a new set of requests if ( fireGlobals && jQuery.active++ === 0 ) { - jQuery.event.trigger("ajaxStart"); + jQuery.event.trigger( "ajaxStart" ); } // Uppercase the type @@ -560,6 +586,7 @@ jQuery.extend({ // If data is available, append data to url if ( s.data ) { cacheURL = ( s.url += ( rquery.test( cacheURL ) ? "&" : "?" ) + s.data ); + // #9682: remove data so that it's not used in an eventual retry delete s.data; } @@ -594,8 +621,8 @@ jQuery.extend({ // Set the Accepts header for the server, depending on the dataType jqXHR.setRequestHeader( "Accept", - s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ? - s.accepts[ s.dataTypes[0] ] + + s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[ 0 ] ] ? + s.accepts[ s.dataTypes[ 0 ] ] + ( s.dataTypes[ 0 ] !== "*" ? ", " + allTypes + "; q=0.01" : "" ) : s.accepts[ "*" ] ); @@ -642,8 +669,8 @@ jQuery.extend({ // Timeout if ( s.async && s.timeout > 0 ) { - timeoutTimer = window.setTimeout(function() { - jqXHR.abort("timeout"); + timeoutTimer = window.setTimeout( function() { + jqXHR.abort( "timeout" ); }, s.timeout ); } @@ -651,9 +678,11 @@ jQuery.extend({ state = 1; transport.send( requestHeaders, done ); } catch ( e ) { + // Propagate exception as error if not done if ( state < 2 ) { done( -1, e ); + // Simply rethrow otherwise } else { throw e; @@ -705,11 +734,11 @@ jQuery.extend({ // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode. if ( s.ifModified ) { - modified = jqXHR.getResponseHeader("Last-Modified"); + modified = jqXHR.getResponseHeader( "Last-Modified" ); if ( modified ) { jQuery.lastModified[ cacheURL ] = modified; } - modified = jqXHR.getResponseHeader("etag"); + modified = jqXHR.getResponseHeader( "etag" ); if ( modified ) { jQuery.etag[ cacheURL ] = modified; } @@ -731,6 +760,7 @@ jQuery.extend({ isSuccess = !error; } } else { + // Extract error from statusText and normalize for non-aborts error = statusText; if ( status || !statusText ) { @@ -766,9 +796,10 @@ jQuery.extend({ if ( fireGlobals ) { globalEventContext.trigger( "ajaxComplete", [ jqXHR, s ] ); + // Handle the global AJAX counter if ( !( --jQuery.active ) ) { - jQuery.event.trigger("ajaxStop"); + jQuery.event.trigger( "ajaxStop" ); } } } @@ -783,10 +814,11 @@ jQuery.extend({ getScript: function( url, callback ) { return jQuery.get( url, undefined, callback, "script" ); } -}); +} ); jQuery.each( [ "get", "post" ], function( i, method ) { jQuery[ method ] = function( url, data, callback, type ) { + // Shift arguments if data argument was omitted if ( jQuery.isFunction( data ) ) { type = type || callback; @@ -795,7 +827,7 @@ jQuery.each( [ "get", "post" ], function( i, method ) { } // The url can be an options object (which then must have .url) - return jQuery.ajax( jQuery.extend({ + return jQuery.ajax( jQuery.extend( { url: url, type: method, dataType: type, @@ -803,7 +835,7 @@ jQuery.each( [ "get", "post" ], function( i, method ) { success: callback }, jQuery.isPlainObject( url ) && url ) ); }; -}); +} ); return jQuery; -}); +} ); |