fix($location): strip off empty hash segments when comparing

Backported from e93710fe0e

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
Closes #10748
This commit is contained in:
Thibault Leruitte
2015-01-14 11:36:23 +01:00
committed by Peter Bacon Darwin
parent 14c50a12a3
commit e81b2f726c
2 changed files with 21 additions and 2 deletions
+7 -2
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);
@@ -724,10 +728,11 @@ function $LocationProvider(){
// update browser
var changeCounter = 0;
$rootScope.$watch(function $locationWatch() {
var oldUrl = $browser.url();
var oldUrl = trimEmptyHash($browser.url());
var newUrl = trimEmptyHash($location.absUrl());
var currentReplace = $location.$$replace;
if (!changeCounter || oldUrl != $location.absUrl()) {
if (!changeCounter || oldUrl != newUrl) {
changeCounter++;
$rootScope.$evalAsync(function() {
if ($rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).
+14
View File
@@ -550,6 +550,20 @@ 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) {
$rootScope.$digest();
$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}));