LibWeb: Implement DataTransfer.clearData()

This commit is contained in:
Tim Ledbetter 2025-09-05 22:05:08 +01:00 committed by Jelle Raaijmakers
commit 0c1532626f
Notes: github-actions[bot] 2025-09-12 10:31:44 +00:00
3 changed files with 48 additions and 1 deletions

View file

@ -264,6 +264,52 @@ void DataTransfer::remove_item(size_t index)
update_data_transfer_types_list();
}
// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-cleardata
void DataTransfer::clear_data(Optional<String> maybe_format)
{
// 1. If the DataTransfer object is no longer associated with a drag data store, return. Nothing happens.
if (!m_associated_drag_data_store)
return;
// 2. If the drag data store's mode is not the read/write mode, return. Nothing happens.
if (m_associated_drag_data_store->mode() != DragDataStore::Mode::ReadWrite)
return;
auto remove_items_from_drag_data_store = [&](Optional<String> const& format = {}) {
auto did_remove_item = false;
for (size_t i = m_associated_drag_data_store->item_list().size(); i > 0; --i) {
auto const& item = m_associated_drag_data_store->item_list().at(i - 1);
if (item.kind == DragDataStoreItem::Kind::Text && (!format.has_value() || item.type_string == *format)) {
m_associated_drag_data_store->remove_item_at(i - 1);
did_remove_item = true;
}
}
if (did_remove_item)
update_data_transfer_types_list();
};
// 3. If the method was called with no arguments, remove each item in the drag data store item list whose kind is
// Plain Unicode string, and return.
if (!maybe_format.has_value()) {
remove_items_from_drag_data_store();
return;
}
// 4. Set format to format, converted to ASCII lowercase.
auto format = maybe_format->to_ascii_lowercase();
// 5. If format equals "text", change it to "text/plain".
// If format equals "url", change it to "text/uri-list".
if (format == "text"sv) {
format = "text/plain"_string;
} else if (format == "url"sv) {
format = "text/uri-list"_string;
}
// 6. Remove each item in the drag data store item list whose kind is Plain Unicode string and whose type string is equal to format.
remove_items_from_drag_data_store(format);
}
bool DataTransfer::contains_item_with_type(DragDataStoreItem::Kind kind, String const& type) const
{
VERIFY(m_associated_drag_data_store);

View file

@ -54,6 +54,7 @@ public:
ReadonlySpan<String> types() const;
String get_data(String const& format) const;
void clear_data(Optional<String> maybe_format = {});
GC::Ref<FileAPI::FileList> files() const;
Optional<DragDataStore::Mode> mode() const;

View file

@ -17,6 +17,6 @@ interface DataTransfer {
readonly attribute FrozenArray<DOMString> types;
DOMString getData(DOMString format);
[FIXME] undefined setData(DOMString format, DOMString data);
[FIXME] undefined clearData(optional DOMString format);
undefined clearData(optional DOMString format);
[SameObject] readonly attribute FileList files;
};