Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix navigate with replace: true and diacriticals/unicode characters in the hash #4175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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