Skip to content

Commit

Permalink
feat(encodeUrl): encode pathname (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
uiolee committed Apr 11, 2024
1 parent d636206 commit 313dee3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/encode_url.ts
Expand Up @@ -9,6 +9,7 @@ const encodeURL = (str: string) => {
if (parsed.origin === 'null') return str;

parsed.search = encodeURI(unescape(parsed.search));
parsed.pathname = encodeURI(decodeURI(parsed.pathname));
// preserve IDN
return format(parsed, { unicode: true });
}
Expand Down
4 changes: 4 additions & 0 deletions test/encode_url.spec.ts
Expand Up @@ -102,4 +102,8 @@ describe('encodeURL', () => {
const content = 'data:,Hello%2C%20World!';
encodeURL(content).should.eql(content);
});
it('encode pathname', () => {
const content = 'https://fóo.com/páth%20[square]';
encodeURL(content).should.eql('https://fóo.com/p%C3%A1th%20%5Bsquare%5D');
});
});

0 comments on commit 313dee3

Please sign in to comment.