CustomEvent.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. JS::NonnullGCPtr<CustomEvent> CustomEvent::create(JS::Realm& realm, FlyString const& event_name, CustomEventInit const& event_init)
  12. {
  13. return 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. void CustomEvent::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. set_prototype(&Bindings::ensure_web_prototype<Bindings::CustomEventPrototype>(realm, "CustomEvent"));
  29. }
  30. void CustomEvent::visit_edges(JS::Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_detail);
  34. }
  35. // https://dom.spec.whatwg.org/#dom-customevent-initcustomevent
  36. void CustomEvent::init_custom_event(String const& type, bool bubbles, bool cancelable, JS::Value detail)
  37. {
  38. // 1. If this’s dispatch flag is set, then return.
  39. if (dispatched())
  40. return;
  41. // 2. Initialize this with type, bubbles, and cancelable.
  42. initialize_event(type, bubbles, cancelable);
  43. // 3. Set this’s detail attribute to detail.
  44. m_detail = detail;
  45. }
  46. }