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

Commit e93710f

Browse files
fix($location): strip off empty hash segments when comparing
The url is the same whether or not there is an empty `#` marker at the end. This prevents unwanted calls to update the browser, since the browser is automatically applying an empty hash if necessary to prevent page reloads. Closes #9635
1 parent 5481e2c commit e93710f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/ng/location.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function stripHash(url) {
6868
return index == -1 ? url : url.substr(0, index);
6969
}
7070

71+
function trimEmptyHash(url) {
72+
return url.replace(/(#.+)|#$/, '$1');
73+
}
74+
7175

7276
function stripFile(url) {
7377
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
@@ -903,10 +907,11 @@ function $LocationProvider() {
903907

904908
// update browser
905909
$rootScope.$watch(function $locationWatch() {
906-
var oldUrl = $browser.url();
910+
var oldUrl = trimEmptyHash($browser.url());
911+
var newUrl = trimEmptyHash($location.absUrl());
907912
var oldState = $browser.state();
908913
var currentReplace = $location.$$replace;
909-
var urlOrStateChanged = oldUrl !== $location.absUrl() ||
914+
var urlOrStateChanged = oldUrl !== newUrl ||
910915
($location.$$html5 && $sniffer.history && oldState !== $location.$$state);
911916

912917
if (initializing || urlOrStateChanged) {

test/ng/locationSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,18 @@ describe('$location', function() {
647647
};
648648
}
649649

650+
describe('location watch', function() {
651+
beforeEach(initService({supportHistory: true}));
652+
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));
653+
654+
it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
655+
$browser.url('http://new.com/a/b#');
656+
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
657+
$rootScope.$digest();
658+
expect($browserUrl).not.toHaveBeenCalled();
659+
}));
660+
});
661+
650662
describe('wiring', function() {
651663

652664
beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));

0 commit comments

Comments
 (0)