MediaQueryList.cpp 3.5 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. WebIDL::ExceptionOr<JS::NonnullGCPtr<MediaQueryList>> MediaQueryList::create(DOM::Document& document, Vector<NonnullRefPtr<MediaQuery>>&& media)
  16. {
  17. return MUST_OR_THROW_OOM(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. JS::ThrowCompletionOr<void> MediaQueryList::initialize(JS::Realm& realm)
  27. {
  28. MUST_OR_THROW_OOM(Base::initialize(realm));
  29. set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaQueryListPrototype>(realm, "MediaQueryList"));
  30. return {};
  31. }
  32. void MediaQueryList::visit_edges(Cell::Visitor& visitor)
  33. {
  34. Base::visit_edges(visitor);
  35. visitor.visit(m_document.ptr());
  36. }
  37. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
  38. DeprecatedString MediaQueryList::media() const
  39. {
  40. return serialize_a_media_query_list(m_media).release_value_but_fixme_should_propagate_errors().to_deprecated_string();
  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. }