Skip to content

Commit 19a1151

Browse files
authored
fix(encodeURL): correct URL search parameter encoding (#413)
* fix(encodeURL): correct URL search parameter encoding fix #411 * group test case
1 parent dea1270 commit 19a1151

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/encode_url.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const encodeURL = (str: string) => {
88
// Exit if input is a data url
99
if (parsed.origin === 'null') return str;
1010

11-
parsed.search = encodeURI(unescape(parsed.search));
11+
parsed.search = new URLSearchParams(parsed.search).toString();
1212
parsed.pathname = encodeURI(decodeURI(parsed.pathname));
1313
// preserve IDN
1414
return format(parsed, { unicode: true });

test/encode_url.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ describe('encodeURL', () => {
6363
encodeURL(content).should.eql('http://foo.com/bar?query=b%C3%A1z#f%C3%B3o');
6464
});
6565

66+
it('perserve escape in search', () => {
67+
// https://github.com/hexojs/hexo-util/issues/411
68+
const content = 'https://fóo.com/path?search1=2%2B2&search2=bár';
69+
encodeURL(content).should.eql(content.replace('bár', 'b%C3%A1r'));
70+
});
71+
6672
it('idn', () => {
6773
const content = 'http://bár.com/baz';
6874
encodeURL(content).should.eql('http://bár.com/baz');

0 commit comments

Comments
 (0)