Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1795,8 +1795,12 @@
}
};

// Cached regex for stripping a leading hash/slash and trailing space.
var routeStripper = /^[#\/]|\s+$/g;
// Cached regex for stripping a leading hash/slash from a fragment.
var routeStripper = /^[#\/]/;

// Cached regex for stripping trailing whitespace from a raw hash value.
// Only applied to the hash before decoding (see #1794, #4198).
var trailingSpaceStripper = /\s+$/;

// Cached regex for stripping leading and trailing slashes.
var rootStripper = /^\/+|\/+$/g;
Expand Down Expand Up @@ -1843,9 +1847,11 @@

// Gets the true hash value. Cannot use location.hash directly due to bug
// in Firefox where location.hash will always be decoded.
// Trailing whitespace from the raw hash is stripped (see #1794), but
// percent-encoded whitespace inside the fragment is preserved (see #4198).
getHash: function(window) {
var match = (window || this).location.href.match(/#(.*)$/);
return match ? match[1] : '';
return match ? match[1].replace(trailingSpaceStripper, '') : '';
},

// Get the pathname and search params, without the root.
Expand Down
27 changes: 23 additions & 4 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,35 @@
new MyRouter;
});

QUnit.test('#1794 - Trailing space in fragments.', function(assert) {
QUnit.test('#1794 - Trailing space stripped from raw hash.', function(assert) {
assert.expect(2);
var history = new Backbone.History;
history.location = {href: 'http://example.com/#fragment '};
assert.strictEqual(history.getHash(), 'fragment');
// An explicit fragment passed to getFragment must not lose its
// trailing whitespace (regression test for #4198).
assert.strictEqual(history.getFragment('fragment '), 'fragment ');
});

QUnit.test('#1820 - Leading slash stripped, trailing space preserved.', function(assert) {
assert.expect(1);
var history = new Backbone.History;
assert.strictEqual(history.getFragment('fragment '), 'fragment');
assert.strictEqual(history.getFragment('/fragment '), 'fragment ');
});

QUnit.test('#1820 - Leading slash and trailing space.', function(assert) {
QUnit.test('#4198 - Percent-encoded trailing space preserved in path.', function(assert) {
assert.expect(1);
var history = new Backbone.History;
assert.strictEqual(history.getFragment('/fragment '), 'fragment');
history.root = '/';
history._wantsHashChange = false;
history.location = {
pathname: '/outbound/22130600/po/powithspacetest%20',
href: 'http://example.com/outbound/22130600/po/powithspacetest%20'
};
assert.strictEqual(
history.getFragment(),
'outbound/22130600/po/powithspacetest '
);
});

QUnit.test('#1980 - Optional parameters.', function(assert) {
Expand Down
Loading