Serenity Operating System
1/*
2 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
3 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <LibJS/Runtime/Realm.h>
9#include <LibWeb/Bindings/Intrinsics.h>
10#include <LibWeb/DOM/CustomEvent.h>
11
12namespace Web::DOM {
13
14WebIDL::ExceptionOr<JS::NonnullGCPtr<CustomEvent>> CustomEvent::create(JS::Realm& realm, DeprecatedFlyString const& event_name, CustomEventInit const& event_init)
15{
16 return MUST_OR_THROW_OOM(realm.heap().allocate<CustomEvent>(realm, realm, event_name, event_init));
17}
18
19WebIDL::ExceptionOr<JS::NonnullGCPtr<CustomEvent>> CustomEvent::construct_impl(JS::Realm& realm, DeprecatedFlyString const& event_name, CustomEventInit const& event_init)
20{
21 return create(realm, event_name, event_init);
22}
23
24CustomEvent::CustomEvent(JS::Realm& realm, DeprecatedFlyString const& event_name, CustomEventInit const& event_init)
25 : Event(realm, event_name, event_init)
26 , m_detail(event_init.detail)
27{
28}
29
30CustomEvent::~CustomEvent() = default;
31
32JS::ThrowCompletionOr<void> CustomEvent::initialize(JS::Realm& realm)
33{
34 MUST_OR_THROW_OOM(Base::initialize(realm));
35 set_prototype(&Bindings::ensure_web_prototype<Bindings::CustomEventPrototype>(realm, "CustomEvent"));
36
37 return {};
38}
39
40void CustomEvent::visit_edges(JS::Cell::Visitor& visitor)
41{
42 Base::visit_edges(visitor);
43 visitor.visit(m_detail);
44}
45
46// https://dom.spec.whatwg.org/#dom-customevent-initcustomevent
47void CustomEvent::init_custom_event(DeprecatedString const& type, bool bubbles, bool cancelable, JS::Value detail)
48{
49 // 1. If this’s dispatch flag is set, then return.
50 if (dispatched())
51 return;
52
53 // 2. Initialize this with type, bubbles, and cancelable.
54 initialize_event(type, bubbles, cancelable);
55
56 // 3. Set this’s detail attribute to detail.
57 m_detail = detail;
58}
59
60}