mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-30 03:00:06 -04:00
Merge pull request #3580 from braddunbar/match-root
If the root doesn't match, no routes can match.
This commit is contained in:
13
backbone.js
13
backbone.js
@@ -1527,6 +1527,13 @@
|
||||
return path === this.root && !this.getSearch();
|
||||
},
|
||||
|
||||
// Does the pathname match the root?
|
||||
matchRoot: function() {
|
||||
var path = this.decodeFragment(this.location.pathname);
|
||||
var root = path.slice(0, this.root.length - 1) + '/';
|
||||
return root === this.root;
|
||||
},
|
||||
|
||||
// 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.
|
||||
@@ -1552,9 +1559,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;
|
||||
},
|
||||
|
||||
@@ -1696,6 +1701,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)) {
|
||||
|
||||
@@ -932,4 +932,67 @@
|
||||
Backbone.history.start({root: '/root', pushState: true});
|
||||
});
|
||||
|
||||
test("Paths that don't match the root should not match no root", 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: 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 roots of the same length", 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: function(){
|
||||
ok(false, 'should not match unless root matches');
|
||||
}
|
||||
}
|
||||
});
|
||||
var router = new Router;
|
||||
Backbone.history.start({root: 'root', pushState: true});
|
||||
});
|
||||
|
||||
test("roots with regex characters", 1, function() {
|
||||
location.replace('http://example.com/x+y.z/foo');
|
||||
Backbone.history.stop();
|
||||
Backbone.history = _.extend(new Backbone.History, {location: location});
|
||||
var Router = Backbone.Router.extend({
|
||||
routes: {foo: function(){ ok(true); }}
|
||||
});
|
||||
var router = new Router;
|
||||
Backbone.history.start({root: 'x+y.z', pushState: true});
|
||||
});
|
||||
|
||||
test("roots with unicode characters", 1, function() {
|
||||
location.replace('http://example.com/®ooτ/foo');
|
||||
Backbone.history.stop();
|
||||
Backbone.history = _.extend(new Backbone.History, {location: location});
|
||||
var Router = Backbone.Router.extend({
|
||||
routes: {foo: function(){ ok(true); }}
|
||||
});
|
||||
var router = new Router;
|
||||
Backbone.history.start({root: '®ooτ', pushState: true});
|
||||
});
|
||||
|
||||
test("roots without slash", 1, function() {
|
||||
location.replace('http://example.com/®ooτ');
|
||||
Backbone.history.stop();
|
||||
Backbone.history = _.extend(new Backbone.History, {location: location});
|
||||
var Router = Backbone.Router.extend({
|
||||
routes: {'': function(){ ok(true); }}
|
||||
});
|
||||
var router = new Router;
|
||||
Backbone.history.start({root: '®ooτ', pushState: true});
|
||||
});
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user