refactor($location): move repeated path normalization code into helper method (#16618)

Closes #16618
This commit is contained in:
Jason Bedard
2018-07-21 02:39:38 -07:00
committed by GitHub
parent c2d0783ca9
commit ff60b3f47a
+24 -33
View File
@@ -38,6 +38,14 @@ function decodePath(path, html5Mode) {
return segments.join('/');
}
function normalizePath(pathValue, searchValue, hashValue) {
var search = toKeyValue(searchValue),
hash = hashValue ? '#' + encodeUriSegment(hashValue) : '',
path = encodePath(pathValue);
return path + (search ? '?' + search : '') + hash;
}
function parseAbsoluteUrl(absoluteUrl, locationObj) {
var parsedUrl = urlResolve(absoluteUrl);
@@ -143,18 +151,8 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
this.$$compose();
};
/**
* Compose url and update `absUrl` property
* @private
*/
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$absUrl = appBaseNoFile + this.$$url.substr(1); // first char is always '/'
this.$$urlUpdatedByLocation = true;
this.$$normalizeUrl = function(url) {
return appBaseNoFile + url.substr(1); // first char is always '/'
};
this.$$parseLinkUrl = function(url, relHref) {
@@ -278,18 +276,8 @@ function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
}
};
/**
* Compose hashbang URL and update `absUrl` property
* @private
*/
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$absUrl = appBase + (this.$$url ? hashPrefix + this.$$url : '');
this.$$urlUpdatedByLocation = true;
this.$$normalizeUrl = function(url) {
return appBase + (url ? hashPrefix + url : '');
};
this.$$parseLinkUrl = function(url, relHref) {
@@ -340,17 +328,10 @@ function LocationHashbangInHtml5Url(appBase, appBaseNoFile, hashPrefix) {
return !!rewrittenUrl;
};
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$normalizeUrl = function(url) {
// include hashPrefix in $$absUrl when $$url is empty so IE9 does not reload page because of removal of '#'
this.$$absUrl = appBase + hashPrefix + this.$$url;
this.$$urlUpdatedByLocation = true;
return appBase + hashPrefix + url;
};
}
@@ -374,6 +355,16 @@ var locationPrototype = {
*/
$$replace: false,
/**
* Compose url and update `url` and `absUrl` property
* @private
*/
$$compose: function() {
this.$$url = normalizePath(this.$$path, this.$$search, this.$$hash);
this.$$absUrl = this.$$normalizeUrl(this.$$url);
this.$$urlUpdatedByLocation = true;
},
/**
* @ngdoc method
* @name $location#absUrl