Skip to content

Commit

Permalink
Merge pull request #40 from jayphelps/query-plus
Browse files Browse the repository at this point in the history
[BUGFIX] Query param plus symbols (+) are now transformed to spaces
  • Loading branch information
machty committed Jan 22, 2015
2 parents a9cd5c8 + a549a68 commit 404b848
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/route-recognizer.js
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
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

0 comments on commit 404b848

Please sign in to comment.