MediaQueryList.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (m_media.is_empty())
  46. return true;
  47. for (auto& media : m_media) {
  48. if (media->matches())
  49. return true;
  50. }
  51. return false;
  52. }
  53. bool MediaQueryList::evaluate()
  54. {
  55. auto window = m_document->window();
  56. if (!window)
  57. return false;
  58. if (m_media.is_empty())
  59. return true;
  60. bool now_matches = false;
  61. for (auto& media : m_media) {
  62. now_matches = now_matches || media->evaluate(*window);
  63. }
  64. return now_matches;
  65. }
  66. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  67. void MediaQueryList::add_listener(DOM::IDLEventListener* listener)
  68. {
  69. // 1. If listener is null, terminate these steps.
  70. if (!listener)
  71. return;
  72. // 2. Append an event listener to the associated list of event listeners with type set to change,
  73. // callback set to listener, and capture set to false, unless there already is an event listener
  74. // in that list with the same type, callback, and capture.
  75. // (NOTE: capture is set to false by default)
  76. add_event_listener_without_options(HTML::EventNames::change, *listener);
  77. }
  78. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  79. void MediaQueryList::remove_listener(DOM::IDLEventListener* listener)
  80. {
  81. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  82. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  83. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  84. remove_event_listener_without_options(HTML::EventNames::change, *listener);
  85. }
  86. void MediaQueryList::set_onchange(WebIDL::CallbackType* event_handler)
  87. {
  88. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  89. }
  90. WebIDL::CallbackType* MediaQueryList::onchange()
  91. {
  92. return event_handler_attribute(HTML::EventNames::change);
  93. }
  94. }