MediaQueryList.cpp 3.4 KB

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