From f55c90433c04198e9ce1708526ce497725ab45b1 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Sat, 8 Feb 2025 23:00:49 +0800 Subject: [PATCH] fix(encodeURL): correct URL search parameter encoding fix hexojs/hexo-util#411 --- lib/encode_url.ts | 2 +- test/encode_url.spec.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/encode_url.ts b/lib/encode_url.ts index 53242adc..8934b2d1 100644 --- a/lib/encode_url.ts +++ b/lib/encode_url.ts @@ -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 }); diff --git a/test/encode_url.spec.ts b/test/encode_url.spec.ts index 9ebb98f2..5c6494d2 100644 --- a/test/encode_url.spec.ts +++ b/test/encode_url.spec.ts @@ -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')); + }); });