CustomEvent.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. 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. 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. set_prototype(&Bindings::cached_web_prototype(realm, "CustomEvent"));
  24. }
  25. CustomEvent::~CustomEvent() = default;
  26. void CustomEvent::visit_edges(JS::Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(m_detail);
  30. }
  31. // https://dom.spec.whatwg.org/#dom-customevent-initcustomevent
  32. void CustomEvent::init_custom_event(String const& type, bool bubbles, bool cancelable, JS::Value detail)
  33. {
  34. // 1. If this’s dispatch flag is set, then return.
  35. if (dispatched())
  36. return;
  37. // 2. Initialize this with type, bubbles, and cancelable.
  38. initialize_event(type, bubbles, cancelable);
  39. // 3. Set this’s detail attribute to detail.
  40. m_detail = detail;
  41. }
  42. }