test/dummy/vendor/assets/bower_components/jquery/src/ajax/xhr.js in loco-rails-1.0.0 vs test/dummy/vendor/assets/bower_components/jquery/src/ajax/xhr.js in loco-rails-1.0.1

- old
+ new

@@ -1,229 +1,169 @@ define( [ "../core", - "../var/document", "../var/support", "../ajax" -], function( jQuery, document, support ) { +], function( jQuery, support ) { -// Create the request object -// (This is still attached to ajaxSettings for backward compatibility) -jQuery.ajaxSettings.xhr = window.ActiveXObject !== undefined ? +"use strict"; - // Support: IE6-IE8 - function() { +jQuery.ajaxSettings.xhr = function() { + try { + return new window.XMLHttpRequest(); + } catch ( e ) {} +}; - // XHR cannot access local files, always use ActiveX for that case - if ( this.isLocal ) { - return createActiveXHR(); - } +var xhrSuccessStatus = { - // Support: IE 9-11 - // IE seems to error on cross-domain PATCH requests when ActiveX XHR - // is used. In IE 9+ always use the native XHR. - // Note: this condition won't catch Edge as it doesn't define - // document.documentMode but it also doesn't support ActiveX so it won't - // reach this code. - if ( document.documentMode > 8 ) { - return createStandardXHR(); - } + // File protocol always yields status code 0, assume 200 + 0: 200, - // Support: IE<9 - // oldIE XHR does not support non-RFC2616 methods (#13240) - // See http://msdn.microsoft.com/en-us/library/ie/ms536648(v=vs.85).aspx - // and http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9 - // Although this check for six methods instead of eight - // since IE also does not support "trace" and "connect" - return /^(get|post|head|put|delete|options)$/i.test( this.type ) && - createStandardXHR() || createActiveXHR(); - } : - - // For all other browsers, use the standard XMLHttpRequest object - createStandardXHR; - -var xhrId = 0, - xhrCallbacks = {}, + // Support: IE <=9 only + // #1450: sometimes IE returns 1223 when it should be 204 + 1223: 204 + }, xhrSupported = jQuery.ajaxSettings.xhr(); -// Support: IE<10 -// Open requests must be manually aborted on unload (#5280) -// See https://support.microsoft.com/kb/2856746 for more info -if ( window.attachEvent ) { - window.attachEvent( "onunload", function() { - for ( var key in xhrCallbacks ) { - xhrCallbacks[ key ]( undefined, true ); - } - } ); -} - -// Determine support properties support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported ); -xhrSupported = support.ajax = !!xhrSupported; +support.ajax = xhrSupported = !!xhrSupported; -// Create transport if the browser can provide an xhr -if ( xhrSupported ) { +jQuery.ajaxTransport( function( options ) { + var callback, errorCallback; - jQuery.ajaxTransport( function( options ) { + // Cross domain only allowed if supported through XMLHttpRequest + if ( support.cors || xhrSupported && !options.crossDomain ) { + return { + send: function( headers, complete ) { + var i, + xhr = options.xhr(); - // Cross domain only allowed if supported through XMLHttpRequest - if ( !options.crossDomain || support.cors ) { + xhr.open( + options.type, + options.url, + options.async, + options.username, + options.password + ); - var callback; - - return { - send: function( headers, complete ) { - var i, - xhr = options.xhr(), - id = ++xhrId; - - // Open the socket - xhr.open( - options.type, - options.url, - options.async, - options.username, - options.password - ); - - // Apply custom fields if provided - if ( options.xhrFields ) { - for ( i in options.xhrFields ) { - xhr[ i ] = options.xhrFields[ i ]; - } + // Apply custom fields if provided + if ( options.xhrFields ) { + for ( i in options.xhrFields ) { + xhr[ i ] = options.xhrFields[ i ]; } + } - // Override mime type if needed - if ( options.mimeType && xhr.overrideMimeType ) { - xhr.overrideMimeType( options.mimeType ); - } + // Override mime type if needed + if ( options.mimeType && xhr.overrideMimeType ) { + xhr.overrideMimeType( options.mimeType ); + } - // X-Requested-With header - // For cross-domain requests, seeing as conditions for a preflight are - // akin to a jigsaw puzzle, we simply never set it to be sure. - // (it can always be set on a per-request basis or even using ajaxSetup) - // For same-domain requests, won't change header if already provided. - if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) { - headers[ "X-Requested-With" ] = "XMLHttpRequest"; - } + // X-Requested-With header + // For cross-domain requests, seeing as conditions for a preflight are + // akin to a jigsaw puzzle, we simply never set it to be sure. + // (it can always be set on a per-request basis or even using ajaxSetup) + // For same-domain requests, won't change header if already provided. + if ( !options.crossDomain && !headers[ "X-Requested-With" ] ) { + headers[ "X-Requested-With" ] = "XMLHttpRequest"; + } - // Set headers - for ( i in headers ) { + // Set headers + for ( i in headers ) { + xhr.setRequestHeader( i, headers[ i ] ); + } - // Support: IE<9 - // IE's ActiveXObject throws a 'Type Mismatch' exception when setting - // request header to a null-value. - // - // To keep consistent with other XHR implementations, cast the value - // to string and ignore `undefined`. - if ( headers[ i ] !== undefined ) { - xhr.setRequestHeader( i, headers[ i ] + "" ); - } - } + // Callback + callback = function( type ) { + return function() { + if ( callback ) { + callback = errorCallback = xhr.onload = + xhr.onerror = xhr.onabort = xhr.onreadystatechange = null; - // Do send the request - // This may raise an exception which is actually - // handled in jQuery.ajax (so no try/catch here) - xhr.send( ( options.hasContent && options.data ) || null ); + if ( type === "abort" ) { + xhr.abort(); + } else if ( type === "error" ) { - // Listener - callback = function( _, isAbort ) { - var status, statusText, responses; + // Support: IE <=9 only + // On a manual native abort, IE9 throws + // errors on any property access that is not readyState + if ( typeof xhr.status !== "number" ) { + complete( 0, "error" ); + } else { + complete( - // Was never called and is aborted or complete - if ( callback && ( isAbort || xhr.readyState === 4 ) ) { - - // Clean up - delete xhrCallbacks[ id ]; - callback = undefined; - xhr.onreadystatechange = jQuery.noop; - - // Abort manually if needed - if ( isAbort ) { - if ( xhr.readyState !== 4 ) { - xhr.abort(); + // File: protocol always yields status 0; see #8605, #14207 + xhr.status, + xhr.statusText + ); } } else { - responses = {}; - status = xhr.status; + complete( + xhrSuccessStatus[ xhr.status ] || xhr.status, + xhr.statusText, - // Support: IE<10 - // Accessing binary-data responseText throws an exception - // (#11426) - if ( typeof xhr.responseText === "string" ) { - responses.text = xhr.responseText; - } + // Support: IE <=9 only + // IE9 has no XHR2 but throws on binary (trac-11426) + // For XHR2 non-text, let the caller handle it (gh-2498) + ( xhr.responseType || "text" ) !== "text" || + typeof xhr.responseText !== "string" ? + { binary: xhr.response } : + { text: xhr.responseText }, + xhr.getAllResponseHeaders() + ); + } + } + }; + }; - // Firefox throws an exception when accessing - // statusText for faulty cross-domain requests - try { - statusText = xhr.statusText; - } catch ( e ) { + // Listen to events + xhr.onload = callback(); + errorCallback = xhr.onerror = callback( "error" ); - // We normalize with Webkit giving an empty statusText - statusText = ""; - } + // Support: IE 9 only + // Use onreadystatechange to replace onabort + // to handle uncaught aborts + if ( xhr.onabort !== undefined ) { + xhr.onabort = errorCallback; + } else { + xhr.onreadystatechange = function() { - // Filter status for non standard behaviors + // Check readyState before timeout as it changes + if ( xhr.readyState === 4 ) { - // If the request is local and we have data: assume a success - // (success with no data won't get notified, that's the best we - // can do given current implementations) - if ( !status && options.isLocal && !options.crossDomain ) { - status = responses.text ? 200 : 404; - - // IE - #1450: sometimes returns 1223 when it should be 204 - } else if ( status === 1223 ) { - status = 204; + // Allow onerror to be called first, + // but that will not handle a native abort + // Also, save errorCallback to a variable + // as xhr.onerror cannot be accessed + window.setTimeout( function() { + if ( callback ) { + errorCallback(); } - } + } ); } - - // Call complete if needed - if ( responses ) { - complete( status, statusText, responses, xhr.getAllResponseHeaders() ); - } }; + } - // Do send the request - // `xhr.send` may raise an exception, but it will be - // handled in jQuery.ajax (so no try/catch here) - if ( !options.async ) { + // Create the abort callback + callback = callback( "abort" ); - // If we're in sync mode we fire the callback - callback(); - } else if ( xhr.readyState === 4 ) { + try { - // (IE6 & IE7) if it's in cache and has been - // retrieved directly we need to fire the callback - window.setTimeout( callback ); - } else { + // Do send the request (this may raise an exception) + xhr.send( options.hasContent && options.data || null ); + } catch ( e ) { - // Register the callback, but delay it in case `xhr.send` throws - // Add to the list of active xhr callbacks - xhr.onreadystatechange = xhrCallbacks[ id ] = callback; - } - }, - - abort: function() { + // #14683: Only rethrow if this hasn't been notified as an error yet if ( callback ) { - callback( undefined, true ); + throw e; } } - }; - } - } ); -} + }, -// Functions to create xhrs -function createStandardXHR() { - try { - return new window.XMLHttpRequest(); - } catch ( e ) {} -} - -function createActiveXHR() { - try { - return new window.ActiveXObject( "Microsoft.XMLHTTP" ); - } catch ( e ) {} -} + abort: function() { + if ( callback ) { + callback(); + } + } + }; + } +} ); } );