Skip to content

Commit 34575db

Browse files
jonkoopsrolandjitsu
authored andcommitted
chore!: drop support for Internet Explorer
Signed-off-by: Jon Koops <[email protected]> BREAKING CHANGE: drops support for Internet Explorer
1 parent 70cbb90 commit 34575db

File tree

2 files changed

+19
-76
lines changed

2 files changed

+19
-76
lines changed

src/file-selector.spec.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,6 @@ it("should return an empty array if the passed event is not a DragEvent", async
7575
expect(files).toHaveLength(0);
7676
});
7777

78-
it("should return {files} from DataTransfer if {items} is not defined (e.g. IE11)", async () => {
79-
const name = "test.json";
80-
const mockFile = createFile(
81-
name,
82-
{ ping: true },
83-
{
84-
type: "application/json",
85-
},
86-
);
87-
const evt = dragEvt([mockFile]);
88-
89-
const files = await fromEvent(evt);
90-
expect(files).toHaveLength(1);
91-
expect(files.every((file) => file instanceof File)).toBe(true);
92-
93-
const [file] = files as FileWithPath[];
94-
95-
expect(file.name).toBe(mockFile.name);
96-
expect(file.type).toBe(mockFile.type);
97-
expect(file.size).toBe(mockFile.size);
98-
expect(file.lastModified).toBe(mockFile.lastModified);
99-
expect(file.path).toBe(`./${name}`);
100-
});
101-
10278
it("should return files from DataTransfer {items} if the passed event is a DragEvent", async () => {
10379
const name = "test.json";
10480
const mockFile = createFile(
@@ -269,7 +245,8 @@ it("filters thumbnail cache files", async () => {
269245
type: "text/plain",
270246
},
271247
);
272-
const evt = dragEvt([mockFile]);
248+
const item = dataTransferItemFromFile(mockFile);
249+
const evt = dragEvtFromFilesAndItems([], [item]);
273250
const items = await fromEvent(evt);
274251
expect(items).toHaveLength(0);
275252
});
@@ -363,17 +340,6 @@ function dragEvtFromItems(
363340
} as any;
364341
}
365342

366-
function dragEvt(
367-
files?: File[],
368-
items?: DataTransferItem[],
369-
type: string = "drop",
370-
): DragEvent {
371-
return {
372-
type,
373-
dataTransfer: { items, files },
374-
} as any;
375-
}
376-
377343
function dragEvtFromFilesAndItems(
378344
files: File[],
379345
items: DataTransferItem[],

src/file-selector.ts

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ function isObject<T>(v: any): v is T {
4444
return typeof v === "object" && v !== null;
4545
}
4646

47-
function getInputFiles(evt: Event) {
48-
return fromList<FileWithPath>((evt.target as HTMLInputElement).files).map(
49-
(file) => toFileWithPath(file),
50-
);
47+
function getInputFiles(event: Event): FileWithPath[] {
48+
if (!(event.target instanceof HTMLInputElement) || !event.target.files) {
49+
return [];
50+
}
51+
52+
return Array.from(event.target.files).map((file) => toFileWithPath(file));
5153
}
5254

5355
// Ee expect each handle to be https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
@@ -56,50 +58,25 @@ async function getFsHandleFiles(handles: any[]) {
5658
return files.map((file) => toFileWithPath(file));
5759
}
5860

59-
async function getDataTransferFiles(dt: DataTransfer, type: string) {
60-
// IE11 does not support dataTransfer.items
61-
// See https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/items#Browser_compatibility
62-
if (dt.items) {
63-
const items = fromList<DataTransferItem>(dt.items).filter(
64-
(item) => item.kind === "file",
65-
);
66-
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
67-
// only 'dragstart' and 'drop' has access to the data (source node)
68-
if (type !== "drop") {
69-
return items;
70-
}
71-
const files = await Promise.all(items.map(toFilePromises));
72-
return noIgnoredFiles(flatten<FileWithPath>(files));
61+
async function getDataTransferFiles(dataTransfer: DataTransfer, type: string) {
62+
const items = Array.from(dataTransfer.items).filter(
63+
(item) => item.kind === "file",
64+
);
65+
66+
// According to https://html.spec.whatwg.org/multipage/dnd.html#dndevents,
67+
// only 'dragstart' and 'drop' has access to the data (source node)
68+
if (type !== "drop") {
69+
return items;
7370
}
7471

75-
return noIgnoredFiles(
76-
fromList<FileWithPath>(dt.files).map((file) => toFileWithPath(file)),
77-
);
72+
const files = await Promise.all(items.map(toFilePromises));
73+
return noIgnoredFiles(flatten<FileWithPath>(files));
7874
}
7975

8076
function noIgnoredFiles(files: FileWithPath[]) {
8177
return files.filter((file) => FILES_TO_IGNORE.indexOf(file.name) === -1);
8278
}
8379

84-
// IE11 does not support Array.from()
85-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Browser_compatibility
86-
// https://developer.mozilla.org/en-US/docs/Web/API/FileList
87-
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItemList
88-
function fromList<T>(items: DataTransferItemList | FileList | null): T[] {
89-
if (items === null) {
90-
return [];
91-
}
92-
93-
const files = [];
94-
95-
for (let i = 0; i < items.length; i++) {
96-
const file = items[i];
97-
files.push(file);
98-
}
99-
100-
return files as any;
101-
}
102-
10380
// https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem
10481
function toFilePromises(item: DataTransferItem) {
10582
if (typeof item.webkitGetAsEntry !== "function") {

0 commit comments

Comments
 (0)