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

Commit 900bb66

Browse files
fix($browser): prevent infinite digests when clearing the hash of a url
By retaining a trailing `#` character in the URL we ensure that the browser does not attempt a reload, which in turn allows us to read the correct `location.href` value. Closes #9635
1 parent b7e2b01 commit 900bb66

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/ng/browser.js

+8
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ function Browser(window, document, $log, $sniffer) {
188188
} else {
189189
if (!sameBase) {
190190
reloadLocation = url;
191+
} else {
192+
// If we are only changing the hash fragment then ensure that we retain a # character
193+
// to prevent the page reloading,
194+
// which stops us from reading the correct location.href,
195+
// which causes $location watches to trigger an infinite digest.
196+
if (url.indexOf('#') === -1) {
197+
url += '#';
198+
}
191199
}
192200
if (replace) {
193201
location.replace(url);

test/ng/browserSpecs.js

+25
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ describe('browser', function() {
550550
expect(locationReplace).not.toHaveBeenCalled();
551551
});
552552

553+
it("should retain the # character when the only change is clearing the hash fragment, to prevent page reload", function() {
554+
sniffer.history = true;
555+
556+
browser.url('http://server/#123');
557+
expect(fakeWindow.location.href).toEqual('http://server/#123');
558+
559+
browser.url('http://server/');
560+
expect(fakeWindow.location.href).toEqual('http://server/#');
561+
562+
});
563+
553564
it('should use location.replace when history.replaceState not available', function() {
554565
sniffer.history = false;
555566
browser.url('http://new.org', true);
@@ -561,6 +572,7 @@ describe('browser', function() {
561572
expect(fakeWindow.location.href).toEqual('http://server/');
562573
});
563574

575+
564576
it('should use location.replace and not use replaceState when the url only changed in the hash fragment to please IE10/11', function() {
565577
sniffer.history = true;
566578
browser.url('http://server/#123', true);
@@ -572,6 +584,19 @@ describe('browser', function() {
572584
expect(fakeWindow.location.href).toEqual('http://server/');
573585
});
574586

587+
588+
it("should retain the # character when replacing and the only change is clearing the hash fragment, to prevent page reload", function() {
589+
sniffer.history = true;
590+
browser.url('http://server/#123');
591+
592+
expect(fakeWindow.location.href).toEqual('http://server/#123');
593+
594+
browser.url('http://server/', true);
595+
596+
expect(locationReplace).toHaveBeenCalledWith('http://server/#');
597+
});
598+
599+
575600
it('should return $browser to allow chaining', function() {
576601
expect(browser.url('http://any.com')).toBe(browser);
577602
});

0 commit comments

Comments
 (0)