GeezerVison is showing github.com — fix(shared): one-letter link schemes no longer become file links on W… · Pipyakas/t3code@a61291e
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: packages/shared/src/fileLinks.test.ts +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,17 @@ describe("parseFileUrlHref", () => { |
103 | 103 | it.each(["https://example.com/a.ts", "file://%", "/Users/julius/a.ts"])("rejects %s", (href) => { |
104 | 104 | expect(parseFileUrlHref(href)).toBeNull(); |
105 | 105 | }); |
| 106 | + |
| 107 | + it("rejects one-letter schemes that Chromium on Windows reads as drive letters", () => { |
| 108 | + const NativeURL = URL; |
| 109 | + using _url = vi.spyOn(globalThis, "URL").mockImplementation(function (input: string | URL) { |
| 110 | + const drive = /^([a-z]):/i.exec(String(input)); |
| 111 | + return drive |
| 112 | + ? new NativeURL(`file:///${drive[1]!.toUpperCase()}:/${String(input).slice(2)}`) |
| 113 | + : new NativeURL(input); |
| 114 | + }); |
| 115 | + expect(parseFileUrlHref("d:x")).toBeNull(); |
| 116 | + }); |
106 | 117 | }); |
107 | 118 |
|
108 | 119 | describe("splitFilePathPosition", () => { |
|
Collapse file
Copy file name to clipboardExpand all lines: packages/shared/src/fileLinks.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,10 @@ const POSITION_HASH_PATTERN = /^#L(\d+)(?:C(\d+)|-L?(\d+))?$/i; |
13 | 13 | export function parseFileUrlHref( |
14 | 14 | href: string, |
15 | 15 | ): { readonly path: string; readonly hash: string } | null { |
| 16 | + // Chromium on Windows parses any one-letter scheme as a drive letter |
| 17 | + // (`new URL("d:x")` is `file:///D:/x`), so the protocol check alone would turn |
| 18 | + // such links into file links only on Windows. |
| 19 | + if (!href.trim().toLowerCase().startsWith("file:")) return null; |
16 | 20 | try { |
17 | 21 | const parsed = new URL(href); |
18 | 22 | if (parsed.protocol.toLowerCase() !== "file:") return null; |
|