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

Add support for question marks in hash fragments #36

Merged
merged 3 commits into from
Jul 12, 2016
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
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