Skip to content

Commit

Permalink
Add support for question marks in hash fragments (#36)
Browse files Browse the repository at this point in the history
Add support for question-marks in hash fragments.  From [rfc3986](https://tools.ietf.org/html/rfc3986#section-3.5):
> The characters slash ("/") and question mark ("?") are allowed to
> represent data within the fragment identifier.
  • Loading branch information
gigabo authored and lingyan committed Jul 12, 2016
1 parent 18ff14b commit 24f0445
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
26 changes: 12 additions & 14 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,20 @@ Route.prototype.match = function (url, options) {

// 2. check path
// remove query string and hash fragment from url
var pos = url.indexOf('?');
var path;
if (pos >= 0) {
// remove query string
path = url.substring(0, pos);
} else {
pos = url.indexOf('#');
//
// hash fragment does not get sent to server.
// But since routr can be used on both server and client,
// we should remove hash fragment before matching the regex.
var path = url;
var pos;

// Leave `pos` at the beginning of the query-string, if any.
['#', '?'].forEach(function(delimiter){
pos = path.indexOf(delimiter);
if (pos >= 0) {
// hash fragment does not get sent to server.
// But since routr can be used on both server and client,
// we should remove hash fragment before matching the regex.
path = url.substring(0, pos);
} else {
path = url;
path = path.substring(0, pos);
}
}
});

var pathMatches = self.regexp.exec(path);
if (!pathMatches) {
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,13 @@ describe('Router', function () {
var route = router.getRoute(encodingConsistencyPath);
expect(route.params.json).to.equal('{"keyword":"foo"}');
});
it('should handle a hash fragment with a question-mark', function () {
var route = router.getRoute('/finance/news/test.html#?', {method: 'get'});
expect(route.name).to.equal('article');
expect(route.params.site).to.equal('finance');
expect(route.params.category).to.equal('news');
expect(route.params.alias).to.equal('test.html');
});

it('should allow route to match multiple methods', function () {
var route = 'multi_methods';
Expand Down

0 comments on commit 24f0445

Please sign in to comment.