From b3bfd02e64b4e5a0754c157b18dd42427c931923 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 21 Aug 2024 08:57:16 -0400 Subject: [PATCH] LibWeb: Store a list of DataTransferItem on the DataTransfer object When scripts receive a DataTransferItem from any IDL method, the spec requires we return the same DataTransferItem for a particular item in the drag data store. Meaning, we cannot just create these on the fly as needed. To do this, we store a list of DataTransferItem on the DataTransfer object. We will return one of these objects any time one is requested by a script. It feels a bit weird to have the DataTransfer object store: the drag data store, a DataTransferItemList, and a list of DataTransferItem. But this is how other engines implement this as well. It basically has to be this way as DataTransferItemList is just a proxy to the DataTransfer - meaning, DataTransfer is the source of truth for all IDL access. --- Userland/Libraries/LibWeb/HTML/DataTransfer.cpp | 8 ++++++++ Userland/Libraries/LibWeb/HTML/DataTransfer.h | 1 + 2 files changed, 9 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp b/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp index 9686a57674d..930ce99ccd0 100644 --- a/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp +++ b/Userland/Libraries/LibWeb/HTML/DataTransfer.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -12,6 +13,7 @@ #include #include #include +#include #include #include @@ -51,6 +53,11 @@ DataTransfer::DataTransfer(JS::Realm& realm, NonnullRefPtr drag_d : PlatformObject(realm) , m_associated_drag_data_store(move(drag_data_store)) { + for (auto const& [i, item] : enumerate(m_associated_drag_data_store->item_list())) { + auto data_transfer_item = DataTransferItem::create(realm, *this, i); + m_item_list.append(data_transfer_item); + } + update_data_transfer_types_list(); } @@ -66,6 +73,7 @@ void DataTransfer::visit_edges(JS::Cell::Visitor& visitor) { Base::visit_edges(visitor); visitor.visit(m_items); + visitor.visit(m_item_list); } void DataTransfer::set_drop_effect(String const& drop_effect) diff --git a/Userland/Libraries/LibWeb/HTML/DataTransfer.h b/Userland/Libraries/LibWeb/HTML/DataTransfer.h index eb6941df74d..b4ad54a2f70 100644 --- a/Userland/Libraries/LibWeb/HTML/DataTransfer.h +++ b/Userland/Libraries/LibWeb/HTML/DataTransfer.h @@ -74,6 +74,7 @@ private: // https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer-items JS::GCPtr m_items; + Vector> m_item_list; // https://html.spec.whatwg.org/multipage/dnd.html#concept-datatransfer-types Vector m_types;