Skip to content

Commit

Permalink
fix(encodeURL): correct URL search parameter encoding
Browse files Browse the repository at this point in the history
fix #411
  • Loading branch information
uiolee committed Feb 8, 2025
1 parent 313dee3 commit f55c904
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/encode_url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const encodeURL = (str: string) => {
// Exit if input is a data url
if (parsed.origin === 'null') return str;

parsed.search = encodeURI(unescape(parsed.search));
parsed.search = new URLSearchParams(parsed.search).toString();
parsed.pathname = encodeURI(decodeURI(parsed.pathname));
// preserve IDN
return format(parsed, { unicode: true });
Expand Down
5 changes: 5 additions & 0 deletions test/encode_url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,9 @@ describe('encodeURL', () => {
const content = 'https://fóo.com/páth%20[square]';
encodeURL(content).should.eql('https://fóo.com/p%C3%A1th%20%5Bsquare%5D');
});
it('perserve escape in search', () => {
// https://github.com/hexojs/hexo-util/issues/411
const content = 'https://fóo.com/path?search1=2%2B2&search2=bár';
encodeURL(content).should.eql(content.replace('bár', 'b%C3%A1r'));
});
});

0 comments on commit f55c904

Please sign in to comment.