From dee3cadcaf4e00941467dab252bf1e5d5fddd6c0 Mon Sep 17 00:00:00 2001 From: SeemRad <96733956+BelodonaQ@users.noreply.github.com> Date: Tue, 20 Oct 2026 10:20:40 +0300 Subject: [PATCH] fix: infer MIME types for files from another window Assisted-by: OpenAI Codex --- e2e/input.e2e.ts | 31 +++++++++++++++++++++++++++++++ src/file-selector.spec.ts | 18 ++++++++++++++++++ src/file-selector.ts | 3 ++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/e2e/input.e2e.ts b/e2e/input.e2e.ts index c2ba3e2..84013e3 100644 --- a/e2e/input.e2e.ts +++ b/e2e/input.e2e.ts @@ -23,3 +23,34 @@ test("keeps webkitRelativePath from ", async ({page}) => expect(files.map(f => f.name).sort()).toEqual(["ping.json", "pong.json"]); expect(files.map(f => f.path).sort()).toEqual(["tree/nested/pong.json", "tree/ping.json"]); }); + +test("infers MIME types for files created in another window", async ({page}) => { + const result = await page.evaluate(async () => { + const moduleUrl = "/dist/index.js"; + const {fromEvent} = await import(moduleUrl); + const iframe = document.createElement("iframe"); + document.body.appendChild(iframe); + try { + const otherWindow = iframe.contentWindow as Window & typeof globalThis; + const file = new otherWindow.File(["{}"], "example.json"); + const [selected] = await fromEvent({target: {files: [file]}}); + return { + isLocalFile: file instanceof File, + isSameFile: selected === file, + type: selected.type, + path: selected.path, + content: await selected.text() + }; + } finally { + iframe.remove(); + } + }); + + expect(result).toEqual({ + isLocalFile: false, + isSameFile: true, + type: "application/json", + path: "./example.json", + content: "{}" + }); +}); diff --git a/src/file-selector.spec.ts b/src/file-selector.spec.ts index 8e1f91a..9c5d4e0 100644 --- a/src/file-selector.spec.ts +++ b/src/file-selector.spec.ts @@ -46,6 +46,24 @@ it("should return an empty array if the evt {target} has no {files} prop", async expect(files).toHaveLength(0); }); +it("infers MIME types for files from another window", async () => { + const iframe = document.createElement("iframe"); + document.body.appendChild(iframe); + try { + const otherWindow = iframe.contentWindow as Window & typeof globalThis; + const file = new otherWindow.File(["{}"], "example.json"); + expect(file).not.toBeInstanceOf(File); + + const files = await fromEvent(inputEvtFromFiles(file)); + + expect(files[0]).toBe(file); + expect(files[0].type).toBe("application/json"); + expect((files[0] as FileWithPath).path).toBe("./example.json"); + } finally { + iframe.remove(); + } +}); + it("should return files if the arg is a list of FileSystemFileHandle", async () => { const name = "test.json"; const [mockFile, mockHandle] = createFileSystemFileHandle( diff --git a/src/file-selector.ts b/src/file-selector.ts index 7559f39..ae015bb 100644 --- a/src/file-selector.ts +++ b/src/file-selector.ts @@ -48,7 +48,8 @@ export async function fromEvent( const items = await getFilesOrItems(evt); // Guess a MIME type from the file extension once, over the final flat list, for any file the // browser left typeless. DataTransferItems (returned for non-'drop' drag events) pass through as-is. - return items.map(item => (item instanceof File ? withMimeType(item, mimeTypes) : item)); + // Converted files have a path even when their File constructor belongs to another window. + return items.map(item => ("path" in item ? withMimeType(item, mimeTypes) : item)); } async function getFilesOrItems(evt: Event | any): Promise<(FileWithPath | DataTransferItem)[]> {