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
This commit is contained in:
Tom Schuster 2021-11-11 09:59:42 +00:00
Родитель 3c994f8afd
Коммит 6357cf7a44
2 изменённых файлов: 35 добавлений и 0 удалений

Просмотреть файл

@ -459,6 +459,20 @@ void DataTransferItemList::GetTypes(nsTArray<nsString>& 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<DataTransferItem>& item : mIndexedItems[i]) {
MOZ_ASSERT(item);
foundFile = item->Kind() == DataTransferItem::KIND_FILE;
if (foundFile) {
break;
}
}
}
}
if (foundFile) { if (foundFile) {
aTypes.AppendElement(u"Files"_ns); aTypes.AppendElement(u"Files"_ns);
} }

Просмотреть файл

@ -112,4 +112,25 @@ test(() => {
dt.types = 42; dt.types = 42;
assert_equals(dt.types, types); assert_equals(dt.types, types);
}, "Verify type is a read-only attribute"); }, "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");
</script> </script>