From 313dee34cafd88c08bfa491dfdbbc78a6a50cf8b Mon Sep 17 00:00:00 2001 From: Uiolee <22849383+uiolee@users.noreply.github.com> Date: Fri, 12 Apr 2024 00:03:42 +0800 Subject: [PATCH] feat(encodeUrl): encode pathname (#388) --- lib/encode_url.ts | 1 + test/encode_url.spec.ts | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/encode_url.ts b/lib/encode_url.ts index b2800748..53242adc 100644 --- a/lib/encode_url.ts +++ b/lib/encode_url.ts @@ -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 }); } diff --git a/test/encode_url.spec.ts b/test/encode_url.spec.ts index 583df516..9ebb98f2 100644 --- a/test/encode_url.spec.ts +++ b/test/encode_url.spec.ts @@ -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'); + }); });