From 5a1ed33d9fed6fe4b91c96526e6eb806ca5351aa Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sat, 22 Nov 2014 09:48:23 -0800 Subject: [PATCH] [fixed] Path.decode/encode with query string Fixes #490 --- modules/utils/Path.js | 16 ++++------------ modules/utils/__tests__/Path-test.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/modules/utils/Path.js b/modules/utils/Path.js index 663787f5eb..ea798b4fd3 100644 --- a/modules/utils/Path.js +++ b/modules/utils/Path.js @@ -2,14 +2,6 @@ var invariant = require('react/lib/invariant'); var merge = require('qs/lib/utils').merge; var qs = require('qs'); -function decodePathSegment(string) { - return decodeURIComponent(string.replace(/\+/g, ' ')); -} - -function encodePathSegment(string) { - return encodeURIComponent(string).replace(/%20/g, '+'); -} - var paramCompileMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$]*)|[*.()\[\]\\+|{}^$]/g; var paramInjectMatcher = /:([a-zA-Z_$][a-zA-Z0-9_$?]*[?]?)|[*]/g; var paramInjectTrailingSlashMatcher = /\/\/\?|\/\?/g; @@ -46,15 +38,15 @@ var Path = { /** * Safely decodes special characters in the given URL path. */ - decode: function decodePath(path) { - return String(path).split('/').map(decodePathSegment).join('/'); + decode: function (path) { + return decodeURI(path.replace(/\+/g, ' ')); }, /** * Safely encodes special characters in the given URL path. */ - encode: function encodePath(path) { - return String(path).split('/').map(encodePathSegment).join('/'); + encode: function (path) { + return encodeURI(path).replace(/%20/g, '+'); }, /** diff --git a/modules/utils/__tests__/Path-test.js b/modules/utils/__tests__/Path-test.js index 98510b46e2..36dc3baa95 100644 --- a/modules/utils/__tests__/Path-test.js +++ b/modules/utils/__tests__/Path-test.js @@ -1,6 +1,18 @@ var expect = require('expect'); var Path = require('../Path'); +describe('Path.decode', function () { + it('properly decodes a path with a query string', function () { + expect(Path.decode('/my/short+path?a=b&c=d')).toEqual('/my/short path?a=b&c=d'); + }); +}); + +describe('Path.encode', function () { + it('properly encodes a path with a query string', function () { + expect(Path.encode('/my/short path?a=b&c=d')).toEqual('/my/short+path?a=b&c=d'); + }); +}); + describe('Path.extractParamNames', function () { describe('when a pattern contains no dynamic segments', function () { it('returns an empty array', function () {