Skip to content

Commit 611513c

Browse files
committed
Replace whitelist with decodeURI
1 parent f9bf1e0 commit 611513c

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

Readme.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,12 @@ npm install path-to-regexp --save
1818
## Usage
1919

2020
```javascript
21-
const {
22-
pathToRegexp,
23-
match,
24-
parse,
25-
compile,
26-
normalizePathname
27-
} = require("path-to-regexp");
21+
const { pathToRegexp, match, parse, compile } = require("path-to-regexp");
2822

2923
// pathToRegexp(path, keys?, options?)
3024
// match(path)
3125
// parse(path)
3226
// compile(path)
33-
// normalizePathname(path)
3427
```
3528

3629
- **path** A string, array of strings, or a regular expression.
@@ -171,16 +164,20 @@ match("/invalid"); //=> false
171164

172165
### Normalize Pathname
173166

174-
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`.
167+
The `normalizePathname` function will return a normalized string for matching with `pathToRegexp`:
175168

176169
```js
177170
const re = pathToRegexp("/caf\u00E9");
178-
const input = encodeURI("/cafe\u0301");
171+
const input = encodeURI("/caf\u00E9");
179172

180173
re.test(input); //=> false
181174
re.test(normalizePathname(input)); //=> true
182175
```
183176

177+
**Note:** It may be preferable to implement something in your own library that normalizes the pathname for matching. E.g. [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) automatically URI encodes paths for you, which would result in a consistent match.
178+
179+
**Tip:** Consider using [`String.prototype.normalize`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize) to resolve unicode variants of the same string.
180+
184181
### Parse
185182

186183
The `parse` function will return a list of strings and keys from a path string:

src/index.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2832,12 +2832,10 @@ describe("path-to-regexp", function() {
28322832
).toEqual(["/caf\u00E9"]);
28332833
});
28342834

2835-
it("should not normalize whitelisted characters", function() {
2836-
const input = "/test/route%2F%25";
2835+
it("should not normalize encoded slash", function() {
2836+
const input = "/test/route%2F";
28372837

2838-
expect(pathToRegexp.normalizePathname(input)).toEqual(
2839-
"/test/route%2F%25"
2840-
);
2838+
expect(pathToRegexp.normalizePathname(input)).toEqual("/test/route%2F");
28412839
});
28422840

28432841
it("should fix repeated slashes", function() {

src/index.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,8 @@ export interface ParseOptions {
1919
* slash and normalizes unicode characters to "NFC". When using this method,
2020
* `decode` should be an identity function so you don't decode strings twice.
2121
*/
22-
export function normalizePathname(
23-
pathname: string,
24-
whitelist: string | string[] = "%/-."
25-
) {
26-
return pathname
27-
.replace(/\/+/g, "/")
28-
.replace(
29-
/(?:%[ef][0-9a-f](?:%[0-9a-f]{2}){2}|%[cd][0-9a-f]%[0-9a-f]{2}|%[0-9a-f]{2})/gi,
30-
m => {
31-
const char = decodeURIComponent(m);
32-
if (whitelist.indexOf(char) > -1) return m;
33-
return char;
34-
}
35-
);
22+
export function normalizePathname(pathname: string) {
23+
return decodeURI(pathname).replace(/\/+/g, "/");
3624
}
3725

3826
/**

0 commit comments

Comments
 (0)