From 6afe7bbd395eb34febd25bda48c5cd82f0a4f8b3 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Wed, 29 Jul 2020 23:23:09 +0100 Subject: [PATCH] fix(std/path): Percent-decode in fromFileUrl() (#6913) --- std/path/from_file_url_test.ts | 2 ++ std/path/posix.ts | 4 ++-- std/path/win32.ts | 8 +++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/std/path/from_file_url_test.ts b/std/path/from_file_url_test.ts index 7a0432f68e..8fe47b27f3 100644 --- a/std/path/from_file_url_test.ts +++ b/std/path/from_file_url_test.ts @@ -5,6 +5,7 @@ import { assertEquals } from "../testing/asserts.ts"; Deno.test("[path] fromFileUrl", function () { assertEquals(posix.fromFileUrl(new URL("file:///home/foo")), "/home/foo"); assertEquals(posix.fromFileUrl("file:///home/foo"), "/home/foo"); + assertEquals(posix.fromFileUrl("file:///home/foo%20bar"), "/home/foo bar"); assertEquals(posix.fromFileUrl("https://example.com/foo"), "/foo"); assertEquals(posix.fromFileUrl("file:///"), "/"); // Drive letters are supported platform-independently to align with the WHATWG @@ -19,6 +20,7 @@ Deno.test("[path] fromFileUrl", function () { Deno.test("[path] fromFileUrl (win32)", function () { assertEquals(win32.fromFileUrl(new URL("file:///home/foo")), "\\home\\foo"); assertEquals(win32.fromFileUrl("file:///home/foo"), "\\home\\foo"); + assertEquals(win32.fromFileUrl("file:///home/foo%20bar"), "\\home\\foo bar"); assertEquals(win32.fromFileUrl("https://example.com/foo"), "\\foo"); assertEquals(win32.fromFileUrl("file:///"), "\\"); assertEquals(win32.fromFileUrl("file:///c:"), "c:\\"); diff --git a/std/path/posix.ts b/std/path/posix.ts index 3c4262203e..fca7f081bd 100644 --- a/std/path/posix.ts +++ b/std/path/posix.ts @@ -435,6 +435,6 @@ export function parse(path: string): ParsedPath { * are ignored. */ export function fromFileUrl(url: string | URL): string { - return (url instanceof URL ? url : new URL(url)).pathname - .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/"); + return decodeURIComponent((url instanceof URL ? url : new URL(url)).pathname + .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/")); } diff --git a/std/path/win32.ts b/std/path/win32.ts index cf0e695373..0283f4b9ce 100644 --- a/std/path/win32.ts +++ b/std/path/win32.ts @@ -914,7 +914,9 @@ export function parse(path: string): ParsedPath { * are ignored. */ export function fromFileUrl(url: string | URL): string { - return (url instanceof URL ? url : new URL(url)).pathname - .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/") - .replace(/\//g, "\\"); + return decodeURIComponent( + (url instanceof URL ? url : new URL(url)).pathname + .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/") + .replace(/\//g, "\\"), + ); }