diff --git a/Libraries/LibWeb/HTML/DataTransfer.cpp b/Libraries/LibWeb/HTML/DataTransfer.cpp
index eaa36c56e8a..bfceea2b8cb 100644
--- a/Libraries/LibWeb/HTML/DataTransfer.cpp
+++ b/Libraries/LibWeb/HTML/DataTransfer.cpp
@@ -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 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 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);
diff --git a/Libraries/LibWeb/HTML/DataTransfer.h b/Libraries/LibWeb/HTML/DataTransfer.h
index 2a963f979ce..732714d416a 100644
--- a/Libraries/LibWeb/HTML/DataTransfer.h
+++ b/Libraries/LibWeb/HTML/DataTransfer.h
@@ -54,6 +54,7 @@ public:
ReadonlySpan types() const;
String get_data(String const& format) const;
+ void clear_data(Optional maybe_format = {});
GC::Ref files() const;
Optional mode() const;
diff --git a/Libraries/LibWeb/HTML/DataTransfer.idl b/Libraries/LibWeb/HTML/DataTransfer.idl
index 4e06c7054f9..82f4da95519 100644
--- a/Libraries/LibWeb/HTML/DataTransfer.idl
+++ b/Libraries/LibWeb/HTML/DataTransfer.idl
@@ -17,6 +17,6 @@ interface DataTransfer {
readonly attribute FrozenArray 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;
};