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

Commit e6fa24f

Browse files
committed
fix($location): avoid unnecessary $locationChange* events due to empty hash
Fixes #16632
1 parent e713549 commit e6fa24f

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

src/ng/browser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
/* global getHash: true, stripHash: false */
2+
/* global getHash: true, stripHash: false, trimEmptyHash: false */
33

44
function getHash(url) {
55
var index = url.indexOf('#');
@@ -143,7 +143,7 @@ function Browser(window, document, $log, $sniffer, $$taskTrackerFactory) {
143143
// - pendingLocation is needed as browsers don't allow to read out
144144
// the new location.href if a reload happened or if there is a bug like in iOS 9 (see
145145
// https://openradar.appspot.com/22186109).
146-
return pendingLocation || location.href;
146+
return pendingLocation || trimEmptyHash(location.href);
147147
}
148148
};
149149

src/ng/location.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
/* global stripHash: true */
2+
/* global stripHash: true, trimEmptyHash: true */
33

44
var PATH_MATCH = /^([^?#]*)(\?([^#]*))?(#(.*))?$/,
55
DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp': 21};

test/ng/browserSpecs.js

+34
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,14 @@ describe('browser', function() {
404404
expect(browser.url()).toEqual('https://another.com');
405405
});
406406

407+
it('should strip an empty hash fragment', function() {
408+
fakeWindow.location.href = 'http://test.com#';
409+
expect(browser.url()).toEqual('http://test.com');
410+
411+
fakeWindow.location.href = 'https://another.com#foo';
412+
expect(browser.url()).toEqual('https://another.com#foo');
413+
});
414+
407415
it('should use history.pushState when available', function() {
408416
sniffer.history = true;
409417
browser.url('http://new.org');
@@ -1047,6 +1055,32 @@ describe('browser', function() {
10471055
expect($location.absUrl()).toEqual('http://server/#otherHash');
10481056
});
10491057
});
1058+
1059+
// issue #16632
1060+
it('should not trigger `$locationChangeStart` more than once due to trailing `#`', function() {
1061+
setup({
1062+
history: true,
1063+
html5Mode: true
1064+
});
1065+
1066+
inject(function($flushPendingTasks, $location, $rootScope) {
1067+
$rootScope.$digest();
1068+
1069+
const spy = jasmine.createSpy('$locationChangeStart');
1070+
$rootScope.$on('$locationChangeStart', spy);
1071+
1072+
$rootScope.$evalAsync(function() {
1073+
fakeWindow.location.href += '#';
1074+
});
1075+
$rootScope.$digest();
1076+
1077+
expect(fakeWindow.location.href).toBe('http://server/#');
1078+
expect($location.absUrl()).toBe('http://server/');
1079+
1080+
expect(spy.calls.count()).toBe(0);
1081+
expect(spy).not.toHaveBeenCalled();
1082+
});
1083+
});
10501084
});
10511085

10521086
describe('integration test with $rootScope', function() {

test/ng/locationSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ describe('$location', function() {
709709
initService({html5Mode: true, supportHistory: true});
710710
mockUpBrowser({initialUrl: 'http://new.com/#', baseHref: '/'});
711711
inject(function($browser, $location, $window) {
712-
expect($browser.url()).toBe('http://new.com/#');
712+
expect($browser.url()).toBe('http://new.com/');
713713
expect($location.absUrl()).toBe('http://new.com/');
714714
expect($window.location.href).toBe('http://new.com/#');
715715
});

0 commit comments

Comments
 (0)