From eefcee3d19eefcc3b166caacbede4bc2b47abe68 Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Sat, 23 Jun 2018 18:39:01 -0700 Subject: [PATCH] refactor($location): move repeated path normalization code into helper method --- src/ng/location.js | 57 +++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 33 deletions(-) diff --git a/src/ng/location.js b/src/ng/location.js index aeb3a3f7ca9c..cc064becd727 100644 --- a/src/ng/location.js +++ b/src/ng/location.js @@ -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