Skip to content

Commit

Permalink
Clean up normaliztion
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed May 17, 2016
1 parent 8f7845b commit 5bf1311
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
2 changes: 0 additions & 2 deletions lib/route-recognizer.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import map from './route-recognizer/dsl';
import Normalizer from './route-recognizer/normalizer';

var normalizeRoute = Normalizer.normalizeRoute;
var normalizePath = Normalizer.normalizePath;
var normalizeRouteSegment = Normalizer.normalizeRouteSegment;
var normalizePathSegment = Normalizer.normalizePathSegment;

var specials = [
'/', '.', '*', '+', '?', '|',
Expand Down
48 changes: 20 additions & 28 deletions lib/route-recognizer/normalizer.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
// 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";
// Match percent-encoded values (e.g. %3a, %3A, %25)
var PERCENT_ENCODED_VALUES = /%[a-fA-F0-9]{2}/g;

function toUpper(str) { return str.toUpperCase(); }

// Turns all percent-encoded values to upper case
// "%3a" -> "%3A"
// Turn 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);
return string.replace(PERCENT_ENCODED_VALUES, toUpper);
}

// Normalizes percent-encoded values to upper-case and decodes percent-encoded
Expand All @@ -27,13 +17,6 @@ function normalizePath(path) {
.join('/');
}

// 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));
}

function percentEncode(char) {
return '%' + charToHex(char);
}
Expand All @@ -42,41 +25,50 @@ function charToHex(char) {
return char.charCodeAt(0).toString(16).toUpperCase();
}

// Decodes percent-encoded values in the string except those
// characters in `reservedSet`
function decodeURIComponentExcept(string, reservedSet) {
string = percentEncodedValuesToUpper(string);
var replacements = {};

for (var i=0; i < reservedSet.length; i++) {
var char = reservedSet[i];
var pChar = percentEncode(char);
if (string.indexOf(pChar) !== -1) {
var replacement = "__" + charToHex(char) + "__";
replacements[pChar] = replacement;
string = string.replace(new RegExp(pChar, 'g'), replacement);

var pCharRegex = new RegExp(pChar, 'g');
string = string.replace(pCharRegex, replacement);
}
}
string = decodeURIComponent(string);

Object.keys(replacements).forEach(function(pChar) {
var replacement = replacements[pChar];
string = string.replace(new RegExp(replacement, 'g'), pChar);
var replacementRegex = new RegExp(replacement, 'g');

string = string.replace(replacementRegex, pChar);
});

return string;
}

// Leave these characters in encoded state in segments
var reservedRouteSegmentChars = ['%', '/'];
var reservedPathSegmentChars = ['%', '/'];

function normalizeRouteSegment(segment) {
return decodeURIComponentExcept(segment, ['%', '/']);
return decodeURIComponentExcept(segment, reservedRouteSegmentChars);
}

function normalizePathSegment(segment) {
return decodeURIComponentExcept(segment, ['%', '/']);
return decodeURIComponentExcept(segment, reservedPathSegmentChars);
}

var Normalizer = {
normalizeRoute: normalizeRoute,
normalizeRouteSegment: normalizeRouteSegment,
normalizePath: normalizePath,
normalizePathSegment: normalizePathSegment
normalizePath: normalizePath
};

export default Normalizer;

0 comments on commit 5bf1311

Please sign in to comment.