CustomEvent.cpp 1.8 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 <LibWeb/DOM/CustomEvent.h>
  8. #include <LibWeb/HTML/Window.h>
  9. namespace Web::DOM {
  10. CustomEvent* CustomEvent::create(HTML::Window& window_object, FlyString const& event_name, CustomEventInit const& event_init)
  11. {
  12. return window_object.heap().allocate<CustomEvent>(window_object.realm(), window_object, event_name, event_init);
  13. }
  14. CustomEvent* CustomEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, CustomEventInit const& event_init)
  15. {
  16. return create(window_object, event_name, event_init);
  17. }
  18. CustomEvent::CustomEvent(HTML::Window& window_object, FlyString const& event_name)
  19. : Event(window_object, event_name)
  20. {
  21. set_prototype(&window_object.cached_web_prototype("CustomEvent"));
  22. }
  23. CustomEvent::CustomEvent(HTML::Window& window_object, FlyString const& event_name, CustomEventInit const& event_init)
  24. : Event(window_object, event_name, event_init)
  25. , m_detail(event_init.detail)
  26. {
  27. set_prototype(&window_object.cached_web_prototype("CustomEvent"));
  28. }
  29. CustomEvent::~CustomEvent() = default;
  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. }