From 0b0d44da275067458e61a5dd80c4563f6aa7fdc6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 20 Aug 2024 18:26:55 -0400 Subject: [PATCH] LibWeb: Implement the ClipboardEvent IDL interface We don't actually generate any such events ourselves. But Google Lens will create one with the DataTransfer attribute set to that of any drop event we send it. --- .../BindingsGenerator/IDLGenerators.cpp | 1 + .../Text/expected/all-window-properties.txt | 1 + Userland/Libraries/LibWeb/CMakeLists.txt | 1 + .../LibWeb/Clipboard/ClipboardEvent.cpp | 41 +++++++++++++++++++ .../LibWeb/Clipboard/ClipboardEvent.h | 41 +++++++++++++++++++ .../LibWeb/Clipboard/ClipboardEvent.idl | 13 ++++++ Userland/Libraries/LibWeb/idl_files.cmake | 1 + 7 files changed, 99 insertions(+) create mode 100644 Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.cpp create mode 100644 Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.h create mode 100644 Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.idl diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 13807ca81bc..b89bc9bf789 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -4178,6 +4178,7 @@ static void generate_using_namespace_definitions(SourceGenerator& generator) generator.append(R"~~~( // FIXME: This is a total hack until we can figure out the namespace for a given type somehow. using namespace Web::Animations; + using namespace Web::Clipboard; using namespace Web::Crypto; using namespace Web::CSS; using namespace Web::DOM; diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index d2f60b59a96..8ca728a4e84 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -51,6 +51,7 @@ CanvasPattern CanvasRenderingContext2D CharacterData Clipboard +ClipboardEvent CloseEvent CloseWatcher Comment diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 87114f9caba..944b506a03b 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES Bindings/OptionConstructor.cpp Bindings/PlatformObject.cpp Clipboard/Clipboard.cpp + Clipboard/ClipboardEvent.cpp Crypto/Crypto.cpp Crypto/CryptoAlgorithms.cpp Crypto/CryptoBindings.cpp diff --git a/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.cpp b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.cpp new file mode 100644 index 00000000000..1a8950b20c8 --- /dev/null +++ b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::Clipboard { + +JS_DEFINE_ALLOCATOR(ClipboardEvent); + +JS::NonnullGCPtr ClipboardEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, ClipboardEventInit const& event_init) +{ + return realm.heap().allocate(realm, realm, event_name, event_init); +} + +ClipboardEvent::ClipboardEvent(JS::Realm& realm, FlyString const& event_name, ClipboardEventInit const& event_init) + : DOM::Event(realm, event_name, event_init) + , m_clipboard_data(event_init.clipboard_data) +{ +} + +ClipboardEvent::~ClipboardEvent() = default; + +void ClipboardEvent::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(ClipboardEvent); +} + +void ClipboardEvent::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_clipboard_data); +} + +} diff --git a/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.h b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.h new file mode 100644 index 00000000000..ca6abc9b787 --- /dev/null +++ b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024, Tim Flynn + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::Clipboard { + +struct ClipboardEventInit : public DOM::EventInit { + JS::GCPtr clipboard_data; +}; + +// https://w3c.github.io/clipboard-apis/#clipboardevent +class ClipboardEvent : public DOM::Event { + WEB_PLATFORM_OBJECT(ClipboardEvent, DOM::Event); + JS_DECLARE_ALLOCATOR(ClipboardEvent); + +public: + static JS::NonnullGCPtr construct_impl(JS::Realm&, FlyString const& event_name, ClipboardEventInit const& event_init); + + virtual ~ClipboardEvent() override; + + JS::GCPtr clipboard_data() { return m_clipboard_data; } + +private: + ClipboardEvent(JS::Realm&, FlyString const& event_name, ClipboardEventInit const& event_init); + + virtual void initialize(JS::Realm&) override; + virtual void visit_edges(JS::Cell::Visitor&) override; + + JS::GCPtr m_clipboard_data; +}; + +} diff --git a/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.idl b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.idl new file mode 100644 index 00000000000..44390df547b --- /dev/null +++ b/Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.idl @@ -0,0 +1,13 @@ +#import +#import + +dictionary ClipboardEventInit : EventInit { + DataTransfer? clipboardData = null; +}; + +// https://w3c.github.io/clipboard-apis/#clipboardevent +[Exposed=Window] +interface ClipboardEvent : Event { + constructor(DOMString type, optional ClipboardEventInit eventInitDict = {}); + readonly attribute DataTransfer? clipboardData; +}; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index d830aa8f319..8ee5bf58ec2 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -8,6 +8,7 @@ libweb_js_bindings(Animations/AnimationTimeline) libweb_js_bindings(Animations/DocumentTimeline) libweb_js_bindings(Animations/KeyframeEffect) libweb_js_bindings(Clipboard/Clipboard) +libweb_js_bindings(Clipboard/ClipboardEvent) libweb_js_bindings(Crypto/Crypto) libweb_js_bindings(Crypto/CryptoKey) libweb_js_bindings(Crypto/SubtleCrypto)