Commit a938d7b1 authored by Richard Gibson's avatar Richard Gibson

No ticket: compress ajax. Close gh-1041.

parent c27dc018
This diff is collapsed.
var oldCallbacks = [], var oldCallbacks = [],
rquestion = /\?/, rjsonp = /(=)\?(?=&|$)|\?\?/;
rjsonp = /(=)\?(?=&|$)|\?\?/,
nonce = jQuery.now();
// Default jsonp settings // Default jsonp settings
jQuery.ajaxSetup({ jQuery.ajaxSetup({
jsonp: "callback", jsonp: "callback",
jsonpCallback: function() { jsonpCallback: function() {
var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( nonce++ ) ); var callback = oldCallbacks.pop() || ( jQuery.expando + "_" + ( ajax_nonce++ ) );
this[ callback ] = true; this[ callback ] = true;
return callback; return callback;
} }
...@@ -17,30 +15,24 @@ jQuery.ajaxSetup({ ...@@ -17,30 +15,24 @@ jQuery.ajaxSetup({
jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
var callbackName, overwritten, responseContainer, var callbackName, overwritten, responseContainer,
data = s.data, jsonProp = s.jsonp !== false && ( rjsonp.test( s.url ) ?
url = s.url, "url" :
hasCallback = s.jsonp !== false, typeof s.data === "string" && !( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") && rjsonp.test( s.data ) && "data"
replaceInUrl = hasCallback && rjsonp.test( url ), );
replaceInData = hasCallback && !replaceInUrl && typeof data === "string" &&
!( s.contentType || "" ).indexOf("application/x-www-form-urlencoded") &&
rjsonp.test( data );
// Handle iff the expected data type is "jsonp" or we have a parameter to set // Handle iff the expected data type is "jsonp" or we have a parameter to set
if ( s.dataTypes[ 0 ] === "jsonp" || replaceInUrl || replaceInData ) { if ( jsonProp || s.dataTypes[ 0 ] === "jsonp" ) {
// Get callback name, remembering preexisting value associated with it // Get callback name, remembering preexisting value associated with it
callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ? callbackName = s.jsonpCallback = jQuery.isFunction( s.jsonpCallback ) ?
s.jsonpCallback() : s.jsonpCallback() :
s.jsonpCallback; s.jsonpCallback;
overwritten = window[ callbackName ];
// Insert callback into url or form data // Insert callback into url or form data
if ( replaceInUrl ) { if ( jsonProp ) {
s.url = url.replace( rjsonp, "$1" + callbackName ); s[ jsonProp ] = s[ jsonProp ].replace( rjsonp, "$1" + callbackName );
} else if ( replaceInData ) { } else if ( s.jsonp !== false ) {
s.data = data.replace( rjsonp, "$1" + callbackName ); s.url += ( ajax_rquery.test( s.url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
} else if ( hasCallback ) {
s.url += ( rquestion.test( url ) ? "&" : "?" ) + s.jsonp + "=" + callbackName;
} }
// Use data converter to retrieve json after script execution // Use data converter to retrieve json after script execution
...@@ -55,6 +47,7 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) { ...@@ -55,6 +47,7 @@ jQuery.ajaxPrefilter( "json jsonp", function( s, originalSettings, jqXHR ) {
s.dataTypes[ 0 ] = "json"; s.dataTypes[ 0 ] = "json";
// Install callback // Install callback
overwritten = window[ callbackName ];
window[ callbackName ] = function() { window[ callbackName ] = function() {
responseContainer = arguments; responseContainer = arguments;
}; };
......
...@@ -4,7 +4,7 @@ jQuery.ajaxSetup({ ...@@ -4,7 +4,7 @@ jQuery.ajaxSetup({
script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript" script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"
}, },
contents: { contents: {
script: /javascript|ecmascript/ script: /(?:java|ecma)script/
}, },
converters: { converters: {
"text script": function( text ) { "text script": function( text ) {
...@@ -32,13 +32,13 @@ jQuery.ajaxTransport( "script", function(s) { ...@@ -32,13 +32,13 @@ jQuery.ajaxTransport( "script", function(s) {
if ( s.crossDomain ) { if ( s.crossDomain ) {
var script, var script,
head = document.head || document.getElementsByTagName( "head" )[0] || document.documentElement; head = document.head || jQuery("head")[0] || document.documentElement;
return { return {
send: function( _, callback ) { send: function( _, callback ) {
script = document.createElement( "script" ); script = document.createElement("script");
script.async = true; script.async = true;
...@@ -57,12 +57,12 @@ jQuery.ajaxTransport( "script", function(s) { ...@@ -57,12 +57,12 @@ jQuery.ajaxTransport( "script", function(s) {
script.onload = script.onreadystatechange = null; script.onload = script.onreadystatechange = null;
// Remove the script // Remove the script
if ( head && script.parentNode ) { if ( script.parentNode ) {
head.removeChild( script ); script.parentNode.removeChild( script );
} }
// Dereference the script // Dereference the script
script = undefined; script = null;
// Callback if not abort // Callback if not abort
if ( !isAbort ) { if ( !isAbort ) {
...@@ -70,14 +70,15 @@ jQuery.ajaxTransport( "script", function(s) { ...@@ -70,14 +70,15 @@ jQuery.ajaxTransport( "script", function(s) {
} }
} }
}; };
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378). // Circumvent IE6 bugs with base elements (#2709 and #4378) by prepending
// Use native DOM manipulation to avoid our domManip AJAX trickery
head.insertBefore( script, head.firstChild ); head.insertBefore( script, head.firstChild );
}, },
abort: function() { abort: function() {
if ( script ) { if ( script ) {
script.onload( 0, 1 ); script.onload( undefined, true );
} }
} }
}; };
......
var xhrCallbacks, var xhrCallbacks, xhrSupported,
xhrId = 0,
// #5280: Internet Explorer will keep connections alive if we don't abort on unload // #5280: Internet Explorer will keep connections alive if we don't abort on unload
xhrOnUnloadAbort = window.ActiveXObject ? function() { xhrOnUnloadAbort = window.ActiveXObject && function() {
// Abort all pending requests // Abort all pending requests
for ( var key in xhrCallbacks ) { var key;
xhrCallbacks[ key ]( 0, 1 ); for ( key in xhrCallbacks ) {
xhrCallbacks[ key ]( undefined, true );
} }
} : false, };
xhrId = 0;
// Functions to create xhrs // Functions to create xhrs
function createStandardXHR() { function createStandardXHR() {
...@@ -17,7 +18,7 @@ function createStandardXHR() { ...@@ -17,7 +18,7 @@ function createStandardXHR() {
function createActiveXHR() { function createActiveXHR() {
try { try {
return new window.ActiveXObject( "Microsoft.XMLHTTP" ); return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch( e ) {} } catch( e ) {}
} }
...@@ -37,15 +38,12 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ? ...@@ -37,15 +38,12 @@ jQuery.ajaxSettings.xhr = window.ActiveXObject ?
createStandardXHR; createStandardXHR;
// Determine support properties // Determine support properties
(function( xhr ) { xhrSupported = jQuery.ajaxSettings.xhr();
jQuery.extend( jQuery.support, { jQuery.support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
ajax: !!xhr, xhrSupported = jQuery.support.ajax = !!xhrSupported;
cors: !!xhr && ( "withCredentials" in xhr )
});
})( jQuery.ajaxSettings.xhr() );
// Create transport if the browser can provide an xhr // Create transport if the browser can provide an xhr
if ( jQuery.support.ajax ) { if ( xhrSupported ) {
jQuery.ajaxTransport(function( s ) { jQuery.ajaxTransport(function( s ) {
// Cross domain only allowed if supported through XMLHttpRequest // Cross domain only allowed if supported through XMLHttpRequest
...@@ -86,7 +84,7 @@ if ( jQuery.support.ajax ) { ...@@ -86,7 +84,7 @@ if ( jQuery.support.ajax ) {
// (it can always be set on a per-request basis or even using ajaxSetup) // (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. // For same-domain requests, won't change header if already provided.
if ( !s.crossDomain && !headers["X-Requested-With"] ) { if ( !s.crossDomain && !headers["X-Requested-With"] ) {
headers[ "X-Requested-With" ] = "XMLHttpRequest"; headers["X-Requested-With"] = "XMLHttpRequest";
} }
// Need an extra try/catch for cross domain requests in Firefox 3 // Need an extra try/catch for cross domain requests in Firefox 3
...@@ -136,10 +134,10 @@ if ( jQuery.support.ajax ) { ...@@ -136,10 +134,10 @@ if ( jQuery.support.ajax ) {
xhr.abort(); xhr.abort();
} }
} else { } else {
status = xhr.status;
responseHeaders = xhr.getAllResponseHeaders();
responses = {}; responses = {};
status = xhr.status;
xml = xhr.responseXML; xml = xhr.responseXML;
responseHeaders = xhr.getAllResponseHeaders();
// Construct response list // Construct response list
if ( xml && xml.documentElement /* #4958 */ ) { if ( xml && xml.documentElement /* #4958 */ ) {
...@@ -211,7 +209,7 @@ if ( jQuery.support.ajax ) { ...@@ -211,7 +209,7 @@ if ( jQuery.support.ajax ) {
abort: function() { abort: function() {
if ( callback ) { if ( callback ) {
callback(0,1); callback( undefined, true );
} }
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment