diff --git a/lib/decode_url.ts b/lib/decode_url.ts index c1410e07..6d31db79 100644 --- a/lib/decode_url.ts +++ b/lib/decode_url.ts @@ -1,8 +1,12 @@ -import { parse, format } from 'url'; +import { format, parse } from 'url'; import { unescape } from 'querystring'; const decodeURL = (str: string) => { - if (parse(str).protocol) { + const index = str.indexOf(':'); + if (index < 0) { + return unescape(str); + } + if (parse(str.slice(0, index + 1)).protocol) { const parsed = new URL(str); // Exit if input is a data url diff --git a/lib/encode_url.ts b/lib/encode_url.ts index 8934b2d1..64705930 100644 --- a/lib/encode_url.ts +++ b/lib/encode_url.ts @@ -1,8 +1,12 @@ -import { parse, format } from 'url'; +import { format, parse } from 'url'; import { unescape } from 'querystring'; const encodeURL = (str: string) => { - if (parse(str).protocol) { + const index = str.indexOf(':'); + if (index < 0) { + return encodeURI(unescape(str)); + } + if (parse(str.slice(0, index + 1)).protocol) { const parsed = new URL(str); // Exit if input is a data url