Skip to content

Commit

Permalink
If the root doesn't match, no routes can match.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Apr 28, 2015
1 parent 8b46ffa commit c1e70a2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 11 additions & 3 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,11 @@
return path === this.root && !this.getSearch();
},

// Does the pathname match the root?
matchRoot: function() {
return this.rootMatcher.test(this.location.pathname);
},

// Unicode characters in `location.pathname` are percent encoded so they're
// decoded for comparison. `%25` should not be decoded since it may be part
// of an encoded parameter.
Expand All @@ -1552,9 +1557,7 @@
getPath: function() {
var path = this.decodeFragment(
this.location.pathname + this.getSearch()
);
var root = this.root.slice(0, -1);
if (!path.indexOf(root)) path = path.slice(root.length);
).slice(this.root.length - 1);
return path.charAt(0) === '/' ? path.slice(1) : path;
},

Expand Down Expand Up @@ -1591,6 +1594,9 @@
// Normalize root to always include a leading and trailing slash.
this.root = ('/' + this.root + '/').replace(rootStripper, '/');

// A regular expression for testing the pathname against the root.
this.rootMatcher = new RegExp('^' + this.root.slice(0, -1) + '(/|$)');

// Transition from hashChange to pushState or vice versa if both are
// requested.
if (this._wantsHashChange && this._wantsPushState) {
Expand Down Expand Up @@ -1696,6 +1702,8 @@
// match, returns `true`. If no defined routes matches the fragment,
// returns `false`.
loadUrl: function(fragment) {
// If the root doesn't match, no routes can match either.
if (!this.matchRoot()) return false;
fragment = this.fragment = this.getFragment(fragment);
return _.any(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
Expand Down
24 changes: 24 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,4 +932,28 @@
Backbone.history.start({root: '/root', pushState: true});
});

test("Paths that don't match the root should not match", 0, function() {
location.replace('http://example.com/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {'foo': 'foo'},
foo: function(){ ok(false, 'should not match unless root matches'); }
});
var router = new Router;
Backbone.history.start({root: 'root', pushState: true});
});

test("Paths that don't match the root should not match", 0, function() {
location.replace('http://example.com/xxxx/foo');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {'foo': 'foo'},
foo: function(){ ok(false, 'should not match unless root matches'); }
});
var router = new Router;
Backbone.history.start({root: 'root', pushState: true});
});

})();

0 comments on commit c1e70a2

Please sign in to comment.