MediaQueryList.cpp 2.6 KB

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