LibWeb: Add the missing CloseEvent IDL constructor

This commit is contained in:
Idan Horowitz 2021-10-01 18:16:12 +03:00 committed by Andreas Kling
parent 7f551d7f6a
commit ded8e84f32
Notes: sideshowbarker 2024-07-18 03:14:29 +09:00
3 changed files with 32 additions and 10 deletions

View File

@ -10,27 +10,37 @@
namespace Web::HTML {
struct CloseEventInit : public DOM::EventInit {
bool was_clean { false };
u16 code { 0 };
String reason { "" };
};
class CloseEvent : public DOM::Event {
public:
using WrapperType = Bindings::CloseEventWrapper;
static NonnullRefPtr<CloseEvent> create(const FlyString& event_name, bool was_clean, u16 code, const String& reason)
static NonnullRefPtr<CloseEvent> create(FlyString const& event_name, CloseEventInit const& event_init = {})
{
return adopt_ref(*new CloseEvent(event_name, was_clean, code, reason));
return adopt_ref(*new CloseEvent(event_name, event_init));
}
static NonnullRefPtr<CloseEvent> create_with_global_object(Bindings::WindowObject&, FlyString const& event_name, CloseEventInit const& event_init)
{
return CloseEvent::create(event_name, event_init);
}
virtual ~CloseEvent() override = default;
bool was_clean() { return m_was_clean; }
bool was_clean() const { return m_was_clean; }
u16 code() const { return m_code; }
String reason() const { return m_reason; }
protected:
CloseEvent(const FlyString& event_name, bool was_clean, u16 code, const String& reason)
: Event(event_name)
, m_was_clean(was_clean)
, m_code(code)
, m_reason(reason)
CloseEvent(FlyString const& event_name, CloseEventInit const& event_init)
: Event(event_name, event_init)
, m_was_clean(event_init.was_clean)
, m_code(event_init.code)
, m_reason(event_init.reason)
{
}

View File

@ -1,7 +1,15 @@
#import <DOM/Event.idl>
interface CloseEvent : Event {
constructor(DOMString type, optional CloseEventInit eventInitDict = {});
readonly attribute boolean wasClean;
readonly attribute unsigned short code;
readonly attribute USVString reason;
};
dictionary CloseEventInit : EventInit {
boolean wasClean = false;
unsigned short code = 0;
USVString reason = "";
};

View File

@ -189,7 +189,11 @@ void WebSocket::on_close(u16 code, String reason, bool was_clean)
{
// 1. Change the readyState attribute's value to CLOSED. This is handled by the Protocol's WebSocket
// 2. If [needed], fire an event named error at the WebSocket object. This is handled by the Protocol's WebSocket
dispatch_event(CloseEvent::create(EventNames::close, was_clean, code, reason));
CloseEventInit event_init {};
event_init.was_clean = was_clean;
event_init.code = code;
event_init.reason = move(reason);
dispatch_event(CloseEvent::create(EventNames::close, event_init));
}
// https://html.spec.whatwg.org/multipage/web-sockets.html#feedback-from-the-protocol