Skip to content

Commit

Permalink
Merge pull request jashkenas#3434 from braddunbar/decode-fragment
Browse files Browse the repository at this point in the history
Decode unicode escapes without decoding `%25`.
  • Loading branch information
jashkenas committed Feb 23, 2015
2 parents cb7ecba + faea842 commit e109f6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,13 @@
return path === this.root && !this.getSearch();
},

// Unicode characters in `location.pathname` are percent encoded so they're
// decoded for comparison. `%25` should not be decoded since it may be part
// of an encoded parameter.
decodeFragment: function(fragment) {
return decodeURI(fragment.replace(/%25/g, '%2525'));
},

// In IE6, the hash fragment and search params are incorrect if the
// fragment contains `?`.
getSearch: function() {
Expand All @@ -1539,7 +1546,9 @@

// Get the pathname and search params, without the root.
getPath: function() {
var path = decodeURI(this.location.pathname + this.getSearch());
var path = this.decodeFragment(
this.location.pathname + this.getSearch()
);
var root = this.root.slice(0, -1);
if (!path.indexOf(root)) path = path.slice(root.length);
return path.charAt(0) === '/' ? path.slice(1) : path;
Expand Down Expand Up @@ -1714,7 +1723,7 @@
var url = root + fragment;

// Strip the hash and decode for matching.
fragment = decodeURI(fragment.replace(pathStripper, ''));
fragment = this.decodeFragment(fragment.replace(pathStripper, ''));

if (this.fragment === fragment) return;
this.fragment = fragment;
Expand Down
15 changes: 15 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,21 @@
Backbone.history.start({pushState: true});
});

test('unicode pathname with % in a parameter', 1, function() {
location.replace('http://example.com/myyjä/foo%20%25%3F%2f%40%25%20bar');
Backbone.history.stop();
Backbone.history = _.extend(new Backbone.History, {location: location});
var Router = Backbone.Router.extend({
routes: {
'myyjä/:query': function(query) {
strictEqual(query, 'foo %?/@% bar');
}
}
});
new Router;
Backbone.history.start({pushState: true});
});

test('newline in route', 1, function() {
location.replace('http://example.com/stuff%0Anonsense?param=foo%0Abar');
Backbone.history.stop();
Expand Down

0 comments on commit e109f6d

Please sign in to comment.