Skip to content

Commit 20c2c9b

Browse files
committed
[fixed] Do not decode + in pathname
[removed] Path.encode/Path.decode Fixes #716
1 parent 8086698 commit 20c2c9b

File tree

6 files changed

+18
-45
lines changed

6 files changed

+18
-45
lines changed

modules/locations/HashLocation.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
var LocationActions = require('../actions/LocationActions');
22
var History = require('../History');
3-
var Path = require('../utils/Path');
43

54
/**
65
* Returns the current URL path from the `hash` portion of the URL, including
76
* query string.
87
*/
98
function getHashPath() {
10-
return Path.decode(
9+
return decodeURI(
1110
// We can't use window.location.hash here because it's not
1211
// consistent across browsers - Firefox will pre-decode it!
1312
window.location.href.split('#')[1] || ''
@@ -96,12 +95,14 @@ var HashLocation = {
9695

9796
push: function (path) {
9897
_actionType = LocationActions.PUSH;
99-
window.location.hash = Path.encode(path);
98+
window.location.hash = encodeURI(path);
10099
},
101100

102101
replace: function (path) {
103102
_actionType = LocationActions.REPLACE;
104-
window.location.replace(window.location.pathname + window.location.search + '#' + Path.encode(path));
103+
window.location.replace(
104+
window.location.pathname + window.location.search + '#' + encodeURI(path)
105+
);
105106
},
106107

107108
pop: function () {

modules/locations/HistoryLocation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
var LocationActions = require('../actions/LocationActions');
22
var History = require('../History');
3-
var Path = require('../utils/Path');
43

54
/**
65
* Returns the current URL path from `window.location`, including query string.
76
*/
87
function getWindowPath() {
9-
return Path.decode(
8+
return decodeURI(
109
window.location.pathname + window.location.search
1110
);
1211
}
@@ -66,13 +65,13 @@ var HistoryLocation = {
6665
},
6766

6867
push: function (path) {
69-
window.history.pushState({ path: path }, '', Path.encode(path));
68+
window.history.pushState({ path: path }, '', encodeURI(path));
7069
History.length += 1;
7170
notifyChange(LocationActions.PUSH);
7271
},
7372

7473
replace: function (path) {
75-
window.history.replaceState({ path: path }, '', Path.encode(path));
74+
window.history.replaceState({ path: path }, '', encodeURI(path));
7675
notifyChange(LocationActions.REPLACE);
7776
},
7877

modules/locations/RefreshLocation.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var HistoryLocation = require('./HistoryLocation');
22
var History = require('../History');
3-
var Path = require('../utils/Path');
43

54
/**
65
* A Location that uses full page refreshes. This is used as
@@ -10,11 +9,11 @@ var Path = require('../utils/Path');
109
var RefreshLocation = {
1110

1211
push: function (path) {
13-
window.location = Path.encode(path);
12+
window.location = encodeURI(path);
1413
},
1514

1615
replace: function (path) {
17-
window.location.replace(Path.encode(path));
16+
window.location.replace(encodeURI(path));
1817
},
1918

2019
pop: History.back,

modules/locations/__tests__/HashLocation-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ var expect = require('expect');
22
var HashLocation = require('../HashLocation');
33

44
describe('HashLocation.getCurrentPath', function () {
5+
afterEach(function () {
6+
window.location.hash = '';
7+
});
58

69
//this test is needed because Firefox will pre-decode the value retrieved from
710
//window.location.hash
8-
it('returns a properly decoded equivalent of what window.location.hash is set to', function () {
11+
it('returns a properly decoded equivalent of window.location.hash', function () {
912
window.location.hash = '';
1013
expect(HashLocation.getCurrentPath()).toBe('');
1114

1215
window.location.hash = 'asdf';
1316
expect(HashLocation.getCurrentPath()).toBe('asdf');
1417

18+
// + is only special in the query component, not the hash
1519
window.location.hash = 'test+spaces';
16-
expect(HashLocation.getCurrentPath()).toBe('test spaces');
20+
expect(HashLocation.getCurrentPath()).toBe('test+spaces');
1721

1822
window.location.hash = 'first%2Fsecond';
1923
expect(HashLocation.getCurrentPath()).toBe('first%2Fsecond');
@@ -24,7 +28,7 @@ describe('HashLocation.getCurrentPath', function () {
2428
window.location.hash = 'first%252Fsecond';
2529
expect(HashLocation.getCurrentPath()).toBe('first%2Fsecond');
2630

27-
//decodeURI doesn't handle lone percents
31+
// decodeURI doesn't handle lone percents
2832
window.location.hash = '%';
2933
expect(function () {
3034
HashLocation.getCurrentPath();
@@ -36,10 +40,6 @@ describe('HashLocation.getCurrentPath', function () {
3640
window.location.hash =
3741
'complicated+string/full%2Fof%3Fspecial%25chars%2520and%23escapes%E1%88%B4';
3842
expect(HashLocation.getCurrentPath())
39-
.toBe('complicated string/full%2Fof%3Fspecial%chars%20and%23escapesሴ');
40-
});
41-
42-
afterEach(function () {
43-
window.location.hash = '';
43+
.toBe('complicated+string/full%2Fof%3Fspecial%chars%20and%23escapesሴ');
4444
});
4545
});

modules/utils/Path.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ function compilePattern(pattern) {
3535

3636
var Path = {
3737

38-
/**
39-
* Safely decodes special characters in the given URL path.
40-
*/
41-
decode: function (path) {
42-
return decodeURI(path.replace(/\+/g, ' '));
43-
},
44-
45-
/**
46-
* Safely encodes special characters in the given URL path.
47-
*/
48-
encode: function (path) {
49-
return encodeURI(path).replace(/%20/g, '+');
50-
},
51-
5238
/**
5339
* Returns an array of the names of all parameters in the given pattern.
5440
*/

modules/utils/__tests__/Path-test.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
var expect = require('expect');
22
var Path = require('../Path');
33

4-
describe('Path.decode', function () {
5-
it('properly decodes a path with a query string', function () {
6-
expect(Path.decode('/my/short+path?a=b&c=d')).toEqual('/my/short path?a=b&c=d');
7-
});
8-
});
9-
10-
describe('Path.encode', function () {
11-
it('properly encodes a path with a query string', function () {
12-
expect(Path.encode('/my/short path?a=b&c=d')).toEqual('/my/short+path?a=b&c=d');
13-
});
14-
});
15-
164
describe('Path.extractParamNames', function () {
175
describe('when a pattern contains no dynamic segments', function () {
186
it('returns an empty array', function () {

0 commit comments

Comments
 (0)