From 6357cf7a44517b525b9f0dc5386447e079e81469 Mon Sep 17 00:00:00 2001 From: Tom Schuster Date: Thu, 11 Nov 2021 09:59:42 +0000 Subject: [PATCH] Bug 1737041 - DataTransfer.types should include 'Files'. r=masayuki It almost feels like we should just use mItems for this, because AppendNewItem right above this function already seems to handle adding files correctly. However that would break GetTypes system callers that expect additional types. I am not sure if I should try updating HasFile to follow GetTypes, because https://bugzilla.mozilla.org/show_bug.cgi?id=1623239 already ran into issues with a similar change. Differential Revision: https://phabricator.services.mozilla.com/D130491 --- dom/events/DataTransferItemList.cpp | 14 +++++++++++++ .../dnd/datastore/datatransfer-types.html | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/dom/events/DataTransferItemList.cpp b/dom/events/DataTransferItemList.cpp index cdadb7727fd0..f5c84f75f835 100644 --- a/dom/events/DataTransferItemList.cpp +++ b/dom/events/DataTransferItemList.cpp @@ -459,6 +459,20 @@ void DataTransferItemList::GetTypes(nsTArray& aTypes, } } + // Additional files will be added at a non-zero index. + if (!foundFile) { + for (uint32_t i = 1; i < mIndexedItems.Length(); i++) { + for (const RefPtr& item : mIndexedItems[i]) { + MOZ_ASSERT(item); + + foundFile = item->Kind() == DataTransferItem::KIND_FILE; + if (foundFile) { + break; + } + } + } + } + if (foundFile) { aTypes.AppendElement(u"Files"_ns); } diff --git a/testing/web-platform/tests/html/editing/dnd/datastore/datatransfer-types.html b/testing/web-platform/tests/html/editing/dnd/datastore/datatransfer-types.html index 3da94d78c648..cd9568987e0a 100644 --- a/testing/web-platform/tests/html/editing/dnd/datastore/datatransfer-types.html +++ b/testing/web-platform/tests/html/editing/dnd/datastore/datatransfer-types.html @@ -112,4 +112,25 @@ test(() => { dt.types = 42; assert_equals(dt.types, types); }, "Verify type is a read-only attribute"); + +test(() => { + const dt = new DataTransfer(); + assert_array_equals(dt.types, []); + + // The added File is respected + dt.items.add(new File(["abc"], "test.txt")); + assert_array_equals(dt.types, ["Files"]); + + // "Files" is always last even after setting data + dt.setData("text/plain", "test"); + assert_array_equals(dt.types, ["text/plain", "Files"]); + + // Removing the File changes types + dt.items.remove(0); + assert_array_equals(dt.types, ["text/plain"]); + + // Adding back a File as the second item works correctly. + dt.items.add(new File(["abc"], "test.txt")); + assert_array_equals(dt.types, ["text/plain", "Files"]); +}, "DataTransfer containing files");