Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions e2e/input.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,34 @@ test("keeps webkitRelativePath from <input webkitdirectory>", 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: "{}"
});
});
18 changes: 18 additions & 0 deletions src/file-selector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/file-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)[]> {
Expand Down
Loading