Skip to content

Commit a201906

Browse files
cvnchristopherthielen
authored andcommitted
fix(urlMatcherFactory): fix tilde edge case with "string" encoding (#3018)
makes it possible to bidirectionally encode/decode "~2F"
1 parent dd2f101 commit a201906

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/params/paramTypes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {ParamTypeDefinition} from "./interface";
1010
// If the slashes are simply URLEncoded, the browser can choose to pre-decode them,
1111
// and bidirectional encoding/decoding fails.
1212
// Tilde was chosen because it's not a RFC 3986 section 2.2 Reserved Character
13-
function valToString(val: any) { return val != null ? val.toString().replace(/~/g, "~~").replace(/\//g, "~2F") : val; }
14-
function valFromString(val: string) { return val != null ? val.toString().replace(/~2F/g, "/").replace(/~~/g, "~") : val; }
13+
function valToString(val: any) { return val != null ? val.toString().replace(/(~|\/)/g, m => ({'~':'~~', '/':'~2F'}[m])) : val; }
14+
function valFromString(val: string) { return val != null ? val.toString().replace(/(~~|~2F)/g, m => ({'~~':'~', '~2F':'/'}[m])) : val; }
1515

1616
export class ParamTypes {
1717
types: any;

test/ng1/urlMatcherFactorySpec.js

+2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ describe("UrlMatcher", function () {
111111

112112
expect(matcher1.format({ foo: "abc" })).toBe('/abc');
113113
expect(matcher1.format({ foo: "~abc" })).toBe('/~~abc');
114+
expect(matcher1.format({ foo: "~2F" })).toBe('/~~2F');
114115

115116
expect(matcher1.exec('/abc').foo).toBe("abc");
116117
expect(matcher1.exec('/~~abc').foo).toBe("~abc");
118+
expect(matcher1.exec('/~~2F').foo).toBe("~2F");
117119
});
118120

119121
describe("snake-case parameters", function() {

0 commit comments

Comments
 (0)