Skip to content

Commit

Permalink
fixup! fix(router): fix URL serialization so special characters are o…
Browse files Browse the repository at this point in the history
…nly encoded where needed
  • Loading branch information
jasonaden committed Feb 21, 2018
1 parent 0e008dd commit 5431d29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
12 changes: 7 additions & 5 deletions packages/router/src/url_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,7 @@ function encodeUriString(s: string, type: 'query' | 'segment' = 'query'): string
.replace(/%40/g, '@')
.replace(/%3A/gi, ':')
.replace(/%24/g, '$')
.replace(/%2C/gi, ',')
.replace(/%5B/gi, '[')
.replace(/%5D/gi, ']');
.replace(/%2C/gi, ',');

return encoded;
}
Expand All @@ -354,15 +352,19 @@ function encodeUriString(s: string, type: 'query' | 'segment' = 'query'): string
* encode both keys and values in a query string key/value pair as well as the fragment string.
*/
export function encodeUriQuery(s: string): string {
return encodeUriString(s).replace(/%3B/gi, ';').replace(/%3F/gi, '?').replace(/%2F/gi, '/');
return encodeUriString(s).replace(/%3B/gi, ';');
}

/**
* Encodes for the URI segment. This is the path portion of a URI, before the query string
* and/or fragment.
*/
export function encodeUriSegment(s: string): string {
return encodeUriString(s).replace(/\(/g, '%28').replace(/\)/g, '%29');
return encodeUriString(s)
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/%2B/gi, '+')
.replace(/%26/gi, '&');
}

export function decode(s: string): string {
Expand Down
24 changes: 12 additions & 12 deletions packages/router/test/url_serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ describe('url serializer', () => {
});

it('should encode query params leaving sub-delimiters intact', () => {
const percentChars = '#&+= ';
const percentCharsEncoded = '%23%26%2B%3D%20';
const intactChars = '/?!$\'[]()*,;:';
const percentChars = '/?#&+=[] ';
const percentCharsEncoded = '%2F%3F%23%26%2B%3D%5B%5D%20';
const intactChars = '!$\'()*,;:';
const params = percentChars + intactChars;
const paramsEncoded = percentCharsEncoded + intactChars;
const mixedCaseString = 'sTrInG';
Expand Down Expand Up @@ -294,9 +294,9 @@ describe('url serializer', () => {
const unreserved = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~`;

it('should encode a minimal set of special characters in queryParams & fragment', () => {
const notEncoded = unreserved + `:/?[]@!$'*,();`;
const encode = ` +%&=#`;
const encoded = `%20%2B%25%26%3D%23`;
const notEncoded = unreserved + `:@!$'*,();`;
const encode = ` +%&=#/?[]`;
const encoded = `%20%2B%25%26%3D%23%2F%3F%5B%5D`;

const parsed = url.parse('/foo');

Expand All @@ -309,9 +309,9 @@ describe('url serializer', () => {

it('should encode minimal special characters plus parens and semi-colon in matrix params',
() => {
const notEncoded = unreserved + `:[]@!$'*,`;
const encode = ` /+%&=#();?`;
const encoded = `%20%2F%2B%25%26%3D%23%28%29%3B%3F`;
const notEncoded = unreserved + `:@!$'*,+&`;
const encode = ` /%=#()[];?`;
const encoded = `%20%2F%25%3D%23%28%29%5B%5D%3B%3F`;

const parsed = url.parse('/foo');

Expand All @@ -321,9 +321,9 @@ describe('url serializer', () => {
});

it('should encode special characters in the path the same as matrix params', () => {
const notEncoded = unreserved + `:[]@!$'*,`;
const encode = ` /+%&=#();?`;
const encoded = `%20%2F%2B%25%26%3D%23%28%29%3B%3F`;
const notEncoded = unreserved + `:@!$'*,+&`;
const encode = ` /%=#()[];?`;
const encoded = `%20%2F%25%3D%23%28%29%5B%5D%3B%3F`;

const parsed = url.parse('/foo');

Expand Down

0 comments on commit 5431d29

Please sign in to comment.