MediaQueryList.cpp 3.3 KB

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