MediaQueryList.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. bool now_matches = false;
  41. for (auto& media : m_media) {
  42. now_matches = now_matches || media.evaluate(m_document.window());
  43. }
  44. return now_matches;
  45. }
  46. JS::Object* MediaQueryList::create_wrapper(JS::GlobalObject& global_object)
  47. {
  48. return wrap(global_object, *this);
  49. }
  50. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  51. void MediaQueryList::add_listener(RefPtr<DOM::EventListener> listener)
  52. {
  53. // 1. If listener is null, terminate these steps.
  54. if (!listener)
  55. return;
  56. // 2. Append an event listener to the associated list of event listeners with type set to change,
  57. // callback set to listener, and capture set to false, unless there already is an event listener
  58. // in that list with the same type, callback, and capture.
  59. // (NOTE: capture is set to false by default)
  60. add_event_listener(HTML::EventNames::change, listener);
  61. }
  62. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  63. void MediaQueryList::remove_listener(RefPtr<DOM::EventListener> listener)
  64. {
  65. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  66. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  67. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  68. remove_event_listener(HTML::EventNames::change, listener);
  69. }
  70. void MediaQueryList::set_onchange(HTML::EventHandler event_handler)
  71. {
  72. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  73. }
  74. HTML::EventHandler MediaQueryList::onchange()
  75. {
  76. return event_handler_attribute(HTML::EventNames::change);
  77. }
  78. }