Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix(noConflict): resolve global JSONP conflicts and window.angular cannibalization #2893

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var /** holds major version number for IE or NaN for real browsers */

_angular = window.angular,
/** @name angular */
angular = window.angular || (window.angular = {}),
angular = window.angular = {},
angularModule,
nodeName_,
uid = ['0', '0', '0'];
Expand Down
18 changes: 9 additions & 9 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,32 @@ var XHR = window.XMLHttpRequest || function() {
*/
function $HttpBackendProvider() {
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks,
return createHttpBackend($browser, $window, XHR, $browser.defer,
$document[0], $window.location.protocol.replace(':', ''));
}];
}

function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
function createHttpBackend($browser, $window, XHR, $browserDefer, rawDocument, locationProtocol) {
// TODO(vojta): fix the signature
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
var status;
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();

if (lowercase(method) == 'jsonp') {
var callbackId = '_' + (callbacks.counter++).toString(36);
callbacks[callbackId] = function(data) {
callbacks[callbackId].data = data;
var callbackHanger = 'ngJsonpCallback_' + Math.random().toString(36).substring(7);
$window[callbackHanger] = function(data) {
$window[callbackHanger].data = data;
};

var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', 'angular.callbacks.' + callbackId),
var jsonpDone = jsonpReq(url.replace('JSON_CALLBACK', callbackHanger),
function() {
if (callbacks[callbackId].data) {
completeRequest(callback, 200, callbacks[callbackId].data);
if ($window[callbackHanger].data) {
completeRequest(callback, 200, $window[callbackHanger].data);
} else {
completeRequest(callback, status || -2);
}
delete callbacks[callbackId];
delete $window[callbackHanger];
});
} else {
var xhr = new XHR();
Expand Down
27 changes: 14 additions & 13 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('$httpBackend', function() {

var $backend, $browser, callbacks,
var $backend, $browser, $window, callbacks,
xhr, fakeDocument, callback,
fakeTimeoutId = 0;

Expand Down Expand Up @@ -36,6 +36,7 @@ describe('$httpBackend', function() {
beforeEach(inject(function($injector) {
callbacks = {counter: 0};
$browser = $injector.get('$browser');
$window = $injector.get('$window');
fakeDocument = {
$$scripts: [],
createElement: jasmine.createSpy('createElement').andCallFake(function() {
Expand All @@ -53,7 +54,7 @@ describe('$httpBackend', function() {
})
}
};
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
$backend = createHttpBackend($browser, $window, MockXhr, fakeTimeout, fakeDocument);
callback = jasmine.createSpy('done');
}));

Expand Down Expand Up @@ -200,7 +201,7 @@ describe('$httpBackend', function() {
expect(response).toBe('response');
});

$backend = createHttpBackend($browser, SyncXhr);
$backend = createHttpBackend($browser, $window, SyncXhr);
$backend('GET', '/url', null, callback);
expect(callback).toHaveBeenCalledOnce();
});
Expand Down Expand Up @@ -232,7 +233,7 @@ describe('$httpBackend', function() {

describe('JSONP', function() {

var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
var SCRIPT_URL = /([^\?]*)\?cb=ngJsonpCallback_(.*)/;


it('should add script tag for JSONP request', function() {
Expand All @@ -243,12 +244,12 @@ describe('$httpBackend', function() {

$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback);
expect(fakeDocument.$$scripts.length).toBe(1);

var script = fakeDocument.$$scripts.shift(),
url = script.src.match(SCRIPT_URL);

expect(url[1]).toBe('http://example.org/path');
callbacks[url[2]]('some-data');
var callbackHanger = 'ngJsonpCallback_' + url[2];
$window[callbackHanger]('some-data');

if (script.onreadystatechange) {
script.readyState = 'complete';
Expand All @@ -267,9 +268,9 @@ describe('$httpBackend', function() {


var script = fakeDocument.$$scripts.shift(),
callbackId = script.src.match(SCRIPT_URL)[2];
callbackHanger = 'ngJsonpCallback_' + script.src.match(SCRIPT_URL)[2];

callbacks[callbackId]('some-data');
$window[callbackHanger]('some-data');

if (script.onreadystatechange) {
script.readyState = 'complete';
Expand All @@ -278,7 +279,7 @@ describe('$httpBackend', function() {
script.onload()
}

expect(callbacks[callbackId]).toBeUndefined();
expect($window[callbackHanger]).toBeUndefined();
expect(fakeDocument.body.removeChild).toHaveBeenCalledOnceWith(script);
});

Expand Down Expand Up @@ -344,7 +345,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 200 if content', function() {
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
$backend = createHttpBackend($browser, $window, MockXhr, null, null, 'http');

$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');
Expand All @@ -355,7 +356,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 200 if content - relative url', function() {
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
$backend = createHttpBackend($browser, $window, MockXhr, null, null, 'file');

$backend('GET', '/whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');
Expand All @@ -366,7 +367,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 404 if no content', function() {
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
$backend = createHttpBackend($browser, $window, MockXhr, null, null, 'http');

$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, '');
Expand All @@ -377,7 +378,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 200 if content - relative url', function() {
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
$backend = createHttpBackend($browser, $window, MockXhr, null, null, 'file');

$backend('GET', '/whatever/index.html', null, callback);
respond(0, '');
Expand Down