diff options
author | Nayeem Rahman <nayeemrmn99@gmail.com> | 2020-07-24 02:37:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-23 21:37:11 -0400 |
commit | a8f74aa381c99e9c3c3d8fdfde02919966a3a824 (patch) | |
tree | d1dc35a1d56bb1845f2389313eda5887682a6632 /std/path | |
parent | b61347b2552cb7159ee3e82c5e9ca5a76e73c3f8 (diff) |
fix: Improve URL compatibility (#6807)
- Fix protocol regex.
- Truncate repeated leading slashes in file paths.
- Make drive letter support platform-independent.
- Drop the hostname if a drive letter is parsed.
- Fix drive letter normalization and basing.
- Allow basing over the host.
- Fix same-protocol basing.
- Remove Windows UNC path support.
- Reverts #6418. This is non-standard. Wouldn't be too much of a problem but it
makes other parts of the spec hard to realize.
Diffstat (limited to 'std/path')
-rw-r--r-- | std/path/from_file_url_test.ts | 26 | ||||
-rw-r--r-- | std/path/posix.ts | 3 | ||||
-rw-r--r-- | std/path/win32.ts | 2 |
3 files changed, 11 insertions, 20 deletions
diff --git a/std/path/from_file_url_test.ts b/std/path/from_file_url_test.ts index 8bbc4e986..7a0432f68 100644 --- a/std/path/from_file_url_test.ts +++ b/std/path/from_file_url_test.ts @@ -7,14 +7,13 @@ Deno.test("[path] fromFileUrl", function () { assertEquals(posix.fromFileUrl("file:///home/foo"), "/home/foo"); assertEquals(posix.fromFileUrl("https://example.com/foo"), "/foo"); assertEquals(posix.fromFileUrl("file:///"), "/"); - // FIXME(nayeemrmn): Remove the condition. UNC paths are supported here when - // run on Windows (matching the underlying URL class), but - // `posix.fromFileUrl()` should not support them under any circumstance. - if (Deno.build.os != "windows") { - assertEquals(posix.fromFileUrl("file:////"), "//"); - assertEquals(posix.fromFileUrl("file:////server"), "//server"); - assertEquals(posix.fromFileUrl("file:////server/file"), "//server/file"); - } + // Drive letters are supported platform-independently to align with the WHATWG + // URL specification. + assertEquals(posix.fromFileUrl("file:///c:"), "c:/"); + assertEquals(posix.fromFileUrl("file:///c:/"), "c:/"); + assertEquals(posix.fromFileUrl("file:///C:/"), "C:/"); + assertEquals(posix.fromFileUrl("file:///C:/Users/"), "C:/Users/"); + assertEquals(posix.fromFileUrl("file:///C:foo/bar"), "/C:foo/bar"); }); Deno.test("[path] fromFileUrl (win32)", function () { @@ -22,18 +21,9 @@ Deno.test("[path] fromFileUrl (win32)", function () { assertEquals(win32.fromFileUrl("file:///home/foo"), "\\home\\foo"); assertEquals(win32.fromFileUrl("https://example.com/foo"), "\\foo"); assertEquals(win32.fromFileUrl("file:///"), "\\"); - // FIXME(nayeemrmn): Remove the condition. UNC paths are only supported here - // when run on Windows (matching the underlying URL class), but - // `win32.fromFileUrl()` should support them under every circumstance. - if (Deno.build.os == "windows") { - assertEquals(win32.fromFileUrl("file:////"), "\\"); - assertEquals(win32.fromFileUrl("file:////server"), "\\"); - assertEquals(win32.fromFileUrl("file:////server/file"), "\\file"); - } - assertEquals(win32.fromFileUrl("file:///c"), "\\c"); assertEquals(win32.fromFileUrl("file:///c:"), "c:\\"); assertEquals(win32.fromFileUrl("file:///c:/"), "c:\\"); assertEquals(win32.fromFileUrl("file:///C:/"), "C:\\"); assertEquals(win32.fromFileUrl("file:///C:/Users/"), "C:\\Users\\"); - assertEquals(win32.fromFileUrl("file:///C:cwd/another"), "\\C:cwd\\another"); + assertEquals(win32.fromFileUrl("file:///C:foo/bar"), "\\C:foo\\bar"); }); diff --git a/std/path/posix.ts b/std/path/posix.ts index 03d07a84a..3c4262203 100644 --- a/std/path/posix.ts +++ b/std/path/posix.ts @@ -435,5 +435,6 @@ export function parse(path: string): ParsedPath { * are ignored. */ export function fromFileUrl(url: string | URL): string { - return new URL(String(url)).pathname; + return (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 66ed1ff14..cf0e69537 100644 --- a/std/path/win32.ts +++ b/std/path/win32.ts @@ -914,7 +914,7 @@ export function parse(path: string): ParsedPath { * are ignored. */ export function fromFileUrl(url: string | URL): string { - return new URL(String(url)).pathname + return (url instanceof URL ? url : new URL(url)).pathname .replace(/^\/*([A-Za-z]:)(\/|$)/, "$1/") .replace(/\//g, "\\"); } |