CustomEvent.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/DOM/CustomEvent.h>
  10. namespace Web::DOM {
  11. WebIDL::ExceptionOr<JS::NonnullGCPtr<CustomEvent>> CustomEvent::create(JS::Realm& realm, FlyString const& event_name, CustomEventInit const& event_init)
  12. {
  13. return MUST_OR_THROW_OOM(realm.heap().allocate<CustomEvent>(realm, realm, event_name, event_init));
  14. }
  15. WebIDL::ExceptionOr<JS::NonnullGCPtr<CustomEvent>> CustomEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, CustomEventInit const& event_init)
  16. {
  17. return create(realm, event_name, event_init);
  18. }
  19. CustomEvent::CustomEvent(JS::Realm& realm, FlyString const& event_name, CustomEventInit const& event_init)
  20. : Event(realm, event_name, event_init)
  21. , m_detail(event_init.detail)
  22. {
  23. }
  24. CustomEvent::~CustomEvent() = default;
  25. JS::ThrowCompletionOr<void> CustomEvent::initialize(JS::Realm& realm)
  26. {
  27. MUST_OR_THROW_OOM(Base::initialize(realm));
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::CustomEventPrototype>(realm, "CustomEvent"));
  29. return {};
  30. }
  31. void CustomEvent::visit_edges(JS::Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_detail);
  35. }
  36. // https://dom.spec.whatwg.org/#dom-customevent-initcustomevent
  37. void CustomEvent::init_custom_event(String const& type, bool bubbles, bool cancelable, JS::Value detail)
  38. {
  39. // 1. If this’s dispatch flag is set, then return.
  40. if (dispatched())
  41. return;
  42. // 2. Initialize this with type, bubbles, and cancelable.
  43. initialize_event(type, bubbles, cancelable);
  44. // 3. Set this’s detail attribute to detail.
  45. m_detail = detail;
  46. }
  47. }