Skip to content

Commit dde5f42

Browse files
committed
Use + instead of %20 to encode spaces in URLs
1 parent 737de0d commit dde5f42

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

modules/helpers/Path.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var invariant = require('react/lib/invariant');
22
var merge = require('react/lib/merge');
33
var qs = require('querystring');
4+
var urlDecode = require('./urlDecode');
5+
var urlEncode = require('./urlEncode');
46

57
var paramMatcher = /((?::[a-z_$][a-z0-9_$]*)|\*)/ig;
68
var queryMatcher = /\?(.+)/;
@@ -41,14 +43,14 @@ var Path = {
4143
*/
4244
extractParams: function (pattern, path) {
4345
if (!isDynamicPattern(pattern)) {
44-
if (pattern === decodeURIComponent(path))
46+
if (pattern === urlDecode(path))
4547
return {}; // No dynamic segments, but the paths match.
4648

4749
return null;
4850
}
4951

5052
var compiled = compilePattern(pattern);
51-
var match = decodeURIComponent(path).match(compiled.matcher);
53+
var match = urlDecode(path).match(compiled.matcher);
5254

5355
if (!match)
5456
return null;
@@ -87,7 +89,7 @@ var Path = {
8789
'Missing "' + paramName + '" parameter for path "' + pattern + '"'
8890
);
8991

90-
return encodeURIComponent(params[paramName]);
92+
return urlEncode(params[paramName]);
9193
});
9294
},
9395

modules/helpers/urlDecode.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** This function was copied from the https://github.com/cujojs/rest source, MIT licensed */
2+
3+
var urlEncodedSpaceRE = /\+/g;
4+
5+
function urlDecode(str) {
6+
// spec says space should be encoded as '+'
7+
str = str.replace(urlEncodedSpaceRE, ' ');
8+
return decodeURIComponent(str);
9+
}
10+
11+
module.exports = urlDecode;

modules/helpers/urlEncode.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/** This function was copied from the https://github.com/cujojs/rest source, MIT licensed */
2+
3+
var encodedSpaceRE = /%20/g;
4+
5+
function urlEncode(str) {
6+
str = encodeURIComponent(str);
7+
// spec says space should be encoded as '+'
8+
return str.replace(encodedSpaceRE, '+');
9+
}
10+
11+
module.exports = urlEncode;

specs/Path.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ describe('Path.extractParams', function () {
3939

4040
describe('and the path matches', function () {
4141
it('returns an empty object', function () {
42-
expect(Path.extractParams(pattern, 'one%2C%20two')).toEqual({});
42+
expect(Path.extractParams(pattern, 'one%2C+two')).toEqual({});
4343
});
4444
});
4545

4646
describe('and the path does not match', function () {
4747
it('returns null', function () {
48-
expect(Path.extractParams(pattern, 'one%20two')).toBe(null);
48+
expect(Path.extractParams(pattern, 'one+two')).toBe(null);
4949
});
5050
});
5151
});
@@ -55,7 +55,7 @@ describe('Path.extractParams', function () {
5555

5656
describe('and the path matches', function () {
5757
it('returns an object with the params', function () {
58-
expect(Path.extractParams(pattern, '/comments/abc/edit%20now')).toEqual({ id: 'abc' });
58+
expect(Path.extractParams(pattern, '/comments/abc/edit+now')).toEqual({ id: 'abc' });
5959
});
6060
});
6161

@@ -131,7 +131,7 @@ describe('Path.injectParams', function () {
131131

132132
describe('and some params have special URL encoding', function () {
133133
it('returns the correct path', function () {
134-
expect(Path.injectParams(pattern, { id: 'one, two' })).toEqual('comments/one%2C%20two/edit');
134+
expect(Path.injectParams(pattern, { id: 'one, two' })).toEqual('comments/one%2C+two/edit');
135135
});
136136
});
137137
});

0 commit comments

Comments
 (0)