Skip to content

Commit 9d6165d

Browse files
author
Kyle Dickerson
committed
#3391 - Add History trailingSlash option always use trailing slash on root.
1 parent 4abcaa5 commit 9d6165d

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

backbone.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,6 +1851,7 @@
18511851
// Is pushState desired ... is it available?
18521852
this.options = _.extend({root: '/'}, this.options, options);
18531853
this.root = this.options.root;
1854+
this._trailingSlash = this.options.trailingSlash || false;
18541855
this._wantsHashChange = this.options.hashChange !== false;
18551856
this._hasHashChange = 'onhashchange' in window && (document.documentMode === void 0 || document.documentMode > 7);
18561857
this._useHashChange = this._wantsHashChange && this._hasHashChange;
@@ -1993,10 +1994,10 @@
19931994
// Normalize the fragment.
19941995
fragment = this.getFragment(fragment || '');
19951996

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

test/router.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,38 @@
807807
Backbone.history.navigate('?x=1');
808808
});
809809

810+
QUnit.test('#3391 - Empty root normalizes to single slash.', function(assert) {
811+
assert.expect(1);
812+
Backbone.history.stop();
813+
Backbone.history = _.extend(new Backbone.History, {
814+
location: location,
815+
history: {
816+
pushState: function(state, title, url) {
817+
assert.strictEqual(url, '/');
818+
}
819+
}
820+
});
821+
location.replace('http://example.com/root/path');
822+
Backbone.history.start({pushState: true, hashChange: false, root: ''});
823+
Backbone.history.navigate('');
824+
});
825+
826+
QUnit.test('#3391 - Use trailing slash on root when trailingSlash is true.', function(assert) {
827+
assert.expect(1);
828+
Backbone.history.stop();
829+
Backbone.history = _.extend(new Backbone.History, {
830+
location: location,
831+
history: {
832+
pushState: function(state, title, url) {
833+
assert.strictEqual(url, '/root/');
834+
}
835+
}
836+
});
837+
location.replace('http://example.com/root/path');
838+
Backbone.history.start({pushState: true, hashChange: false, root: 'root', trailingSlash: true});
839+
Backbone.history.navigate('');
840+
});
841+
810842
QUnit.test('#2765 - Fragment matching sans query/hash.', function(assert) {
811843
assert.expect(2);
812844
Backbone.history.stop();

0 commit comments

Comments
 (0)