Skip to content

Commit fbc109c

Browse files
committed
[fixed] Use %20 instead of + in URL pathnames
Fixes #2407
1 parent e2a979e commit fbc109c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

modules/PatternUtils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export function compilePattern(pattern) {
7272
* - * Consumes (non-greedy) all characters up to the next
7373
* character in the pattern, or to the end of the URL if
7474
* there is none
75+
* - ** Consumes (greedy) all characters up to the next character
76+
* in the pattern, or to the end of the URL if there is none
7577
*
7678
* The return value is an object with the following properties:
7779
*
@@ -94,7 +96,7 @@ export function matchPattern(pattern, pathname) {
9496
let remainingPathname, paramValues
9597
if (match != null) {
9698
paramValues = Array.prototype.slice.call(match, 1).map(function (v) {
97-
return v != null ? decodeURIComponent(v.replace(/\+/g, '%20')) : v
99+
return v != null ? decodeURIComponent(v) : v
98100
})
99101

100102
if (captureRemaining) {
@@ -154,7 +156,7 @@ export function formatPattern(pattern, params) {
154156
)
155157

156158
if (paramValue != null)
157-
pathname += encodeURI(paramValue).replace(/%20/g, '+')
159+
pathname += encodeURI(paramValue)
158160
} else if (token === '(') {
159161
parenCount += 1
160162
} else if (token === ')') {
@@ -170,7 +172,7 @@ export function formatPattern(pattern, params) {
170172
)
171173

172174
if (paramValue != null)
173-
pathname += encodeURIComponent(paramValue).replace(/%20/g, '+')
175+
pathname += encodeURIComponent(paramValue)
174176
} else {
175177
pathname += token
176178
}

modules/__tests__/formatPattern-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ describe('formatPattern', function () {
5757

5858
describe('and some params have special URL encoding', function () {
5959
it('returns the correct path', function () {
60-
expect(formatPattern(pattern, { id: 'one, two' })).toEqual('/comments/one%2C+two/edit')
60+
expect(formatPattern(pattern, { id: 'one, two' })).toEqual('/comments/one%2C%20two/edit')
6161
})
6262
})
6363

modules/__tests__/PatternUtils-test.js renamed to modules/__tests__/matchPattern-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ describe('matchPattern', function () {
2020
assertMatch('/:id.:ext', '/path.jpg', '', [ 'id', 'ext' ], [ 'path', 'jpg' ])
2121
})
2222

23+
it('works with named params that contain spaces', function () {
24+
assertMatch('/:id', '/path+more', '', [ 'id' ], [ 'path+more' ])
25+
assertMatch('/:id', '/path%20more', '', [ 'id' ], [ 'path more' ])
26+
})
27+
2328
it('works with splat params', function () {
2429
assertMatch('/files/*.*', '/files/path.jpg', '', [ 'splat', 'splat' ], [ 'path', 'jpg' ])
2530
})

0 commit comments

Comments
 (0)