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

Commit f728b78

Browse files
committed
fix($location): retain original encoded path
Change location to retain the expected path encoding. The decode/encode cycle for the location path loses data as %3B (a semi-colon in the path) and ; (a sub-delim in the URI for path parameters) are considered to be different.
1 parent 0ea270e commit f728b78

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/Angular.js

-1
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,6 @@ function toKeyValue(obj) {
10651065
*/
10661066
function encodeUriSegment(val) {
10671067
return encodeUriQuery(val, true).
1068-
replace(/%3B/gi, ';').
10691068
replace(/%26/gi, '&').
10701069
replace(/%3D/gi, '=').
10711070
replace(/%2B/gi, '+');

src/ng/location.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ function parseAppUrl(relativeUrl, locationObj, appBase) {
3737
relativeUrl = '/' + relativeUrl;
3838
}
3939
var match = urlResolve(relativeUrl, appBase);
40-
locationObj.$$path = decodeURIComponent(prefixed && match.pathname.charAt(0) === '/' ?
41-
match.pathname.substring(1) : match.pathname);
42-
locationObj.$$search = parseKeyValue(match.search);
43-
locationObj.$$hash = decodeURIComponent(match.hash);
40+
locationObj.pathEncoded = prefixed && match.pathname.charAt(0) === '/' ?
41+
match.pathname.substring(1) : match.pathname;
4442

4543
// make sure path starts with '/';
46-
if (locationObj.$$path && locationObj.$$path.charAt(0) != '/') {
47-
locationObj.$$path = '/' + locationObj.$$path;
44+
if (locationObj.pathEncoded && locationObj.pathEncoded.charAt(0) != '/') {
45+
locationObj.pathEncoded = '/' + locationObj.pathEncoded;
4846
}
47+
48+
locationObj.$$path = decodeURIComponent(locationObj.pathEncoded);
49+
locationObj.$$search = parseKeyValue(match.search);
50+
locationObj.$$hash = decodeURIComponent(match.hash);
4951
}
5052

5153

@@ -123,7 +125,7 @@ function LocationHtml5Url(appBase, basePrefix) {
123125
var search = toKeyValue(this.$$search),
124126
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
125127

126-
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
128+
this.$$url = this.pathEncoded + (search ? '?' + search : '') + hash;
127129
this.$$absUrl = appBaseNoFile + this.$$url.substr(1); // first char is always '/'
128130
};
129131

@@ -190,7 +192,7 @@ function LocationHashbangUrl(appBase, hashPrefix) {
190192
var search = toKeyValue(this.$$search),
191193
hash = this.$$hash ? '#' + encodeUriSegment(this.$$hash) : '';
192194

193-
this.$$url = encodePath(this.$$path) + (search ? '?' + search : '') + hash;
195+
this.$$url = this.pathEncoded + (search ? '?' + search : '') + hash;
194196
this.$$absUrl = appBase + (this.$$url ? hashPrefix + this.$$url : '');
195197
};
196198

@@ -350,8 +352,10 @@ LocationHashbangInHtml5Url.prototype =
350352
* @param {string=} path New path
351353
* @return {string} path
352354
*/
353-
path: locationGetterSetter('$$path', function(path) {
354-
return path.charAt(0) == '/' ? path : '/' + path;
355+
path: locationGetterSetter('$$path', function(path, locationObj) {
356+
path = path.charAt(0) == '/' ? path : '/' + path;
357+
locationObj.pathEncoded = encodePath(path);
358+
return path;
355359
}),
356360

357361
/**
@@ -446,7 +450,7 @@ function locationGetterSetter(property, preprocess) {
446450
if (isUndefined(value))
447451
return this[property];
448452

449-
this[property] = preprocess(value);
453+
this[property] = preprocess(value, this);
450454
this.$$compose();
451455

452456
return this;

test/AngularSpec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,12 @@ describe('angular', function() {
569569
toEqual("-_.!~*'()%20-_.!~*'()");
570570

571571
//don't encode the rest of pchar'
572-
expect(encodeUriSegment(':;@&=+$, :;@&=+$,')).
573-
toEqual(':;@&=+$,%20:;@&=+$,');
572+
expect(encodeUriSegment(':@&=+$, :@&=+$,')).
573+
toEqual(':@&=+$,%20:@&=+$,');
574574

575-
//encode '/', and ' ''
576-
expect(encodeUriSegment('/ /')).
577-
toEqual('%2F%20%2F');
575+
//encode '/', ';' and ' ''
576+
expect(encodeUriSegment('/; /;')).
577+
toEqual('%2F%3B%20%2F%3B');
578578
});
579579
});
580580

0 commit comments

Comments
 (0)