From 5cc5e5a2490f0e6365e8d02924d3a50cc29b6219 Mon Sep 17 00:00:00 2001 From: Denis Sokolov Date: Fri, 5 Jun 2015 13:20:22 +0300 Subject: [PATCH] Encode parameter values to allow slashes in values, fix #15 --- index.js | 10 ++++++++-- test/named-parameter-test.js | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index c942590..e4c9aec 100644 --- a/index.js +++ b/index.js @@ -68,10 +68,14 @@ function extractParameters(route, fragment) { var param = params[i]; if (param) { + if (key[0] === '*') { - param = param.split('/'); + param = param.split('/').map(decodeURIComponent); + } else { + param = decodeURIComponent(param); } + memo[key.substr(1)] = param; } @@ -88,7 +92,9 @@ function insertParameters(route, obj) { var value = obj[k] || ''; if (Array.isArray(value)) { - value = value.join('/'); + value = value.map(encodeURIComponent).join('/'); + } else { + value = encodeURIComponent(value); } str = str.replace(route.keys[key], value + '$1'); diff --git a/test/named-parameter-test.js b/test/named-parameter-test.js index 9301d6e..c04b865 100644 --- a/test/named-parameter-test.js +++ b/test/named-parameter-test.js @@ -27,3 +27,19 @@ test('named parameter followed by optional parameter', function(t) { t.end(); }); + +test('named parameter with colon in the content', function(t) { + var page = docuri.route('page/:id'); + + t.deepEqual(page(page({ id: 'value:colon' })), { id: 'value:colon' }, 'parsed page has colon set back correctly'); + + t.end(); +}); + +test('named parameter with slash in the content', function(t) { + var page = docuri.route('page/:id'); + + t.deepEqual(page(page({ id: 'value/slash' })), { id: 'value/slash' }, 'parsed page has slash set back correctly'); + + t.end(); +});