MediaQueryList.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2021, 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/MediaQueryListWrapper.h>
  8. #include <LibWeb/CSS/MediaQueryList.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/EventDispatcher.h>
  11. #include <LibWeb/DOM/EventListener.h>
  12. #include <LibWeb/HTML/EventHandler.h>
  13. namespace Web::CSS {
  14. MediaQueryList::MediaQueryList(DOM::Document& document, NonnullRefPtrVector<MediaQuery>&& media)
  15. : DOM::EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  16. , m_document(document)
  17. , m_media(move(media))
  18. {
  19. evaluate();
  20. }
  21. MediaQueryList::~MediaQueryList()
  22. {
  23. }
  24. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
  25. String MediaQueryList::media() const
  26. {
  27. return serialize_a_media_query_list(m_media);
  28. }
  29. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
  30. bool MediaQueryList::matches() const
  31. {
  32. for (auto& media : m_media) {
  33. if (media.matches())
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool MediaQueryList::evaluate()
  39. {
  40. if (!m_document)
  41. return false;
  42. bool now_matches = false;
  43. for (auto& media : m_media) {
  44. now_matches = now_matches || media.evaluate(m_document->window());
  45. }
  46. return now_matches;
  47. }
  48. JS::Object* MediaQueryList::create_wrapper(JS::GlobalObject& global_object)
  49. {
  50. return wrap(global_object, *this);
  51. }
  52. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  53. void MediaQueryList::add_listener(RefPtr<DOM::EventListener> listener)
  54. {
  55. // 1. If listener is null, terminate these steps.
  56. if (!listener)
  57. return;
  58. // 2. Append an event listener to the associated list of event listeners with type set to change,
  59. // callback set to listener, and capture set to false, unless there already is an event listener
  60. // in that list with the same type, callback, and capture.
  61. // (NOTE: capture is set to false by default)
  62. add_event_listener(HTML::EventNames::change, listener);
  63. }
  64. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  65. void MediaQueryList::remove_listener(RefPtr<DOM::EventListener> listener)
  66. {
  67. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  68. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  69. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  70. remove_event_listener(HTML::EventNames::change, listener);
  71. }
  72. void MediaQueryList::set_onchange(HTML::EventHandler event_handler)
  73. {
  74. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  75. }
  76. HTML::EventHandler MediaQueryList::onchange()
  77. {
  78. return event_handler_attribute(HTML::EventNames::change);
  79. }
  80. }