MediaQueryList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/Bindings/MediaQueryListPrototype.h>
  9. #include <LibWeb/CSS/MediaQueryList.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/EventDispatcher.h>
  12. #include <LibWeb/DOM/IDLEventListener.h>
  13. #include <LibWeb/HTML/EventHandler.h>
  14. namespace Web::CSS {
  15. JS_DEFINE_ALLOCATOR(MediaQueryList);
  16. JS::NonnullGCPtr<MediaQueryList> MediaQueryList::create(DOM::Document& document, Vector<NonnullRefPtr<MediaQuery>>&& media)
  17. {
  18. return document.heap().allocate<MediaQueryList>(document.realm(), document, move(media));
  19. }
  20. MediaQueryList::MediaQueryList(DOM::Document& document, Vector<NonnullRefPtr<MediaQuery>>&& media)
  21. : DOM::EventTarget(document.realm())
  22. , m_document(document)
  23. , m_media(move(media))
  24. {
  25. evaluate();
  26. }
  27. void MediaQueryList::initialize(JS::Realm& realm)
  28. {
  29. Base::initialize(realm);
  30. WEB_SET_PROTOTYPE_FOR_INTERFACE(MediaQueryList);
  31. }
  32. void MediaQueryList::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_document);
  36. }
  37. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
  38. String MediaQueryList::media() const
  39. {
  40. return serialize_a_media_query_list(m_media);
  41. }
  42. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
  43. bool MediaQueryList::matches() const
  44. {
  45. for (auto& media : m_media) {
  46. if (media->matches())
  47. return true;
  48. }
  49. return false;
  50. }
  51. bool MediaQueryList::evaluate()
  52. {
  53. auto window = m_document->window();
  54. if (!window)
  55. return false;
  56. bool now_matches = false;
  57. for (auto& media : m_media) {
  58. now_matches = now_matches || media->evaluate(*window);
  59. }
  60. return now_matches;
  61. }
  62. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  63. void MediaQueryList::add_listener(DOM::IDLEventListener* listener)
  64. {
  65. // 1. If listener is null, terminate these steps.
  66. if (!listener)
  67. return;
  68. // 2. Append an event listener to the associated list of event listeners with type set to change,
  69. // callback set to listener, and capture set to false, unless there already is an event listener
  70. // in that list with the same type, callback, and capture.
  71. // (NOTE: capture is set to false by default)
  72. add_event_listener_without_options(HTML::EventNames::change, *listener);
  73. }
  74. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  75. void MediaQueryList::remove_listener(DOM::IDLEventListener* listener)
  76. {
  77. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  78. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  79. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  80. remove_event_listener_without_options(HTML::EventNames::change, *listener);
  81. }
  82. void MediaQueryList::set_onchange(WebIDL::CallbackType* event_handler)
  83. {
  84. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  85. }
  86. WebIDL::CallbackType* MediaQueryList::onchange()
  87. {
  88. return event_handler_attribute(HTML::EventNames::change);
  89. }
  90. }