Skip to content

Commit

Permalink
#3391 - Add History trailingSlash option always use trailing slash …
Browse files Browse the repository at this point in the history
…on root.
  • Loading branch information
Kyle Dickerson committed Jul 21, 2023
1 parent 4abcaa5 commit 9d6165d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
7 changes: 4 additions & 3 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,7 @@
// Is pushState desired ... is it available?
this.options = _.extend({root: '/'}, this.options, options);
this.root = this.options.root;
this._trailingSlash = this.options.trailingSlash || false;
this._wantsHashChange = this.options.hashChange !== false;
this._hasHashChange = 'onhashchange' in window && (document.documentMode === void 0 || document.documentMode > 7);
this._useHashChange = this._wantsHashChange && this._hasHashChange;
Expand Down Expand Up @@ -1993,10 +1994,10 @@
// Normalize the fragment.
fragment = this.getFragment(fragment || '');

// Don't include a trailing slash on the root.
// Strip trailing slash on the root unless _trailingSlash is true
var rootPath = this.root;
if (fragment === '' || fragment.charAt(0) === '?') {
rootPath = rootPath.slice(0, -1) || '/';
if (!this._trailingSlash && (fragment === '' || fragment.charAt(0) === '?')) {
rootPath = rootPath.slice(0, -1) || '/'; // rootPath must always have at least '/'
}
var url = rootPath + fragment;

Expand Down
32 changes: 32 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,38 @@
Backbone.history.navigate('?x=1');
});

QUnit.test('#3391 - Empty root normalizes to single slash.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
assert.strictEqual(url, '/');
}
}
});
location.replace('http://example.com/root/path');
Backbone.history.start({pushState: true, hashChange: false, root: ''});
Backbone.history.navigate('');
});

QUnit.test('#3391 - Use trailing slash on root when trailingSlash is true.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {
location: location,
history: {
pushState: function(state, title, url) {
assert.strictEqual(url, '/root/');
}
}
});
location.replace('http://example.com/root/path');
Backbone.history.start({pushState: true, hashChange: false, root: 'root', trailingSlash: true});
Backbone.history.navigate('');
});

QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) {
assert.expect(2);
Backbone.history.stop();
Expand Down

0 comments on commit 9d6165d

Please sign in to comment.