Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Query param plus symbols (+) are now transformed to spaces #40

Merged
merged 1 commit into from
Jan 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/route-recognizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ function addSegment(currentState, segment) {
return currentState;
}

function decodeQueryParamPart(part) {
// http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
part = part.replace(/\+/gm, '%20');
return decodeURIComponent(part);
}

// The main interface

var RouteRecognizer = function() {
Expand Down Expand Up @@ -446,7 +452,7 @@ RouteRecognizer.prototype = {
var pairs = queryString.split("&"), queryParams = {};
for(var i=0; i < pairs.length; i++) {
var pair = pairs[i].split('='),
key = decodeURIComponent(pair[0]),
key = decodeQueryParamPart(pair[0]),
keyLength = key.length,
isArray = false,
value;
Expand All @@ -461,7 +467,7 @@ RouteRecognizer.prototype = {
queryParams[key] = [];
}
}
value = pair[1] ? decodeURIComponent(pair[1]) : '';
value = pair[1] ? decodeQueryParamPart(pair[1]) : '';
}
if (isArray) {
queryParams[key].push(value);
Expand Down
8 changes: 8 additions & 0 deletions tests/recognizer-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ test("A simple route with query params with encoding recognizes", function() {
deepEqual(router.recognize("/foo/bar?other=something%20100%25").queryParams, { other: 'something 100%' });
});

test("A route with query params with pluses for spaces instead of %20 recognizes", function() {
var handler = {};
var router = new RouteRecognizer();
router.add([{ path: "/foo/bar", handler: handler}]);

deepEqual(router.recognize("/foo/bar?++one+two=three+four+five++").queryParams, { ' one two': 'three four five ' });
});


test("A `/` route recognizes", function() {
var handler = {};
Expand Down