LibWeb: Implement separate DataTransfer factories for IDL / internal use

The IDL constructor has to take separate steps than a DataTransfer that
is internally constructed. Notably, an IDL-created object has its own
drag data store, and that store is placed in a read-write mode.
This commit is contained in:
Timothy Flynn 2024-08-21 06:50:22 -04:00 committed by Andreas Kling
parent f7c4165dde
commit 5c9287aa99
Notes: github-actions[bot] 2024-08-22 12:22:55 +00:00
5 changed files with 34 additions and 14 deletions

View file

@ -0,0 +1,2 @@
dropEffect: none
effectAllowed: none

View file

@ -0,0 +1,8 @@
<script src="../include.js"></script>
<script>
test(() => {
let dataTransfer = new DataTransfer();
println(`dropEffect: ${dataTransfer.dropEffect}`);
println(`effectAllowed: ${dataTransfer.effectAllowed}`);
});
</script>

View file

@ -27,14 +27,31 @@ ENUMERATE_DATA_TRANSFER_EFFECTS
}
JS::NonnullGCPtr<DataTransfer> DataTransfer::construct_impl(JS::Realm& realm)
JS::NonnullGCPtr<DataTransfer> DataTransfer::create(JS::Realm& realm, NonnullRefPtr<DragDataStore> drag_data_store)
{
return realm.heap().allocate<DataTransfer>(realm, realm);
return realm.heap().allocate<DataTransfer>(realm, realm, move(drag_data_store));
}
DataTransfer::DataTransfer(JS::Realm& realm)
: PlatformObject(realm)
// https://html.spec.whatwg.org/multipage/dnd.html#dom-datatransfer
JS::NonnullGCPtr<DataTransfer> DataTransfer::construct_impl(JS::Realm& realm)
{
// 1. Set the drag data store's item list to be an empty list.
auto drag_data_store = DragDataStore::create();
// 2. Set the drag data store's mode to read/write mode.
drag_data_store->set_mode(DragDataStore::Mode::ReadWrite);
// 3. Set the dropEffect and effectAllowed to "none".
// NOTE: This is done by the default-initializers.
return realm.heap().allocate<DataTransfer>(realm, realm, move(drag_data_store));
}
DataTransfer::DataTransfer(JS::Realm& realm, NonnullRefPtr<DragDataStore> drag_data_store)
: PlatformObject(realm)
, m_associated_drag_data_store(move(drag_data_store))
{
update_data_transfer_types_list();
}
DataTransfer::~DataTransfer() = default;
@ -195,12 +212,6 @@ JS::NonnullGCPtr<FileAPI::FileList> DataTransfer::files() const
return files;
}
void DataTransfer::associate_with_drag_data_store(NonnullRefPtr<DragDataStore> drag_data_store)
{
m_associated_drag_data_store = move(drag_data_store);
update_data_transfer_types_list();
}
void DataTransfer::disassociate_with_drag_data_store()
{
m_associated_drag_data_store.clear();

View file

@ -37,6 +37,7 @@ class DataTransfer : public Bindings::PlatformObject {
JS_DECLARE_ALLOCATOR(DataTransfer);
public:
static JS::NonnullGCPtr<DataTransfer> create(JS::Realm&, NonnullRefPtr<DragDataStore>);
static JS::NonnullGCPtr<DataTransfer> construct_impl(JS::Realm&);
virtual ~DataTransfer() override;
@ -55,11 +56,10 @@ public:
String get_data(String const& format) const;
JS::NonnullGCPtr<FileAPI::FileList> files() const;
void associate_with_drag_data_store(NonnullRefPtr<DragDataStore> drag_data_store);
void disassociate_with_drag_data_store();
private:
DataTransfer(JS::Realm&);
DataTransfer(JS::Realm&, NonnullRefPtr<DragDataStore>);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(JS::Cell::Visitor&) override;

View file

@ -506,8 +506,7 @@ JS::NonnullGCPtr<HTML::DragEvent> DragAndDropEventHandler::fire_a_drag_and_drop_
}
// 6. Let dataTransfer be a newly created DataTransfer object associated with the given drag data store.
auto data_transfer = HTML::DataTransfer::construct_impl(realm);
data_transfer->associate_with_drag_data_store(*m_drag_data_store);
auto data_transfer = HTML::DataTransfer::create(realm, *m_drag_data_store);
// 7. Set the effectAllowed attribute to the drag data store's drag data store allowed effects state.
data_transfer->set_effect_allowed_internal(m_drag_data_store->allowed_effects_state());