From ae1ad8528ecdef9647eaabda464f789f251059a3 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Wed, 29 Jun 2016 18:47:47 +0100 Subject: [PATCH] fix: allow encoded URI components in URL parameters Fix router5/router5#63 --- modules/Path.js | 4 ++-- package.json | 2 +- test/main.js | 8 +++++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/Path.js b/modules/Path.js index 8bb0165..c71434f 100644 --- a/modules/Path.js +++ b/modules/Path.js @@ -1,5 +1,5 @@ const defaultOrConstrained = (match) => - '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~]+') + ')'; + '(' + (match ? match.replace(/(^<|>$)/g, '') : '[a-zA-Z0-9-_.~%]+') + ')'; const rules = [ { @@ -166,7 +166,7 @@ export default class Path { // Reduce named params to key-value pairs return match.slice(1, this.urlParams.length + 1) .reduce((params, m, i) => { - params[this.urlParams[i]] = m; + params[this.urlParams[i]] = decodeURIComponent(m); return params; }, {}); } diff --git a/package.json b/package.json index 9684b9a..7a348f9 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/commonjs/path-parser.js", "jsnext:main": "modules/Path.js", "scripts": { - "test": "_mocha", + "test": "mocha --compilers js:babel-core/register", "test-cover": "istanbul cover _mocha", "lint": "eslint modules/*.js", "build:amd": "BABEL_ENV=rollup rollup -c rollup.config.js --format amd", diff --git a/test/main.js b/test/main.js index bb53af0..1e2d45e 100644 --- a/test/main.js +++ b/test/main.js @@ -2,7 +2,7 @@ var path = require('path'); var pkg = require('../package.json'); -var Path = require(path.join(__dirname, '..', pkg.main)); +var Path = require('../modules/Path'); var should = require('should'); require('mocha'); @@ -168,4 +168,10 @@ describe('Path', function () { path.match('/', true).should.eql({}); path.match('', 1).should.eql({}); }); + + it('should match paths with encoded values', function () { + var path = new Path('/test/:id'); + + path.partialMatch('/test/%7B123-456%7D').should.eql({ id: '{123-456}' }); + }); });