Skip to content

Commit 8f7cce3

Browse files
committed
fix(router): encode URLs the same way AngularJS did (closer to spec) (#17890)
fixes #16067
1 parent a318093 commit 8f7cce3

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎packages/router/src/url_tree.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,24 @@ function serializeSegment(segment: UrlSegmentGroup, root: boolean): string {
325325
}
326326
}
327327

328+
/**
329+
* This method is intended for encoding *key* or *value* parts of query component. We need a custom
330+
* method because encodeURIComponent is too aggressive and encodes stuff that doesn't have to be
331+
* encoded per http://tools.ietf.org/html/rfc3986:
332+
* query = *( pchar / "/" / "?" )
333+
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@"
334+
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
335+
* pct-encoded = "%" HEXDIG HEXDIG
336+
* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
337+
* / "*" / "+" / "," / ";" / "="
338+
*/
328339
export function encode(s: string): string {
329-
return encodeURIComponent(s);
340+
return encodeURIComponent(s)
341+
.replace(/%40/g, '@')
342+
.replace(/%3A/gi, ':')
343+
.replace(/%24/g, '$')
344+
.replace(/%2C/gi, ',')
345+
.replace(/%3B/gi, ';');
330346
}
331347

332348
export function decode(s: string): string {

‎packages/router/test/url_serializer.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ describe('url serializer', () => {
219219
expect(url.serialize(tree)).toEqual(u);
220220
});
221221

222+
it('should encode query params leaving sub-delimiters intact', () => {
223+
const percentChars = '/?#[]&+= ';
224+
const percentCharsEncoded = '%2F%3F%23%5B%5D%26%2B%3D%20';
225+
const intactChars = '!$\'()*,;:';
226+
const params = percentChars + intactChars;
227+
const paramsEncoded = percentCharsEncoded + intactChars;
228+
const mixedCaseString = 'sTrInG';
229+
230+
expect(percentCharsEncoded).toEqual(encode(percentChars));
231+
expect(intactChars).toEqual(encode(intactChars));
232+
// Verify it replaces repeated characters correctly
233+
expect(paramsEncoded + paramsEncoded).toEqual(encode(params + params));
234+
// Verify it doesn't change the case of alpha characters
235+
expect(mixedCaseString + paramsEncoded).toEqual(encode(mixedCaseString + params));
236+
});
237+
222238
it('should encode/decode fragment', () => {
223239
const u = `/one#${encodeURI("one two=three four")}`;
224240
const tree = url.parse(u);

‎packages/router/upgrade/src/upgrade.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ export function setUpLocationSync(ngUpgrade: UpgradeModule) {
7171
ngUpgrade.$injector.get('$rootScope')
7272
.$on('$locationChangeStart', (_: any, next: string, __: string) => {
7373
url.href = next;
74-
router.navigateByUrl(url.pathname + url.search);
74+
router.navigateByUrl(url.pathname + url.search + url.hash);
7575
});
7676
}

0 commit comments

Comments
 (0)