MediaQueryList.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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, String 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. // TODO: Replace this with a "media query list" and serialize on demand
  26. return m_media;
  27. }
  28. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
  29. bool MediaQueryList::matches() const
  30. {
  31. // TODO: Implement me :^)
  32. return false;
  33. }
  34. JS::Object* MediaQueryList::create_wrapper(JS::GlobalObject& global_object)
  35. {
  36. return wrap(global_object, *this);
  37. }
  38. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  39. void MediaQueryList::add_listener(RefPtr<DOM::EventListener> listener)
  40. {
  41. // 1. If listener is null, terminate these steps.
  42. if (!listener)
  43. return;
  44. // 2. Append an event listener to the associated list of event listeners with type set to change,
  45. // callback set to listener, and capture set to false, unless there already is an event listener
  46. // in that list with the same type, callback, and capture.
  47. // (NOTE: capture is set to false by default)
  48. add_event_listener(HTML::EventNames::change, listener);
  49. }
  50. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  51. void MediaQueryList::remove_listener(RefPtr<DOM::EventListener> listener)
  52. {
  53. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  54. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  55. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  56. remove_event_listener(HTML::EventNames::change, listener);
  57. }
  58. void MediaQueryList::set_onchange(HTML::EventHandler event_handler)
  59. {
  60. set_event_handler_attribute(HTML::EventNames::change, event_handler);
  61. }
  62. HTML::EventHandler MediaQueryList::onchange()
  63. {
  64. return event_handler_attribute(HTML::EventNames::change);
  65. }
  66. }