Skip to content

Commit 2bba886

Browse files
committed
fix(convert/pathToUri): improve encoding of path other than the file
1 parent 2145ca9 commit 2bba886

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/convert.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default class Convert {
1717
* @returns The Uri corresponding to the path. e.g. file:///a/b/c.txt
1818
*/
1919
public static pathToUri(filePath: string): string {
20+
if (new URL(filePath, "file://").protocol !== "file:") {
21+
return filePath
22+
}
2023
let newPath = filePath.replace(/\\/g, "/")
2124
if (newPath[0] !== "/") {
2225
newPath = `/${newPath}`

test/convert.test.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ describe("Convert", () => {
2525
})
2626

2727
describe("pathToUri", () => {
28+
it("does not convert path other than file:", () => {
29+
expect(Convert.pathToUri("http://atom.io/a")).toBe("http://atom.io/a")
30+
expect(Convert.pathToUri("https://atom.io/b")).toBe("https://atom.io/b")
31+
expect(Convert.pathToUri("deno:/hello.js")).toBe("deno:/hello.js")
32+
})
33+
34+
it("does not convert non-alphanumeric path other than file:", () => {
35+
expect(Convert.pathToUri("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82")
36+
expect(Convert.pathToUri("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar")
37+
expect(Convert.pathToUri("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js")
38+
})
39+
2840
it("prefixes an absolute path with file://", () => {
2941
expect(Convert.pathToUri("/a/b/c/d.txt")).toBe("file:///a/b/c/d.txt")
3042
})
@@ -47,10 +59,18 @@ describe("Convert", () => {
4759
})
4860

4961
describe("uriToPath", () => {
50-
it("does not convert http: and https: uri's", () => {
62+
it("does not convert uri other than file:", () => {
5163
setProcessPlatform("darwin")
5264
expect(Convert.uriToPath("http://atom.io/a")).toBe("http://atom.io/a")
5365
expect(Convert.uriToPath("https://atom.io/b")).toBe("https://atom.io/b")
66+
expect(Convert.uriToPath("deno:/hello.js")).toBe("deno:/hello.js")
67+
})
68+
69+
it("does not convert non-alphanumeric uri other than file:", () => {
70+
setProcessPlatform("darwin")
71+
expect(Convert.uriToPath("http://atom.io/a%40%E3%81%82")).toBe("http://atom.io/a%40%E3%81%82")
72+
expect(Convert.uriToPath("https://atom.io/b?foo=bar")).toBe("https://atom.io/b?foo=bar")
73+
expect(Convert.uriToPath("deno:/hello%40%E3%81%82.js")).toBe("deno:/hello%40%E3%81%82.js")
5474
})
5575

5676
it("converts a file:// path to an absolute path", () => {

0 commit comments

Comments
 (0)