MediaQueryList.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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::NonnullGCPtr<MediaQueryList> MediaQueryList::create(DOM::Document& document, Vector<NonnullRefPtr<MediaQuery>>&& media)
  16. {
  17. return document.heap().allocate<MediaQueryList>(document.realm(), document, move(media));
  18. }
  19. MediaQueryList::MediaQueryList(DOM::Document& document, Vector<NonnullRefPtr<MediaQuery>>&& media)
  20. : DOM::EventTarget(document.realm())
  21. , m_document(document)
  22. , m_media(move(media))
  23. {
  24. evaluate();
  25. }
  26. void MediaQueryList::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaQueryListPrototype>(realm, "MediaQueryList"));
  30. }
  31. void MediaQueryList::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_document.ptr());
  35. }
  36. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
  37. DeprecatedString MediaQueryList::media() const
  38. {
  39. return serialize_a_media_query_list(m_media).to_deprecated_string();
  40. }
  41. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
  42. bool MediaQueryList::matches() const
  43. {
  44. for (auto& media : m_media) {
  45. if (media->matches())
  46. return true;
  47. }
  48. return false;
  49. }
  50. bool MediaQueryList::evaluate()
  51. {
  52. bool now_matches = false;
  53. for (auto& media : m_media) {
  54. now_matches = now_matches || media->evaluate(m_document->window());
  55. }
  56. return now_matches;
  57. }
  58. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  59. void MediaQueryList::add_listener(DOM::IDLEventListener* listener)
  60. {
  61. // 1. If listener is null, terminate these steps.
  62. if (!listener)
  63. return;
  64. // 2. Append an event listener to the associated list of event listeners with type set to change,
  65. // callback set to listener, and capture set to false, unless there already is an event listener
  66. // in that list with the same type, callback, and capture.
  67. // (NOTE: capture is set to false by default)
  68. add_event_listener_without_options(HTML::EventNames::change, *listener);
  69. }
  70. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  71. void MediaQueryList::remove_listener(DOM::IDLEventListener* listener)
  72. {
  73. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  74. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  75. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  76. remove_event_listener_without_options(HTML::EventNames::change, *listener);
  77. }
  78. void MediaQueryList::set_onchange(WebIDL::CallbackType* event_handler)
  79. {
  80. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  81. }
  82. WebIDL::CallbackType* MediaQueryList::onchange()
  83. {
  84. return event_handler_attribute(HTML::EventNames::change);
  85. }
  86. }