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

WIP - feat($location): make URL encoding/decoding configurable #16506

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1405,20 +1405,26 @@ function tryDecodeURIComponent(value) {
* Parses an escaped url query string into key-value pairs.
* @returns {Object.<string,boolean|Array>}
*/
function parseKeyValue(/**string*/keyValue) {
function parseKeyValue(/**string*/keyValue, decodeQueryKeyValue) {
var decode = decodeQueryKeyValue || function(x) {
return tryDecodeURIComponent(x.replace(/\+/g, '%20'));
};

var obj = {};

forEach((keyValue || '').split('&'), function(keyValue) {
var splitPoint, key, val;
if (keyValue) {
key = keyValue = keyValue.replace(/\+/g,'%20');
splitPoint = keyValue.indexOf('=');
if (splitPoint !== -1) {
if (splitPoint === -1) {
key = keyValue;
} else {
key = keyValue.substring(0, splitPoint);
val = keyValue.substring(splitPoint + 1);
}
key = tryDecodeURIComponent(key);
key = decode(key);
if (isDefined(key)) {
val = isDefined(val) ? tryDecodeURIComponent(val) : true;
val = isDefined(val) ? decode(val) : true;
if (!hasOwnProperty.call(obj, key)) {
obj[key] = val;
} else if (isArray(obj[key])) {
Expand All @@ -1429,23 +1435,27 @@ function parseKeyValue(/**string*/keyValue) {
}
}
});

return obj;
}

function toKeyValue(obj) {
function toKeyValue(obj, encodeQueryKeyValue) {
var encode = encodeQueryKeyValue || function(x) { return encodeUriQuery(x, true); };
var processPair = function(key, value) {
parts.push(encode(String(key)) + (value === true ? '' : '=' + encode(String(value))));
};

var parts = [];

forEach(obj, function(value, key) {
if (isArray(value)) {
forEach(value, function(arrayValue) {
parts.push(encodeUriQuery(key, true) +
(arrayValue === true ? '' : '=' + encodeUriQuery(arrayValue, true)));
});
forEach(value, processPair.bind(null, key));
} else {
parts.push(encodeUriQuery(key, true) +
(value === true ? '' : '=' + encodeUriQuery(value, true)));
processPair(key, value);
}
});
return parts.length ? parts.join('&') : '';

return parts.join('&');
}


Expand Down
12 changes: 12 additions & 0 deletions src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
$xhrFactoryProvider,
$jsonpCallbacksProvider,
$LocationProvider,
$$DecodePathSegmentProvider,
$$DecodeQueryKeyValueProvider,
$$DecodeHashProvider,
$$EncodePathSegmentProvider,
$$EncodeQueryKeyValueProvider,
$$EncodeHashProvider,
$LogProvider,
$$MapProvider,
$ParseProvider,
Expand Down Expand Up @@ -246,6 +252,12 @@ function publishExternalAPI(angular) {
$xhrFactory: $xhrFactoryProvider,
$jsonpCallbacks: $jsonpCallbacksProvider,
$location: $LocationProvider,
$$decodePathSegment: $$DecodePathSegmentProvider,
$$decodeQueryKeyValue: $$DecodeQueryKeyValueProvider,
$$decodeHash: $$DecodeHashProvider,
$$encodePathSegment: $$EncodePathSegmentProvider,
$$encodeQueryKeyValue: $$EncodeQueryKeyValueProvider,
$$encodeHash: $$EncodeHashProvider,
$log: $LogProvider,
$parse: $ParseProvider,
$rootScope: $RootScopeProvider,
Expand Down
147 changes: 117 additions & 30 deletions src/ng/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ var $locationMinErr = minErr('$location');
* Encode path using encodeUriSegment, ignoring forward slashes
*
* @param {string} path Path to encode
* @param {Function} encodePathSegment Function for encoding a path segment
* @returns {string}
*/
function encodePath(path) {
function encodePath(path, encodePathSegment) {
var segments = path.split('/'),
i = segments.length;

while (i--) {
// decode forward slashes to prevent them from being double encoded
segments[i] = encodeUriSegment(segments[i].replace(/%2F/g, '/'));
segments[i] = encodePathSegment(segments[i]);
}

return segments.join('/');
}

function decodePath(path, html5Mode) {
function decodePath(path, decodePathSegment) {
var segments = path.split('/'),
i = segments.length;

while (i--) {
segments[i] = decodeURIComponent(segments[i]);
if (html5Mode) {
// encode forward slashes to prevent them from being mistaken for path separators
segments[i] = segments[i].replace(/\//g, '%2F');
}
segments[i] = decodePathSegment(segments[i]);
}

return segments.join('/');
Expand All @@ -47,7 +43,7 @@ function parseAbsoluteUrl(absoluteUrl, locationObj) {
}

var DOUBLE_SLASH_REGEX = /^\s*[\\/]{2,}/;
function parseAppUrl(url, locationObj, html5Mode) {
function parseAppUrl(url, locationObj) {

if (DOUBLE_SLASH_REGEX.test(url)) {
throw $locationMinErr('badpath', 'Invalid url "{0}".', url);
Expand All @@ -59,9 +55,9 @@ function parseAppUrl(url, locationObj, html5Mode) {
}
var match = urlResolve(url);
var path = prefixed && match.pathname.charAt(0) === '/' ? match.pathname.substring(1) : match.pathname;
locationObj.$$path = decodePath(path, html5Mode);
locationObj.$$search = parseKeyValue(match.search);
locationObj.$$hash = decodeURIComponent(match.hash);
locationObj.$$path = decodePath(path, locationObj.$$decodePathSegment);
locationObj.$$search = parseKeyValue(match.search, locationObj.$$decodeQueryKeyValue);
locationObj.$$hash = locationObj.$$decodeHash(match.hash);

// make sure path starts with '/';
if (locationObj.$$path && locationObj.$$path.charAt(0) !== '/') {
Expand Down Expand Up @@ -134,7 +130,7 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
appBaseNoFile);
}

parseAppUrl(pathUrl, this, true);
parseAppUrl(pathUrl, this);

if (!this.$$path) {
this.$$path = '/';
Expand All @@ -148,10 +144,11 @@ function LocationHtml5Url(appBase, appBaseNoFile, basePrefix) {
* @private
*/
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
var path = encodePath(this.$$path, this.$$encodePathSegment),
search = toKeyValue(this.$$search, this.$$encodeQueryKeyValue),
hash = this.$$hash ? '#' + this.$$encodeHash(this.$$hash) : '';

this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$url = path + (search ? '?' + search : '') + hash;
this.$$absUrl = appBaseNoFile + this.$$url.substr(1); // first char is always '/'

this.$$urlUpdatedByLocation = true;
Expand Down Expand Up @@ -237,7 +234,7 @@ function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
}
}

parseAppUrl(withoutHashUrl, this, false);
parseAppUrl(withoutHashUrl, this);

this.$$path = removeWindowsDriveName(this.$$path, withoutHashUrl, appBase);

Expand Down Expand Up @@ -283,10 +280,11 @@ function LocationHashbangUrl(appBase, appBaseNoFile, hashPrefix) {
* @private
*/
this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
var path = encodePath(this.$$path, this.$$encodePathSegment),
search = toKeyValue(this.$$search, this.$$encodeQueryKeyValue),
hash = this.$$hash ? '#' + this.$$encodeHash(this.$$hash) : '';

this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
this.$$url = path + (search ? '?' + search : '') + hash;
this.$$absUrl = appBase + (this.$$url ? hashPrefix + this.$$url : '');

this.$$urlUpdatedByLocation = true;
Expand Down Expand Up @@ -341,16 +339,15 @@ function LocationHashbangInHtml5Url(appBase, appBaseNoFile, hashPrefix) {
};

this.$$compose = function() {
var search = toKeyValue(this.$$search),
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
var path = encodePath(this.$$path, this.$$encodePathSegment),
search = toKeyValue(this.$$search, this.$$encodeQueryKeyValue),
hash = this.$$hash ? '#' + this.$$encodeHash(this.$$hash) : '';

this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
// include hashPrefix in $$absUrl when $$url is empty so IE9 does not reload page because of removal of '#'
this.$$url = path + (search ? '?' + search : '') + hash;
this.$$absUrl = appBase + hashPrefix + this.$$url;

this.$$urlUpdatedByLocation = true;
};

}


Expand Down Expand Up @@ -422,9 +419,9 @@ var locationPrototype = {
}

var match = PATH_MATCH.exec(url);
if (match[1] || url === '') this.path(decodeURIComponent(match[1]));
if (match[1] || url === '') this.path(decodePath(match[1], this.$$decodePathSegment));
if (match[2] || match[1] || url === '') this.search(match[3] || '');
this.hash(match[5] || '');
this.hash(this.$$decodeHash(match[5] || ''));

return this;
},
Expand Down Expand Up @@ -578,7 +575,7 @@ var locationPrototype = {
case 1:
if (isString(search) || isNumber(search)) {
search = search.toString();
this.$$search = parseKeyValue(search);
this.$$search = parseKeyValue(search, this.$$decodeQueryKeyValue);
} else if (isObject(search)) {
search = copy(search, {});
// remove object undefined or null properties
Expand Down Expand Up @@ -706,6 +703,83 @@ function locationGetterSetter(property, preprocess) {
}


/**
* @private
* A function for decoding URL path segments.
*/
$$DecodePathSegmentProvider.$inject = ['$locationProvider'];
/** @this */ function $$DecodePathSegmentProvider($locationProvider) {
this.$get = ['$sniffer', function($sniffer) {
var html5Mode = $locationProvider.html5Mode().enabled && $sniffer.history;

return function(segment) {
segment = decodeURIComponent(segment);
if (html5Mode) {
// encode forward slashes to prevent them from being mistaken for path separators
segment = segment.replace(/\//g, '%2F');
}
return segment;
};
}];
}
/**
* @private
* A function for decoding URL query keys/values.
*/
/** @this */ function $$DecodeQueryKeyValueProvider() {
this.$get = function() {
return function(keyOrValue) {
return tryDecodeURIComponent(keyOrValue.replace(/\+/g, '%20'));
};
};
}
/**
* @private
* A function for decoding URL hash fragments.
*/
/** @this */ function $$DecodeHashProvider() {
this.$get = function() {
return function(hash) {
return decodeURIComponent(hash);
};
};
}
/**
* @private
* A function for encoding URL path segments.
*/
/** @this */ function $$EncodePathSegmentProvider() {
this.$get = function() {
return function(segment) {
// decode forward slashes to prevent them from being double encoded
return encodeUriSegment(segment.replace(/%2F/g, '/'));
};
};
}
/**
* @private
* A function for encoding URL query keys/values.
*/
/** @this */ function $$EncodeQueryKeyValueProvider() {
this.$get = function() {
return function(keyOrValue) {
return encodeUriQuery(keyOrValue, true);
};
};
}
/**
* @private
* A function for encoding URL hash fragments.
*/
/** @this */ function $$EncodeHashProvider() {
this.$get = function() {
return function(hash) {
return encodeUriSegment(hash);
};
};
}


/**
* @ngdoc service
* @name $location
Expand Down Expand Up @@ -852,7 +926,11 @@ function $LocationProvider() {
*/

this.$get = ['$rootScope', '$browser', '$sniffer', '$rootElement', '$window',
function($rootScope, $browser, $sniffer, $rootElement, $window) {
'$$decodePathSegment', '$$decodeQueryKeyValue', '$$decodeHash',
'$$encodePathSegment', '$$encodeQueryKeyValue', '$$encodeHash',
function($rootScope, $browser, $sniffer, $rootElement, $window,
$$decodePathSegment, $$decodeQueryKeyValue, $$decodeHash,
$$encodePathSegment, $$encodeQueryKeyValue, $$encodeHash) {
var $location,
LocationMode,
baseHref = $browser.baseHref(), // if base[href] is undefined, it defaults to ''
Expand All @@ -872,6 +950,15 @@ function $LocationProvider() {
}
var appBaseNoFile = stripFile(appBase);

extend(LocationMode.prototype, {
$$decodePathSegment: $$decodePathSegment,
$$decodeQueryKeyValue: $$decodeQueryKeyValue,
$$decodeHash: $$decodeHash,
$$encodePathSegment: $$encodePathSegment,
$$encodeQueryKeyValue: $$encodeQueryKeyValue,
$$encodeHash: $$encodeHash
});

$location = new LocationMode(appBase, appBaseNoFile, '#' + hashPrefix);
$location.$$parseLinkUrl(initialUrl, initialUrl);

Expand Down
Loading