diff --git a/Userland/Libraries/LibWeb/HTML/MessageEvent.cpp b/Userland/Libraries/LibWeb/HTML/MessageEvent.cpp index b877e111034..5b28be7570a 100644 --- a/Userland/Libraries/LibWeb/HTML/MessageEvent.cpp +++ b/Userland/Libraries/LibWeb/HTML/MessageEvent.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2024, Jamie Mansfield * * SPDX-License-Identifier: BSD-2-Clause */ @@ -75,4 +76,30 @@ JS::NonnullGCPtr MessageEvent::ports() const return *m_ports_array; } +// https://html.spec.whatwg.org/multipage/comms.html#dom-messageevent-initmessageevent +void MessageEvent::init_message_event(String const& type, bool bubbles, bool cancelable, JS::Value data, String const& origin, String const& last_event_id, Optional source, Vector> const& ports) +{ + // The initMessageEvent(type, bubbles, cancelable, data, origin, lastEventId, source, ports) method must initialize the event in a + // manner analogous to the similarly-named initEvent() method. + + // 1. If this’s dispatch flag is set, then return. + if (dispatched()) + return; + + // 2. Initialize this with type, bubbles, and cancelable. + initialize_event(type, bubbles, cancelable); + + // Implementation Defined: Initialise other values. + m_data = data; + m_origin = origin; + m_last_event_id = last_event_id; + m_source = source; + m_ports.clear(); + m_ports.ensure_capacity(ports.size()); + for (auto const& port : ports) { + VERIFY(port); + m_ports.unchecked_append(static_cast(*port)); + } +} + } diff --git a/Userland/Libraries/LibWeb/HTML/MessageEvent.h b/Userland/Libraries/LibWeb/HTML/MessageEvent.h index 032ddbd2097..810c1b42774 100644 --- a/Userland/Libraries/LibWeb/HTML/MessageEvent.h +++ b/Userland/Libraries/LibWeb/HTML/MessageEvent.h @@ -40,6 +40,8 @@ public: JS::NonnullGCPtr ports() const; Variant, JS::Handle, Empty> source() const; + void init_message_event(String const& type, bool bubbles, bool cancelable, JS::Value data, String const& origin, String const& last_event_id, Optional source, Vector> const& ports); + private: virtual void initialize(JS::Realm&) override; virtual void visit_edges(Cell::Visitor&) override; diff --git a/Userland/Libraries/LibWeb/HTML/MessageEvent.idl b/Userland/Libraries/LibWeb/HTML/MessageEvent.idl index ef13fc735c3..a85cb1e3eb1 100644 --- a/Userland/Libraries/LibWeb/HTML/MessageEvent.idl +++ b/Userland/Libraries/LibWeb/HTML/MessageEvent.idl @@ -15,6 +15,8 @@ interface MessageEvent : Event { readonly attribute MessageEventSource? source; // FIXME: readonly attribute FrozenArray ports; readonly attribute any ports; + + undefined initMessageEvent(DOMString type, optional boolean bubbles = false, optional boolean cancelable = false, optional any data = null, optional USVString origin = "", optional DOMString lastEventId = "", optional MessageEventSource? source = null, optional sequence ports = []); }; dictionary MessageEventInit : EventInit {