-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0733745
Do not encode pathnames, decode in createLocation
pshrmn 23f6f34
Throw when creating bad path, warn for bad location
pshrmn 15e0f69
Rethrow with clearer message on URIError
pshrmn fb03872
Throw URIError, add tests to memory history
pshrmn e1d22fd
Remove unnecessary test case
pshrmn 4bd96b1
Add note to CHANGES.md
pshrmn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,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 = pathname || '/' | ||
validatePath(path) | ||
|
||
if (search && search !== '?') | ||
path += (search.charAt(0) === '?' ? search : `?${search}`) | ||
|
||
if (hash && hash !== '#') | ||
path += (hash.charAt(0) === '#' ? hash : `#${hash}`) | ||
|
||
|
||
return path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.