-
Notifications
You must be signed in to change notification settings - Fork 960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No encode, decode in createLocation #465
Changes from 2 commits
0733745
23f6f34
15e0f69
fb03872
e1d22fd
4bd96b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,6 @@ export const parsePath = (path) => { | |
search = pathname.substr(searchIndex) | ||
pathname = pathname.substr(0, searchIndex) | ||
} | ||
|
||
pathname = decodeURI(pathname) | ||
|
||
return { | ||
pathname, | ||
|
@@ -39,16 +37,32 @@ export const parsePath = (path) => { | |
} | ||
} | ||
|
||
const validatePath = (path) => { | ||
try { | ||
decodeURI(path) | ||
} catch(e) { | ||
if (e instanceof URIError) { | ||
throw new Error( | ||
'You are attempting to create a path that cannot be decoded. ' + | ||
'Please call encodeURI on the pathname.' | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A slightly more useful error than the default |
||
} | ||
throw e | ||
} | ||
} | ||
|
||
export const createPath = (location) => { | ||
const { pathname, search, hash } = location | ||
|
||
let path = encodeURI(pathname || '/') | ||
let path = pathname || '/' | ||
validatePath(path) | ||
|
||
if (search && search !== '?') | ||
path += (search.charAt(0) === '?' ? search : `?${search}`) | ||
|
||
if (hash && hash !== '#') | ||
path += (hash.charAt(0) === '#' ? hash : `#${hash}`) | ||
|
||
|
||
return path | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import expect from 'expect' | ||
import execSteps from './execSteps' | ||
|
||
export default (history, done) => { | ||
const steps = [ | ||
() => { | ||
// encoded string | ||
const pathname = '/view/%23abc' | ||
history.replace(pathname) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/view/%23abc' | ||
}) | ||
|
||
// encoded object | ||
const pathname = '/view/%23abc' | ||
history.replace({ pathname }) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/view/%23abc' | ||
}) | ||
// unencoded string | ||
const pathname = '/view/#abc' | ||
history.replace(pathname) | ||
} | ||
, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/view/', | ||
hash: '#abc' | ||
}) | ||
// unencoded object | ||
const pathname = '/view/#abc' | ||
history.replace({ pathname }) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/view/#abc' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not actually sure about this test case. It passes, but I'm not sure if the behavior is desirable. This only happens if the user passes a bad location object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like the kind of thing we don't actually need to test. i.e. if anyone actually tried to do this I'd probably just tell them to use |
||
}) | ||
} | ||
] | ||
|
||
execSteps(steps, history, done) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import expect from 'expect' | ||
import execSteps from './execSteps' | ||
|
||
export default (history, done) => { | ||
const steps = [ | ||
() => { | ||
// encoded string | ||
const pathname = '/%E6%AD%B4%E5%8F%B2' | ||
history.replace(pathname) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/歴史' | ||
}) | ||
|
||
// encoded object | ||
const pathname = '/%E6%AD%B4%E5%8F%B2' | ||
history.replace({ pathname }) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/歴史' | ||
}) | ||
// unencoded string | ||
const pathname = '/歴史' | ||
history.replace(pathname) | ||
} | ||
, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/歴史' | ||
}) | ||
// unencoded object | ||
const pathname = '/歴史' | ||
history.replace({ pathname }) | ||
}, | ||
(location) => { | ||
expect(location).toMatch({ | ||
pathname: '/歴史' | ||
}) | ||
} | ||
] | ||
|
||
execSteps(steps, history, done) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import expect from 'expect' | ||
import execSteps from './execSteps' | ||
|
||
export default (history, done) => { | ||
const steps = [ | ||
() => { | ||
// encoded string | ||
const pathname = '/hello%' | ||
expect(() => { | ||
history.push(pathname) | ||
}).toThrow( | ||
'You are attempting to create a path that cannot be decoded. ' + | ||
'Please call encodeURI on the pathname.' | ||
) | ||
} | ||
] | ||
|
||
let consoleError = console.error // eslint-disable-line no-console | ||
|
||
console.error = () => {} // eslint-disable-line no-console | ||
|
||
execSteps(steps, history, (...args) => { | ||
console.error = consoleError // eslint-disable-line no-console | ||
done(...args) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import expect from 'expect' | ||
import execSteps from './execSteps' | ||
|
||
export default (history, done) => { | ||
const steps = [ | ||
() => { | ||
// encoded string | ||
const pathname = '/hello%' | ||
expect(() => { | ||
history.replace(pathname) | ||
}).toThrow( | ||
'You are attempting to create a path that cannot be decoded. ' + | ||
'Please call encodeURI on the pathname.' | ||
) | ||
} | ||
] | ||
|
||
let consoleError = console.error // eslint-disable-line no-console | ||
|
||
console.error = () => {} // eslint-disable-line no-console | ||
|
||
execSteps(steps, history, (...args) => { | ||
console.error = consoleError // eslint-disable-line no-console | ||
done(...args) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we try to create a location with a bad percent-encoding, we swallow the error, keep
location.pathname
encoded, and log a warning.