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

Commit 10ac594

Browse files
fix($browser): prevent infinite digests when clearing the hash of a url
By using `location.hash` to update the current browser location when only the hash has changed, we prevent the browser from attempting to reload. Closes #9629 Closes #9635 Closes #10228 Closes #10308
1 parent d21dff2 commit 10ac594

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/ng/browser.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ function Browser(window, document, $log, $sniffer) {
6161
}
6262
}
6363

64+
function getHash(url) {
65+
var index = url.indexOf('#');
66+
return index === -1 ? '' : url.substr(index + 1);
67+
}
68+
6469
/**
6570
* @private
6671
* Note: this method is used only by scenario runner
@@ -190,8 +195,10 @@ function Browser(window, document, $log, $sniffer) {
190195
}
191196
if (replace) {
192197
location.replace(url);
193-
} else {
198+
} else if (!sameBase) {
194199
location.href = url;
200+
} else {
201+
location.hash = getHash(url);
195202
}
196203
}
197204
return self;

test/ng/browserSpecs.js

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
/* global getHash:true, stripHash:true */
4+
35
var historyEntriesLength;
46
var sniffer = {};
57

@@ -51,6 +53,12 @@ function MockWindow(options) {
5153
mockWindow.history.state = null;
5254
historyEntriesLength++;
5355
},
56+
get hash() {
57+
return getHash(locationHref);
58+
},
59+
set hash(value) {
60+
locationHref = stripHash(locationHref) + '#' + value;
61+
},
5462
replace: function(url) {
5563
locationHref = url;
5664
mockWindow.history.state = null;
@@ -550,6 +558,17 @@ describe('browser', function() {
550558
expect(locationReplace).not.toHaveBeenCalled();
551559
});
552560

561+
it("should retain the # character when the only change is clearing the hash fragment, to prevent page reload", function() {
562+
sniffer.history = true;
563+
564+
browser.url('http://server/#123');
565+
expect(fakeWindow.location.href).toEqual('http://server/#123');
566+
567+
browser.url('http://server/');
568+
expect(fakeWindow.location.href).toEqual('http://server/#');
569+
570+
});
571+
553572
it('should use location.replace when history.replaceState not available', function() {
554573
sniffer.history = false;
555574
browser.url('http://new.org', true);
@@ -561,6 +580,7 @@ describe('browser', function() {
561580
expect(fakeWindow.location.href).toEqual('http://server/');
562581
});
563582

583+
564584
it('should use location.replace and not use replaceState when the url only changed in the hash fragment to please IE10/11', function() {
565585
sniffer.history = true;
566586
browser.url('http://server/#123', true);
@@ -572,6 +592,7 @@ describe('browser', function() {
572592
expect(fakeWindow.location.href).toEqual('http://server/');
573593
});
574594

595+
575596
it('should return $browser to allow chaining', function() {
576597
expect(browser.url('http://any.com')).toBe(browser);
577598
});

0 commit comments

Comments
 (0)