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

Commit

Permalink
fix($location): prevent infinite digest error in IE7
Browse files Browse the repository at this point in the history
Refactored `replacedUrl` to store the new URL on both
`location.replace` and setting `location.href` directly to handle
delays in the actual location value change in IE.

Closes #2802
  • Loading branch information
Angel Balcarcel authored and btford committed Oct 2, 2013
1 parent 78a5889 commit d707114
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function Browser(window, document, $log, $sniffer) {

var lastBrowserUrl = location.href,
baseElement = document.find('base'),
replacedUrl = null;
newLocation = null;

/**
* @name ng.$browser#url
Expand Down Expand Up @@ -163,28 +163,28 @@ function Browser(window, document, $log, $sniffer) {
baseElement.attr('href', baseElement.attr('href'));
}
} else {
newLocation = url;
if (replace) {
location.replace(url);
replacedUrl = url;
} else {
location.href = url;
replacedUrl = null;
}
}
return self;
// getter
} else {
// - the replacedUrl is a workaround for an IE8-9 issue with location.replace method that doesn't update
// location.href synchronously
// - newLocation is a workaround for an IE7-9 issue with location.replace and location.href
// methods not updating location.href synchronously.
// - the replacement is a workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=407172
return replacedUrl || location.href.replace(/%27/g,"'");
return newLocation || location.href.replace(/%27/g,"'");
}
};

var urlChangeListeners = [],
urlChangeInit = false;

function fireUrlChange() {
newLocation = null;
if (lastBrowserUrl == self.url()) return;

lastBrowserUrl = self.url();
Expand Down
27 changes: 25 additions & 2 deletions test/ng/browserSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ describe('browser', function() {
it('should default path in cookie to "" (empty string)', function () {
browser.cookies('cookie', 'bender');
// This only fails in Safari and IE when cookiePath returns undefined
// Where it now succeeds since baseHref return '' instead of undefined
// Where it now succeeds since baseHref return '' instead of undefined
expect(document.cookie).toEqual('cookie=bender');
});
});
Expand Down Expand Up @@ -535,9 +535,32 @@ describe('browser', function() {
fakeWindow.setTimeout.flush();
expect(callback).toHaveBeenCalledWith('http://server.new');

callback.reset();

fakeWindow.fire('popstate');
fakeWindow.fire('hashchange');
expect(callback).toHaveBeenCalledOnce();
expect(callback).not.toHaveBeenCalled();
});

describe('after an initial location change by browser.url method when neither history nor hashchange supported', function() {
beforeEach(function() {
sniffer.history = false;
sniffer.hashchange = false;
browser.url("http://server.current");
});

it('should fire callback with the correct URL on location change outside of angular', function() {
browser.onUrlChange(callback);

fakeWindow.location.href = 'http://server.new';
fakeWindow.setTimeout.flush();
expect(callback).toHaveBeenCalledWith('http://server.new');

fakeWindow.fire('popstate');
fakeWindow.fire('hashchange');
expect(callback).toHaveBeenCalledOnce();
});

});

it('should not fire urlChange if changed by browser.url method (polling)', function() {
Expand Down

0 comments on commit d707114

Please sign in to comment.