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

When root option omits traling slash, Backbone.History.navigate and Backbone.History.start set URL with a slash between the root and route fragment when using pushState #1505

Merged
merged 3 commits into from Jul 27, 2012
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion backbone.js
Expand Up @@ -1023,6 +1023,9 @@
var docMode = document.documentMode;
var oldIE = (isExplorer.exec(navigator.userAgent.toLowerCase()) && (!docMode || docMode <= 7));

// Normalize root to always include trailing slash
if (!trailingSlash.test(this.options.root)) this.options.root = this.options.root + '/';

if (oldIE && this._wantsHashChange) {
this.iframe = Backbone.$('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo('body')[0].contentWindow;
this.navigate(fragment);
Expand All @@ -1042,7 +1045,8 @@
// opened by a non-pushState browser.
this.fragment = fragment;
var loc = this.location;
var atRoot = (loc.pathname === this.options.root) && !loc.search;
var atRoot = (loc.pathname.replace(trailingSlash, '') === this.options.root.replace(trailingSlash, '')) &&
!loc.search;

// If we've started off with a route from a `pushState`-enabled browser,
// but we're currently in a browser that doesn't support it...
Expand Down
51 changes: 51 additions & 0 deletions test/router.js
Expand Up @@ -318,4 +318,55 @@ $(document).ready(function() {
strictEqual(Backbone.history.fragment, 'x');
});

test("Router: insert slash before fragment when root fragment has no trailing slash", 3, function() {
Backbone.history.stop();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's probably best to create a new history object here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered that. Better safe than sorry I guess ;)

location.replace('http://example.com/root');
Backbone.history = new Backbone.History({
location: location,
history: {
pushState: function(state, title, url) {
strictEqual(url, '/root/fragment');
}
}
});
Backbone.history.start({
pushState: true,
root: '/root',
hashChange: false
});
Backbone.history.navigate('fragment');

Backbone.history.stop();
location.replace('http://example.com/root#fragment');
location.protocol = 'http:';
location.host = 'example.com';
Backbone.history = new Backbone.History({
location: location,
history: {
pushState: function(state, title, url) {},
replaceState: function(state, title, url) {
strictEqual(url, 'http://example.com/root/fragment');
}
}
});
Backbone.history.start({
pushState: true,
root: '/root'
});

Backbone.history.stop();
location.replace('http://example.com/root');
var backboneHistory = new Backbone.History({
location: location
});
backboneHistory.loadUrl = function() {
ok(true);
};
Backbone.history = backboneHistory;
Backbone.history.start({
pushState: true,
root: '/root'
});
});

});