Skip to content

Commit

Permalink
Merge pull request visionmedia#22 from ovaillancourt/master
Browse files Browse the repository at this point in the history
Fix for page.js not matching routes containing query strings.
  • Loading branch information
tj committed Jul 5, 2012
2 parents 9a4cc7a + 14315c2 commit 9b60993
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
10 changes: 6 additions & 4 deletions build/page.js
Expand Up @@ -88,7 +88,7 @@
if (false !== options.popstate) addEventListener('popstate', onpopstate, false);
if (false !== options.click) addEventListener('click', onclick, false);
if (!dispatch) return;
page.replace(location.pathname, null, true, dispatch);
page.replace(location.pathname + location.search, null, true, dispatch);
};

/**
Expand Down Expand Up @@ -264,8 +264,10 @@

Route.prototype.match = function(path, params){
var keys = this.keys
, m = this.regexp.exec(path);

, qsIndex = path.indexOf('?')
, pathname = ~qsIndex ? path.slice(0, qsIndex) : path
, m = this.regexp.exec(pathname);

if (!m) return false;

for (var i = 1, len = m.length; i < len; ++i) {
Expand Down Expand Up @@ -348,7 +350,7 @@
while (el && 'A' != el.nodeName) el = el.parentNode;
if (!el || 'A' != el.nodeName) return;
var href = el.href;
var path = el.pathname;
var path = el.pathname + el.search;
if (el.hash) return;
if (!sameOrigin(href)) return;
var orig = path;
Expand Down
2 changes: 1 addition & 1 deletion build/page.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions lib/page.js
Expand Up @@ -88,7 +88,7 @@
if (false !== options.popstate) addEventListener('popstate', onpopstate, false);
if (false !== options.click) addEventListener('click', onclick, false);
if (!dispatch) return;
page.replace(location.pathname, null, true, dispatch);
page.replace(location.pathname + location.search, null, true, dispatch);
};

/**
Expand Down Expand Up @@ -264,8 +264,10 @@

Route.prototype.match = function(path, params){
var keys = this.keys
, m = this.regexp.exec(path);

, qsIndex = path.indexOf('?')
, pathname = ~qsIndex ? path.slice(0, qsIndex) : path
, m = this.regexp.exec(pathname);

if (!m) return false;

for (var i = 1, len = m.length; i < len; ++i) {
Expand Down Expand Up @@ -348,7 +350,7 @@
while (el && 'A' != el.nodeName) el = el.parentNode;
if (!el || 'A' != el.nodeName) return;
var href = el.href;
var path = el.pathname;
var path = el.pathname + el.search;
if (el.hash) return;
if (!sameOrigin(href)) return;
var orig = path;
Expand Down
58 changes: 58 additions & 0 deletions test/tests.js
Expand Up @@ -15,6 +15,42 @@ describe('page', function(){
})
})

describe('when matching routes', function(){
describe('when the path contains a query string', function(){
it('should not consider the query string when matching', function(done){
page('/qs', function(ctx){
done();
});

page('/qs?test=true');
})

it('should not consider the query string when matching a route ending with a named param', function(done){
page('/qs/:testParam', function(ctx){
done();
});

page('/qs/test?test=true');
})

it('should not consider the query string when matching a route ending with a named wildcard param', function(done){
page('/qs/named/wildcard/:testParam(*)', function(ctx){
done();
});

page('/qs/named/wildcard/something/else?test=true');
})

it('should not consider the query string when matching a route ending with a wildcard', function(done){
page('/qs/wildcard/end/*', function(ctx){
done();
});

page('/qs/wildcard/end/with/something/here?test=true');
});
})
})

describe('when the route matches', function(){
it('should invoke the callback', function(done){
page('/user/:name', function(ctx){
Expand All @@ -32,6 +68,28 @@ describe('page', function(){

page('/blog/post/something');
})

describe('when the match has a query string', function(){

it('should not include the query string inside ctx.params for named param values', function(done){
page('/query/string/:name', function(ctx){
expect(ctx.params.name).to.equal('something');
done();
})

page('/query/string/something?test=true');
})

it('should not include the query string inside ctx.params for named wildcard params', function(done){
page('/query/string/wildcard/:name(*)', function(ctx){
expect(ctx.params.name).to.equal('something');
done();
})

page('/query/string/wildcard/something?test=true');
})
})

})

describe('when next() is called', function(){
Expand Down

0 comments on commit 9b60993

Please sign in to comment.