From 8d050558d3dc2c0abd0bde4bc9d03bfb66974689 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Tue, 25 Jan 2011 03:52:17 +0100 Subject: [PATCH 1/6] #8044 Removes unnec. rnonword var --- src/core.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/core.js b/src/core.js index 520db45ed..43d46e9e2 100644 --- a/src/core.js +++ b/src/core.js @@ -26,9 +26,6 @@ var jQuery = function( selector, context ) { trimLeft = /^\s+/, trimRight = /\s+$/, - // Check for non-word characters - rnonword = /\W/, - // Check for digits rdigit = /\d/, From 5ca8f0617f5c94495380ff783452a52eab706d39 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Tue, 25 Jan 2011 16:08:19 +0100 Subject: [PATCH 2/6] Reworks how values of parameters passed to error callbacks are determined. Fixes #8050. --- src/ajax.js | 22 ++++++++++++++++------ src/ajax/xhr.js | 9 +++------ test/unit/ajax.js | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 89d60e1d0..8f8bc60ab 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -400,8 +400,9 @@ jQuery.extend({ // Cancel the request abort: function( statusText ) { + statusText = statusText || "abort"; if ( transport ) { - transport.abort( statusText || "abort" ); + transport.abort( statusText ); } done( 0, statusText ); return this; @@ -438,7 +439,7 @@ jQuery.extend({ var isSuccess, success, - error = ( statusText = statusText || "error" ), + error, response = responses ? ajaxHandleResponses( s, jXHR, responses ) : undefined, lastModified, etag; @@ -476,6 +477,16 @@ jQuery.extend({ error = "" + e; } } + } else { + // We extract error from statusText + // then normalize statusText and status for non-aborts + error = statusText; + if( status ) { + statusText = "error"; + if ( status < 0 ) { + status = 0; + } + } } // Set data for the fake xhr object @@ -634,7 +645,7 @@ jQuery.extend({ // If no transport, we auto-abort if ( !transport ) { - done( 0, "notransport" ); + done( -1, "No Transport" ); } else { // Set state as sending state = jXHR.readyState = 1; @@ -653,9 +664,8 @@ jQuery.extend({ transport.send( requestHeaders, done ); } catch (e) { // Propagate exception as error if not done - if ( status === 1 ) { - done( 0, "error", "" + e ); - jXHR = false; + if ( status < 2 ) { + done( -1, "" + e ); // Simply rethrow otherwise } else { jQuery.error( e ); diff --git a/src/ajax/xhr.js b/src/ajax/xhr.js index 5629dcd60..b82064239 100644 --- a/src/ajax/xhr.js +++ b/src/ajax/xhr.js @@ -108,12 +108,9 @@ if ( jQuery.support.ajax ) { } catch( _ ) {} // Do send the request - try { - xhr.send( ( s.hasContent && s.data ) || null ); - } catch( e ) { - complete( 0, "error", "" + e ); - return; - } + // This may raise an exception which is actually + // handled in jQuery.ajax (so no try/catch here) + xhr.send( ( s.hasContent && s.data ) || null ); // Listener callback = function( _, isAbort ) { diff --git a/test/unit/ajax.js b/test/unit/ajax.js index b44f0773f..d01837239 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -240,6 +240,47 @@ test("jQuery.ajax() - error callbacks", function() { }); }); +test("jQuery.ajax() - textStatus and errorThrown values", function() { + + var nb = 3; + + expect( 2 * nb ); + stop(); + + function startN() { + if ( !( --nb ) ) { + start(); + } + } + + jQuery.ajax({ + url: url("data/nonExistingURL"), + error: function( _ , textStatus , errorThrown ){ + strictEqual( textStatus, "error", "textStatus is 'error' for 404" ); + strictEqual( errorThrown, "Not Found", "errorThrown is 'Not Found' for 404"); + startN(); + } + }); + + jQuery.ajax({ + url: url("data/name.php?wait=5"), + error: function( _ , textStatus , errorThrown ){ + strictEqual( textStatus, "abort", "textStatus is 'abort' for abort" ); + strictEqual( errorThrown, "abort", "errorThrown is 'abort' for abort"); + startN(); + } + }).abort(); + + jQuery.ajax({ + url: url("data/name.php?wait=5"), + error: function( _ , textStatus , errorThrown ){ + strictEqual( textStatus, "mystatus", "textStatus is 'mystatus' for abort('mystatus')" ); + strictEqual( errorThrown, "mystatus", "errorThrown is 'mystatus' for abort('mystatus')"); + startN(); + } + }).abort( "mystatus" ); +}); + test("jQuery.ajax() - responseText on error", function() { expect( 1 ); From 325dcdc2ab05173f809b9d83af59918b3695cc23 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Wed, 26 Jan 2011 00:55:39 +0100 Subject: [PATCH 3/6] Fixes #8054 by reverting feature enhancement 5812 (4920). Regexps no longer searches for %3F in url or data to find jsonp callback placeholders. --- src/ajax/jsonp.js | 2 +- test/unit/ajax.js | 21 ++------------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/ajax/jsonp.js b/src/ajax/jsonp.js index f5742d998..16a4c2fd2 100644 --- a/src/ajax/jsonp.js +++ b/src/ajax/jsonp.js @@ -1,7 +1,7 @@ (function( jQuery ) { var jsc = jQuery.now(), - jsre = /(\=)(?:\?|%3F)(&|$)|()(?:\?\?|%3F%3F)()/i; + jsre = /(\=)\?(&|$)|()\?\?()/i; // Default jsonp settings jQuery.ajaxSetup({ diff --git a/test/unit/ajax.js b/test/unit/ajax.js index d01837239..abe90c88e 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -1199,10 +1199,10 @@ test("jQuery.getScript(String, Function) - no callback", function() { jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) { test("jQuery.ajax() - JSONP, " + label, function() { - expect(17); + expect(16); var count = 0; - function plus(){ if ( ++count == 17 ) start(); } + function plus(){ if ( ++count == 16 ) start(); } stop(); @@ -1306,23 +1306,6 @@ jQuery.each( [ "Same Domain", "Cross Domain" ] , function( crossDomain , label ) } }); - jQuery.ajax({ - url: "data/jsonp.php", - dataType: "jsonp", - crossDomain: crossDomain, - data: { - callback: "?" - }, - success: function(data){ - ok( data.data, "JSON results returned (GET, processed data callback)" ); - plus(); - }, - error: function(data){ - ok( false, "Ajax error JSON (GET, processed data callback)" ); - plus(); - } - }); - jQuery.ajax({ url: "data/jsonp.php", dataType: "jsonp", From 0e5b341cc0f3f9bf0f6659e09704f2267cfdfdba Mon Sep 17 00:00:00 2001 From: jaubourg Date: Wed, 26 Jan 2011 01:36:05 +0100 Subject: [PATCH 4/6] Fixes #5856. Adds document protocol at the beginning of URLs without protocol (thanks go to skrings for the initial pull request). Simplifies cross-domain detection regexp and logic as a consequence. Also took the opportunity to remove an unused variable. Unit test added. --- src/ajax.js | 19 ++++++++----------- test/unit/ajax.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index 8f8bc60ab..a6ee676d4 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -7,15 +7,13 @@ var r20 = /%20/g, rheaders = /^(.*?):\s*(.*?)\r?$/mg, // IE leaves an \r character at EOL rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i, rnoContent = /^(?:GET|HEAD)$/, + rprotocol = /^\/\//, rquery = /\?/, rscript = /)<[^<]*)*<\/script>/gi, rselectTextarea = /^(?:select|textarea)/i, rspacesAjax = /\s+/, rts = /([?&])_=[^&]*/, - rurl = /^(\w+:)?\/\/([^\/?#:]+)(?::(\d+))?/, - - // Slice function - sliceFunc = Array.prototype.slice, + rurl = /^(\w+:)\/\/([^\/?#:]+)(?::(\d+))?/, // Keep a copy of the old load method _load = jQuery.fn.load, @@ -544,8 +542,9 @@ jQuery.extend({ }; // Remove hash character (#7531: and string promotion) + // Add protocol if not provided (#5856: IE7 issue with protocol-less urls) // We also use the url parameter if available - s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ); + s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ).replace( rprotocol, protocol + "//" ); // Extract dataTypes list s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax ); @@ -553,12 +552,10 @@ jQuery.extend({ // Determine if a cross-domain request is in order if ( !s.crossDomain ) { parts = rurl.exec( s.url.toLowerCase() ); - s.crossDomain = !!( - parts && - ( parts[ 1 ] && parts[ 1 ] != protocol || - parts[ 2 ] != loc.hostname || - ( parts[ 3 ] || ( ( parts[ 1 ] || protocol ) === "http:" ? 80 : 443 ) ) != - ( loc.port || ( protocol === "http:" ? 80 : 443 ) ) ) + s.crossDomain = !!( parts && + ( parts[ 1 ] != protocol || parts[ 2 ] != loc.hostname || + ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? 80 : 443 ) ) != + ( loc.port || ( protocol === "http:" ? 80 : 443 ) ) ) ); } diff --git a/test/unit/ajax.js b/test/unit/ajax.js index abe90c88e..2624b5583 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -415,6 +415,18 @@ test(".ajax() - contentType" , function() { }); +test(".ajax() - protocol-less urls", function() { + expect(1); + + jQuery.ajax({ + url: "//somedomain.com", + beforeSend: function( xhr, settings ) { + equals(settings.url, location.protocol + "//somedomain.com", "Make sure that the protocol is added."); + return false; + } + }); +}); + test(".ajax() - hash", function() { expect(3); From d7d64713a72c67243b279b9dcb16ae9fbb825c17 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Wed, 26 Jan 2011 01:45:00 +0100 Subject: [PATCH 5/6] Fixes #5866. Issue number in previous commit was wrong both in comments and commit message. See https://github.com/jquery/jquery/commit/0e5b341cc0f3f9bf0f6659e09704f2267cfdfdba for previous commit. --- src/ajax.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ajax.js b/src/ajax.js index a6ee676d4..caacb6059 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -542,7 +542,7 @@ jQuery.extend({ }; // Remove hash character (#7531: and string promotion) - // Add protocol if not provided (#5856: IE7 issue with protocol-less urls) + // Add protocol if not provided (#5866: IE7 issue with protocol-less urls) // We also use the url parameter if available s.url = ( "" + ( url || s.url ) ).replace( rhash, "" ).replace( rprotocol, protocol + "//" ); From bab8079593913dbc689404aa4e83c46b9b4c9355 Mon Sep 17 00:00:00 2001 From: jaubourg Date: Wed, 26 Jan 2011 17:37:08 +0100 Subject: [PATCH 6/6] Passes jXHR object as third argument of prefilters and transport factories. --- src/ajax.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ajax.js b/src/ajax.js index caacb6059..592f297f2 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -73,7 +73,7 @@ function addToPrefiltersOrTransports( structure ) { } //Base inspection function for prefilters and transports -function inspectPrefiltersOrTransports( structure, options, originalOptions, +function inspectPrefiltersOrTransports( structure, options, originalOptions, jXHR, dataType /* internal */, inspected /* internal */ ) { dataType = dataType || options.dataTypes[ 0 ]; @@ -97,7 +97,7 @@ function inspectPrefiltersOrTransports( structure, options, originalOptions, } else { options.dataTypes.unshift( selection ); selection = inspectPrefiltersOrTransports( - structure, options, originalOptions, selection, inspected ); + structure, options, originalOptions, jXHR, selection, inspected ); } } } @@ -105,7 +105,7 @@ function inspectPrefiltersOrTransports( structure, options, originalOptions, // we try the catchall dataType if not done already if ( ( executeOnly || !selection ) && !inspected[ "*" ] ) { selection = inspectPrefiltersOrTransports( - structure, options, originalOptions, "*", inspected ); + structure, options, originalOptions, jXHR, "*", inspected ); } // unnecessary when only executing (prefilters) // but it'll be ignored by the caller in that case @@ -565,7 +565,7 @@ jQuery.extend({ } // Apply prefilters - inspectPrefiltersOrTransports( prefilters, s, options ); + inspectPrefiltersOrTransports( prefilters, s, options, jXHR ); // Uppercase the type s.type = s.type.toUpperCase(); @@ -638,7 +638,7 @@ jQuery.extend({ } // Get transport - transport = inspectPrefiltersOrTransports( transports, s, options ); + transport = inspectPrefiltersOrTransports( transports, s, options, jXHR ); // If no transport, we auto-abort if ( !transport ) {