]> source.dussan.org Git - jquery.git/commitdiff
Fix #12838: hook point for non-jQuery.ajax synchronous script fetch/execute in domMan...
authorRichard Gibson <richard.gibson@gmail.com>
Sun, 2 Dec 2012 05:14:25 +0000 (00:14 -0500)
committerRichard Gibson <richard.gibson@gmail.com>
Wed, 17 Apr 2013 15:27:57 +0000 (11:27 -0400)
src/ajax.js
src/manipulation.js
test/unit/manipulation.js

index 62c3d8ae148201ab2dd63df51e653d3f3da34dad..d0a8a4f120dbdf17a9ef5f3ab1290141c49a3caa 100644 (file)
@@ -194,25 +194,6 @@ jQuery.each( [ "ajaxStart", "ajaxStop", "ajaxComplete", "ajaxError", "ajaxSucces
        };
 });
 
-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;
-                       callback = data;
-                       data = undefined;
-               }
-
-               return jQuery.ajax({
-                       url: url,
-                       type: method,
-                       dataType: type,
-                       data: data,
-                       success: callback
-               });
-       };
-});
-
 jQuery.extend({
 
        // Counter for holding the number of active queries
@@ -694,15 +675,34 @@ jQuery.extend({
                return jqXHR;
        },
 
-       getScript: function( url, callback ) {
-               return jQuery.get( url, undefined, callback, "script" );
-       },
-
        getJSON: function( url, data, callback ) {
                return jQuery.get( url, data, callback, "json" );
+       },
+
+       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;
+                       callback = data;
+                       data = undefined;
+               }
+
+               return jQuery.ajax({
+                       url: url,
+                       type: method,
+                       dataType: type,
+                       data: data,
+                       success: callback
+               });
+       };
+});
+
 /* Handles responses to an ajax request:
  * - finds the right dataType (mediates between content-type and expected dataType)
  * - returns the corresponding response
index 640dfcd6f40a4f761b81e0c98007c1b3b650062d..f37ec421ab63a35d12dc001e86f08b12d1bfb0ae 100644 (file)
@@ -256,14 +256,7 @@ jQuery.fn.extend({
 
                                                        if ( node.src ) {
                                                                // Hope ajax is available...
-                                                               jQuery.ajax({
-                                                                       url: node.src,
-                                                                       type: "GET",
-                                                                       dataType: "script",
-                                                                       async: false,
-                                                                       global: false,
-                                                                       "throws": true
-                                                               });
+                                                               jQuery._evalUrl( node.src );
                                                        } else {
                                                                jQuery.globalEval( node.textContent.replace( rcleanScript, "" ) );
                                                        }
@@ -465,6 +458,17 @@ jQuery.extend({
                        data_user.discard( elem );
                        data_priv.discard( elem );
                }
+       },
+
+       _evalUrl: function( url ) {
+               return jQuery.ajax({
+                       url: url,
+                       type: "GET",
+                       dataType: "text",
+                       async: false,
+                       global: false,
+                       success: jQuery.globalEval
+               });
        }
 });
 
index 30710da583ba1ffa7a2d8c99e5886ce1b984425f..09b425c8af19375476b276a67ddef01d2e2771ea 100644 (file)
@@ -1880,14 +1880,19 @@ test( "html() - script exceptions bubble (#11743)", function() {
                ok( false, "Exception ignored" );
        }, "Exception bubbled from inline script" );
 
-       var onerror = window.onerror;
-       window.onerror = function() {
-               ok( true, "Exception thrown in remote script" );
-               window.onerror = onerror;
-       };
+       if ( jQuery.ajax ) {
+               var onerror = window.onerror;
+               window.onerror = function() {
+                       ok( true, "Exception thrown in remote script" );
+               };
 
-       jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
-       ok( true, "Exception ignored" );
+               jQuery("#qunit-fixture").html("<script src='data/badcall.js'></script>");
+               ok( true, "Exception ignored" );
+               window.onerror = onerror;
+       } else {
+               ok( true, "No jQuery.ajax" );
+               ok( true, "No jQuery.ajax" );
+       }
 });
 
 test( "checked state is cloned with clone()", function() {
@@ -1927,7 +1932,7 @@ testIframeWithCallback( "buildFragment works even if document[0] is iframe's win
 
 test( "script evaluation (#11795)", function() {
 
-       expect( 11 );
+       expect( 13 );
 
        var scriptsIn, scriptsOut,
                fixture = jQuery("#qunit-fixture").empty(),
@@ -1968,6 +1973,44 @@ test( "script evaluation (#11795)", function() {
        fixture.append( scriptsOut.detach() );
        deepEqual( fixture.children("script").get(), scriptsOut.get(), "Scripts detached without reevaluation" );
        objGlobal.ok = isOk;
+
+       if ( jQuery.ajax ) {
+               Globals.register("testBar");
+               jQuery("#qunit-fixture").append( "<script src='" + url("data/test.js") + "'/>" );
+               strictEqual( window["testBar"], "bar", "Global script evaluation" );
+       } else {
+               ok( true, "No jQuery.ajax" );
+               ok( true, "No jQuery.ajax" );
+       }
+});
+
+test( "jQuery._evalUrl (#12838)", function() {
+
+       expect( 5 );
+
+       var message, expectedArgument,
+               ajax = jQuery.ajax,
+               evalUrl = jQuery._evalUrl;
+
+       message = "jQuery.ajax implementation";
+       expectedArgument = 1;
+       jQuery.ajax = function( input ) {
+               equal( ( input.url || input ).slice( -1 ), expectedArgument, message );
+               expectedArgument++;
+       };
+       jQuery("#qunit-fixture").append("<script src='1'/><script src='2'/>");
+       equal( expectedArgument, 3, "synchronous execution" );
+
+       message = "custom implementation";
+       expectedArgument = 3;
+       jQuery._evalUrl = jQuery.ajax;
+       jQuery.ajax = function( options ) {
+               strictEqual( options, {}, "Unexpected call to jQuery.ajax" );
+       };
+       jQuery("#qunit-fixture").append("<script src='3'/><script src='4'/>");
+
+       jQuery.ajax = ajax;
+       jQuery._evalUrl = evalUrl;
 });
 
 test( "insertAfter, insertBefore, etc do not work when destination is original element. Element is removed (#4087)", function() {