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

Commit efb1706

Browse files
committed
fixup! fix($location): avoid unnecessary $locationChange* events due to empty hash
1 parent e6fa24f commit efb1706

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

src/ng/browser.js

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

44
function getHash(url) {
55
var index = url.indexOf('#');
66
return index === -1 ? '' : url.substr(index);
77
}
88

9+
function trimEmptyHash(url) {
10+
return url.replace(/#$/, '');
11+
}
12+
913
/**
1014
* ! This is a private undocumented service !
1115
*
@@ -143,7 +147,7 @@ function Browser(window, document, $log, $sniffer, $$taskTrackerFactory) {
143147
// - pendingLocation is needed as browsers don't allow to read out
144148
// the new location.href if a reload happened or if there is a bug like in iOS 9 (see
145149
// https://openradar.appspot.com/22186109).
146-
return pendingLocation || trimEmptyHash(location.href);
150+
return trimEmptyHash(pendingLocation || location.href);
147151
}
148152
};
149153

src/ng/location.js

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

44
var PATH_MATCH = /^([^?#]*)(\?([^#]*))?(#(.*))?$/,
55
DEFAULT_PORTS = {'http': 80, 'https': 443, 'ftp': 21};
@@ -95,17 +95,11 @@ function stripBaseUrl(base, url) {
9595
}
9696
}
9797

98-
9998
function stripHash(url) {
10099
var index = url.indexOf('#');
101100
return index === -1 ? url : url.substr(0, index);
102101
}
103102

104-
function trimEmptyHash(url) {
105-
return url.replace(/#$/, '');
106-
}
107-
108-
109103
function stripFile(url) {
110104
return url.substr(0, stripHash(url).lastIndexOf('/') + 1);
111105
}
@@ -944,7 +938,7 @@ function $LocationProvider() {
944938

945939

946940
// rewrite hashbang url <> html5 url
947-
if (trimEmptyHash($location.absUrl()) !== trimEmptyHash(initialUrl)) {
941+
if ($location.absUrl() !== initialUrl) {
948942
$browser.url($location.absUrl(), true);
949943
}
950944

@@ -963,7 +957,6 @@ function $LocationProvider() {
963957
var oldUrl = $location.absUrl();
964958
var oldState = $location.$$state;
965959
var defaultPrevented;
966-
newUrl = trimEmptyHash(newUrl);
967960
$location.$$parse(newUrl);
968961
$location.$$state = newState;
969962

@@ -991,8 +984,8 @@ function $LocationProvider() {
991984
if (initializing || $location.$$urlUpdatedByLocation) {
992985
$location.$$urlUpdatedByLocation = false;
993986

994-
var oldUrl = trimEmptyHash($browser.url());
995-
var newUrl = trimEmptyHash($location.absUrl());
987+
var oldUrl = $browser.url();
988+
var newUrl = $location.absUrl();
996989
var oldState = $browser.state();
997990
var currentReplace = $location.$$replace;
998991
var urlOrStateChanged = !urlsEqual(oldUrl, newUrl) ||

src/ngMock/angular-mocks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
/* global routeToRegExp: false */
3+
/* global routeToRegExp: false, trimEmptyHash: false */
44

55
/**
66
* @ngdoc object
@@ -225,7 +225,7 @@ angular.mock.$Browser.prototype = {
225225
state = null;
226226
}
227227
if (url) {
228-
this.$$url = url;
228+
this.$$url = trimEmptyHash(url);
229229
// Native pushState serializes & copies the object; simulate it.
230230
this.$$state = angular.copy(state);
231231
return this;

0 commit comments

Comments
 (0)