diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index e0025e4ebd2..f8a167d4fec 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -333,6 +333,7 @@ set(SOURCES HTML/CloseEvent.cpp HTML/CloseWatcher.cpp HTML/CloseWatcherManager.cpp + HTML/CommandEvent.cpp HTML/CORSSettingAttribute.cpp HTML/CrossOrigin/AbstractOperations.cpp HTML/CrossOrigin/Reporting.cpp diff --git a/Libraries/LibWeb/HTML/CommandEvent.cpp b/Libraries/LibWeb/HTML/CommandEvent.cpp new file mode 100644 index 00000000000..b7c14ef436f --- /dev/null +++ b/Libraries/LibWeb/HTML/CommandEvent.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2025, Glenn Skrzypczak + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include + +namespace Web::HTML { + +GC_DEFINE_ALLOCATOR(CommandEvent); + +GC::Ref CommandEvent::create(JS::Realm& realm, FlyString const& event_name, CommandEventInit event_init) +{ + return realm.create(realm, event_name, move(event_init)); +} + +WebIDL::ExceptionOr> CommandEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, CommandEventInit event_init) +{ + return create(realm, event_name, move(event_init)); +} + +CommandEvent::CommandEvent(JS::Realm& realm, FlyString const& event_name, CommandEventInit event_init) + : DOM::Event(realm, event_name, event_init) + , m_source(event_init.source) + , m_command(move(event_init.command)) +{ +} + +void CommandEvent::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_source); +} + +void CommandEvent::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(CommandEvent); +} + +} diff --git a/Libraries/LibWeb/HTML/CommandEvent.h b/Libraries/LibWeb/HTML/CommandEvent.h new file mode 100644 index 00000000000..03f641976f3 --- /dev/null +++ b/Libraries/LibWeb/HTML/CommandEvent.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025, Glenn Skrzypczak + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include +#include + +namespace Web::HTML { + +struct CommandEventInit : public DOM::EventInit { + GC::Ptr source; + String command; +}; + +class CommandEvent : public DOM::Event { + WEB_PLATFORM_OBJECT(CommandEvent, DOM::Event); + GC_DECLARE_ALLOCATOR(CommandEvent); + +public: + [[nodiscard]] static GC::Ref create(JS::Realm&, FlyString const& event_name, CommandEventInit = {}); + static WebIDL::ExceptionOr> construct_impl(JS::Realm&, FlyString const& event_name, CommandEventInit); + + // https://html.spec.whatwg.org/multipage/interaction.html#dom-commandevent-command + String const& command() const { return m_command; } + + // https://html.spec.whatwg.org/multipage/interaction.html#dom-commandevent-source + GC::Ptr source() const { return as(retarget(m_source, current_target())); } + +private: + void visit_edges(Visitor&) override; + + CommandEvent(JS::Realm&, FlyString const& event_name, CommandEventInit event_init); + + void initialize(JS::Realm&) override; + + GC::Ptr m_source; + String m_command; +}; + +} diff --git a/Libraries/LibWeb/HTML/CommandEvent.idl b/Libraries/LibWeb/HTML/CommandEvent.idl new file mode 100644 index 00000000000..60424ba2016 --- /dev/null +++ b/Libraries/LibWeb/HTML/CommandEvent.idl @@ -0,0 +1,14 @@ +#import + +// https://html.spec.whatwg.org/multipage/interaction.html#commandevent +[Exposed=Window] +interface CommandEvent : Event { + constructor(DOMString type, optional CommandEventInit eventInitDict = {}); + readonly attribute Element? source; + readonly attribute DOMString command; +}; + +dictionary CommandEventInit : EventInit { + Element? source = null; + DOMString command = ""; +}; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 64e15e52480..58567b80bae 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -119,6 +119,7 @@ libweb_js_bindings(HTML/CanvasPattern) libweb_js_bindings(HTML/CanvasRenderingContext2D) libweb_js_bindings(HTML/CloseEvent) libweb_js_bindings(HTML/CloseWatcher) +libweb_js_bindings(HTML/CommandEvent) libweb_js_bindings(HTML/CustomElements/CustomElementRegistry) libweb_js_bindings(HTML/DataTransfer) libweb_js_bindings(HTML/DataTransferItem) diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 8ab200c6e33..c89a3121fb3 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -69,6 +69,7 @@ ClipboardEvent ClipboardItem CloseEvent CloseWatcher +CommandEvent Comment CompositionEvent CompressionStream diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.txt new file mode 100644 index 00000000000..80afdaaa11f --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.txt @@ -0,0 +1,27 @@ +Harness status: OK + +Found 22 tests + +22 Pass +Pass command is a readonly defaulting to '' +Pass source is readonly defaulting to null +Pass command reflects initialized attribute +Pass command set to undefined +Pass command set to null +Pass command set to false +Pass command explicitly set to empty string +Pass command set to true +Pass command set to a number +Pass command set to [] +Pass command set to [1, 2, 3] +Pass command set to an object +Pass command set to an object with a toString function +Pass CommandEventInit properties set value +Pass CommandEventInit properties set value 2 +Pass CommandEventInit properties set value 3 +Pass source set to undefined +Pass source set to null +Pass source set to false +Pass source set to true +Pass source set to {} +Pass source set to non-Element EventTarget \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.html new file mode 100644 index 00000000000..3c857014f94 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/the-button-element/command-and-commandfor/event-interface.html @@ -0,0 +1,163 @@ + + + + + + + + +
+ + +