Skip to content
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

Update LocationUtils.js #797

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions modules/LocationUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,8 @@ export function createLocation(path, state, key, currentLocation) {
if (state !== undefined && location.state === undefined)
location.state = state;
}

try {
location.pathname = decodeURI(location.pathname);
} catch (e) {
if (e instanceof URIError) {
throw new URIError(
'Pathname "' +
location.pathname +
'" could not be decoded. ' +
'This is likely caused by an invalid percent-encoding.'
);
} else {
throw e;
}
}

validateSegmentsCanBeDecoded(location.pathname);

if (key) location.key = key;

Expand All @@ -69,6 +56,27 @@ export function createLocation(path, state, key, currentLocation) {
return location;
}

function validateSegmentsCanBeDecoded(pathname){
var segments = pathname.split('/');

for(var i = 0; i < segments.length; ++i) {
try {
decodeURIComponent(segments[i]);
} catch (e) {
if (e instanceof URIError) {
throw new URIError(
'Pathname "' +
pathname +
'" has segment(s) that cannot be decoded. ' +
'This is likely caused by an invalid percent-encoding.'
);
} else {
throw e;
}
}
}
}

export function locationsAreEqual(a, b) {
return (
a.pathname === b.pathname &&
Expand Down
4 changes: 2 additions & 2 deletions modules/__tests__/TestSequences/LocationPathnameAlwaysSame.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export default function(history, done) {
},
location => {
expect(location).toMatchObject({
pathname: '/歴史'
pathname: '/%E6%AD%B4%E5%8F%B2'
});
// encoded object
const pathname = '/%E6%AD%B4%E5%8F%B2';
history.replace({ pathname });
},
location => {
expect(location).toMatchObject({
pathname: '/歴史'
pathname: '/%E6%AD%B4%E5%8F%B2'
});
// unencoded string
const pathname = '/歴史';
Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/TestSequences/PushInvalidPathname.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function(history, done) {
expect(() => {
history.push('/hello%');
}).toThrow(
'Pathname "/hello%" could not be decoded. This is likely caused by an invalid percent-encoding.'
'Pathname "/hello%" has segment(s) that cannot be decoded. This is likely caused by an invalid percent-encoding.'
);
}
];
Expand Down
2 changes: 1 addition & 1 deletion modules/__tests__/TestSequences/ReplaceInvalidPathname.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function(history, done) {
expect(() => {
history.replace('/hello%');
}).toThrow(
'Pathname "/hello%" could not be decoded. This is likely caused by an invalid percent-encoding.'
'Pathname "/hello%" has segment(s) that cannot be decoded. This is likely caused by an invalid percent-encoding.'
);
}
];
Expand Down