Skip to content

Commit

Permalink
fix(urlMatcherFactory): fix tilde edge case with "string" encoding (#…
Browse files Browse the repository at this point in the history
…3018)

makes it possible to bidirectionally encode/decode "~2F"
  • Loading branch information
cvn authored and christopherthielen committed Sep 19, 2016
1 parent dd2f101 commit a201906
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/params/paramTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {ParamTypeDefinition} from "./interface";
// If the slashes are simply URLEncoded, the browser can choose to pre-decode them,
// and bidirectional encoding/decoding fails.
// Tilde was chosen because it's not a RFC 3986 section 2.2 Reserved Character
function valToString(val: any) { return val != null ? val.toString().replace(/~/g, "~~").replace(/\//g, "~2F") : val; }
function valFromString(val: string) { return val != null ? val.toString().replace(/~2F/g, "/").replace(/~~/g, "~") : val; }
function valToString(val: any) { return val != null ? val.toString().replace(/(~|\/)/g, m => ({'~':'~~', '/':'~2F'}[m])) : val; }
function valFromString(val: string) { return val != null ? val.toString().replace(/(~~|~2F)/g, m => ({'~~':'~', '~2F':'/'}[m])) : val; }

export class ParamTypes {
types: any;
Expand Down
2 changes: 2 additions & 0 deletions test/ng1/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ describe("UrlMatcher", function () {

expect(matcher1.format({ foo: "abc" })).toBe('/abc');
expect(matcher1.format({ foo: "~abc" })).toBe('/~~abc');
expect(matcher1.format({ foo: "~2F" })).toBe('/~~2F');

expect(matcher1.exec('/abc').foo).toBe("abc");
expect(matcher1.exec('/~~abc').foo).toBe("~abc");
expect(matcher1.exec('/~~2F').foo).toBe("~2F");
});

describe("snake-case parameters", function() {
Expand Down

0 comments on commit a201906

Please sign in to comment.