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

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

Merged
merged 1 commit into from Jul 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 24 additions & 33 deletions src/ng/location.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
};

}


Expand All @@ -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
Expand Down