Skip to content

Commit

Permalink
Update trailing slash for search params.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Oct 23, 2014
1 parent 687ebf6 commit 23599e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1593,17 +1593,22 @@
if (!History.started) return false;
if (!options || options === true) options = {trigger: !!options};

var url = this.root + (fragment = this.getFragment(fragment || ''));
// Normalize the fragment.
fragment = this.getFragment(fragment || '');

// Don't include a trailing slash on the root.
var root = this.root;
if (fragment === '' || fragment.charAt(0) === '?') {
root = root.slice(0, -1) || '/';
}
var url = root + fragment;

// Strip the hash and decode for matching.
fragment = decodeURI(fragment.replace(pathStripper, ''));

if (this.fragment === fragment) return;
this.fragment = fragment;

// Don't include a trailing slash on the root.
if (fragment === '' && url !== '/') url = url.slice(0, -1);

// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, url);
Expand Down
15 changes: 15 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,21 @@
Backbone.history.navigate('');
});

test('#2656 - No trailing slash on root.', 1, function() {
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url){
strictEqual(url, '/root?x=1');
}
}
});
location.replace('http://example.com/root/path');
Backbone.history.start({pushState: true, hashChange: false, root: 'root'});
Backbone.history.navigate('?x=1');
});

test('#2765 - Fragment matching sans query/hash.', 2, function() {
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
Expand Down

0 comments on commit 23599e7

Please sign in to comment.