From 65b8f54100bdd5b4a86f96aedc56e02812213308 Mon Sep 17 00:00:00 2001 From: uiolee <22849383+uiolee@users.noreply.github.com> Date: Wed, 27 Dec 2023 16:46:34 +0800 Subject: [PATCH] feat(encodeUrl): encode pathname --- lib/encode_url.ts | 1 + test/encode_url.spec.ts | 6 ++++++ 2 files changed, 7 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..b25fb9a1 100644 --- a/test/encode_url.spec.ts +++ b/test/encode_url.spec.ts @@ -102,4 +102,10 @@ 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]'; + const result = encodeURL(content); + console.log(result); + result.should.eql('https://fóo.com/p%C3%A1th%20%5Bsquare%5D'); + }); });