Skip to content

Commit

Permalink
[fixed] Use %20 instead of + in URL pathnames
Browse files Browse the repository at this point in the history
Fixes #2407
  • Loading branch information
mjackson committed Oct 30, 2015
1 parent e2a979e commit fbc109c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions modules/PatternUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export function compilePattern(pattern) {
* - * Consumes (non-greedy) all characters up to the next
* character in the pattern, or to the end of the URL if
* there is none
* - ** Consumes (greedy) all characters up to the next character
* in the pattern, or to the end of the URL if there is none
*
* The return value is an object with the following properties:
*
Expand All @@ -94,7 +96,7 @@ export function matchPattern(pattern, pathname) {
let remainingPathname, paramValues
if (match != null) {
paramValues = Array.prototype.slice.call(match, 1).map(function (v) {
return v != null ? decodeURIComponent(v.replace(/\+/g, '%20')) : v
return v != null ? decodeURIComponent(v) : v

This comment has been minimized.

Copy link
@mjackson

mjackson Oct 30, 2015

Author Member

This fixes #2426

})

if (captureRemaining) {
Expand Down Expand Up @@ -154,7 +156,7 @@ export function formatPattern(pattern, params) {
)

if (paramValue != null)
pathname += encodeURI(paramValue).replace(/%20/g, '+')
pathname += encodeURI(paramValue)
} else if (token === '(') {
parenCount += 1
} else if (token === ')') {
Expand All @@ -170,7 +172,7 @@ export function formatPattern(pattern, params) {
)

if (paramValue != null)
pathname += encodeURIComponent(paramValue).replace(/%20/g, '+')
pathname += encodeURIComponent(paramValue)
} else {
pathname += token
}
Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/formatPattern-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('formatPattern', function () {

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ describe('matchPattern', function () {
assertMatch('/:id.:ext', '/path.jpg', '', [ 'id', 'ext' ], [ 'path', 'jpg' ])
})

it('works with named params that contain spaces', function () {
assertMatch('/:id', '/path+more', '', [ 'id' ], [ 'path+more' ])
assertMatch('/:id', '/path%20more', '', [ 'id' ], [ 'path more' ])
})

it('works with splat params', function () {
assertMatch('/files/*.*', '/files/path.jpg', '', [ 'splat', 'splat' ], [ 'path', 'jpg' ])
})
Expand Down

0 comments on commit fbc109c

Please sign in to comment.