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
This commit is contained in:
Peter Bacon Darwin
2014-11-05 13:49:01 +00:00
parent 5481e2cfcd
commit e93710fe0e
2 changed files with 19 additions and 2 deletions

View File

@@ -68,6 +68,10 @@ function stripHash(url) {
return index == -1 ? url : url.substr(0, index);
}
function trimEmptyHash(url) {
return url.replace(/(#.+)|#$/, '$1');
}
function stripFile(url) {
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
@@ -903,10 +907,11 @@ function $LocationProvider() {
// update browser
$rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url();
var oldUrl = trimEmptyHash($browser.url());
var newUrl = trimEmptyHash($location.absUrl());
var oldState = $browser.state();
var currentReplace = $location.$$replace;
var urlOrStateChanged = oldUrl !== $location.absUrl() ||
var urlOrStateChanged = oldUrl !== newUrl ||
($location.$$html5 && $sniffer.history && oldState !== $location.$$state);
if (initializing || urlOrStateChanged) {

View File

@@ -647,6 +647,18 @@ describe('$location', function() {
};
}
describe('location watch', function() {
beforeEach(initService({supportHistory: true}));
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));
it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
$browser.url('http://new.com/a/b#');
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
$rootScope.$digest();
expect($browserUrl).not.toHaveBeenCalled();
}));
});
describe('wiring', function() {
beforeEach(initService({html5Mode:false,hashPrefix: '!',supportHistory: true}));