-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
normalize route/path, encoded/decode dynamic path segments
This allows handlers like "/foo/:bar" to match a path with a url encoded segment such as "/foo/abc%Fdef" -> { params: { bar: "abc/def" } } See related issue emberjs/ember.js#11497
- Loading branch information
Showing
3 changed files
with
475 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Matches all percent-encoded values like %3a | ||
var percentEncodedValueRegex = /%[a-fA-F0-9]{2}/g; | ||
|
||
// The percent-encoding for the percent "%" character | ||
var percentEncodedPercent = "%25"; | ||
|
||
function toUpper(str) { return str.toUpperCase(); } | ||
|
||
// Turns all percent-encoded values to upper case | ||
// "%3a" -> "%3A" | ||
function percentEncodedValuesToUpper(string) { | ||
return string.replace(percentEncodedValueRegex, toUpper); | ||
} | ||
|
||
function decodeURIWithoutPercents(string) { | ||
return string.split(percentEncodedPercent) | ||
.map(decodeURI) | ||
.join(percentEncodedPercent); | ||
} | ||
|
||
// Normalizes percent-encoded values to upper-case and decodes percent-encoded | ||
// values that are not reserved (like unicode characters). | ||
// Safe to call multiple times on the same path. | ||
function normalizePath(path) { | ||
return decodeURIWithoutPercents(percentEncodedValuesToUpper(path)); | ||
} | ||
|
||
// Normalizes percent-encoded values to upper-case and decodes percent-encoded | ||
// values that are not reserved (like unicode characters). | ||
// Safe to call multiple times on the same route. | ||
function normalizeRoute(route) { | ||
return decodeURIWithoutPercents(percentEncodedValuesToUpper(route)); | ||
} | ||
|
||
var Normalizer = { | ||
normalizeRoute: normalizeRoute, | ||
normalizePath: normalizePath | ||
}; | ||
|
||
export default Normalizer; |
Oops, something went wrong.