Skip to content

Commit

Permalink
Fix navigate with replace: true and diacriticals/unicode characters in
Browse files Browse the repository at this point in the history
the hash

It seems there is a change in chrome 63 (also similarly
introduced a while ago in some earlier version of iOS Safari) where the
hash portion of the location.href is encoded, where it used to be
decoded

Fixes jashkenas#4085
  • Loading branch information
gwynjudd committed Dec 14, 2017
1 parent 8ec8860 commit 9fd4c79
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@
// Checks the current URL to see if it has changed, and if it has,
// calls `loadUrl`, normalizing across the hidden iframe.
checkUrl: function(e) {
var current = this.getFragment();
var current = this.decodeFragment(this.getFragment());

// If the user pressed the back button, the iframe's hash will have
// changed and we should use that for comparison.
Expand All @@ -1782,7 +1782,8 @@
loadUrl: function(fragment) {
// If the root doesn't match, no routes can match either.
if (!this.matchRoot()) return false;
fragment = this.fragment = this.getFragment(fragment);
fragment = this.getFragment(fragment);
this.fragment = this.decodeFragment(fragment);
return _.some(this.handlers, function(handler) {
if (handler.route.test(fragment)) {
handler.callback(fragment);
Expand Down
68 changes: 68 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
var Router = Backbone.Router.extend({

count: 0,
UTFCount: 0,
anythingCount: 0,

routes: {
'noCallback': 'noCallback',
Expand Down Expand Up @@ -114,6 +116,7 @@
},

charUTF: function() {
this.UTFCount++;
this.charType = 'UTF';
},

Expand Down Expand Up @@ -159,6 +162,7 @@
},

anything: function(whatever) {
this.anythingCount++;
this.anything = whatever;
},

Expand Down Expand Up @@ -426,6 +430,70 @@
assert.equal(router.charType, 'UTF');
});

QUnit.test('#4085 - Hashes with UTF8 in them.', function(assert) {
var done = assert.async();

assert.expect(4);
router.anythingCount = router.UTFCount = 0;
Backbone.history.navigate('charñ', {trigger: true});
assert.equal(router.UTFCount, 1);
assert.equal(router.anythingCount, 0);

setTimeout(function() {
assert.equal(router.UTFCount, 1);
assert.equal(router.anythingCount, 0);
done();
}, 1000);
});

QUnit.test('#4085 - Hashes with UTF8 in them, no trigger.', function(assert) {
var done = assert.async();

assert.expect(4);
router.anythingCount = router.UTFCount = 0;
Backbone.history.navigate('charñ', {trigger: false});
assert.equal(router.UTFCount, 0);
assert.equal(router.anythingCount, 0);

setTimeout(function() {
assert.equal(router.UTFCount, 0);
assert.equal(router.anythingCount, 0);
done();
}, 1000);
});

QUnit.test('#4085 - Hashes with UTF8 in them, replace.', function(assert) {
var done = assert.async();

assert.expect(4);
router.anythingCount = router.UTFCount = 0;
Backbone.history.navigate('charñ', {trigger: true, replace: true});
assert.equal(router.UTFCount, 1);
assert.equal(router.anythingCount, 0);

setTimeout(function() {
assert.equal(router.UTFCount, 1);
assert.equal(router.anythingCount, 0);
done();
}, 1000);
});

QUnit.test('#4085 - Hashes with UTF8 in them, replace, no trigger.', function(assert) {
var done = assert.async();

assert.expect(4);
router.anythingCount = router.UTFCount = 0;
Backbone.history.navigate('charñ', {trigger: false, replace: true});
assert.equal(router.UTFCount, 0);
assert.equal(router.anythingCount, 0);

setTimeout(function() {
assert.equal(router.UTFCount, 0);
assert.equal(router.anythingCount, 0);
done();
}, 1000);
});

QUnit.test('#1185 - Use pathname when hashChange is not wanted.', function(assert) {
assert.expect(1);
Backbone.history.stop();
Expand Down

0 comments on commit 9fd4c79

Please sign in to comment.