1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 00:21:05 -05:00

fix(std/path): Percent-decode in fromFileUrl() (#6913)

This commit is contained in:
Nayeem Rahman 2020-07-29 23:23:09 +01:00 committed by GitHub
parent 1b60840f28
commit 6afe7bbd39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 5 deletions

View file

@ -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:\\");

View file

@ -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/"));
}

View file

@ -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, "\\"),
);
}