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

Commit e81b2f7

Browse files
Thibault Leruittepetebacondarwin
Thibault Leruitte
authored andcommitted
fix($location): strip off empty hash segments when comparing
Backported from e93710f 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
1 parent 14c50a1 commit e81b2f7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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);
@@ -724,10 +728,11 @@ function $LocationProvider(){
724728
// update browser
725729
var changeCounter = 0;
726730
$rootScope.$watch(function $locationWatch() {
727-
var oldUrl = $browser.url();
731+
var oldUrl = trimEmptyHash($browser.url());
732+
var newUrl = trimEmptyHash($location.absUrl());
728733
var currentReplace = $location.$$replace;
729734

730-
if (!changeCounter || oldUrl != $location.absUrl()) {
735+
if (!changeCounter || oldUrl != newUrl) {
731736
changeCounter++;
732737
$rootScope.$evalAsync(function() {
733738
if ($rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).

test/ng/locationSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,20 @@ describe('$location', function() {
550550
};
551551
}
552552

553+
describe('location watch', function() {
554+
beforeEach(initService({supportHistory: true}));
555+
beforeEach(inject(initBrowser({url:'http://new.com/a/b#'})));
556+
557+
it('should not update browser if only the empty hash fragment is cleared by updating the search', inject(function($rootScope, $browser, $location) {
558+
$rootScope.$digest();
559+
560+
$browser.url('http://new.com/a/b#');
561+
var $browserUrl = spyOnlyCallsWithArgs($browser, 'url').andCallThrough();
562+
$rootScope.$digest();
563+
expect($browserUrl).not.toHaveBeenCalled();
564+
}));
565+
});
566+
553567
describe('wiring', function() {
554568

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

0 commit comments

Comments
 (0)