MediaQueryList.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. namespace Web::CSS {
  12. MediaQueryList::MediaQueryList(DOM::Document& document, String media)
  13. : DOM::EventTarget(static_cast<Bindings::ScriptExecutionContext&>(document))
  14. , m_document(document)
  15. , m_media(move(media))
  16. {
  17. }
  18. MediaQueryList::~MediaQueryList()
  19. {
  20. }
  21. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
  22. String MediaQueryList::media() const
  23. {
  24. // TODO: Replace this with a "media query list" and serialize on demand
  25. return m_media;
  26. }
  27. // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
  28. bool MediaQueryList::matches() const
  29. {
  30. // TODO: Implement me :^)
  31. return false;
  32. }
  33. bool MediaQueryList::dispatch_event(NonnullRefPtr<DOM::Event> event)
  34. {
  35. return DOM::EventDispatcher::dispatch(*this, event);
  36. }
  37. JS::Object* MediaQueryList::create_wrapper(JS::GlobalObject& global_object)
  38. {
  39. return wrap(global_object, *this);
  40. }
  41. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
  42. void MediaQueryList::add_listener(RefPtr<DOM::EventListener> listener)
  43. {
  44. // 1. If listener is null, terminate these steps.
  45. if (!listener)
  46. return;
  47. // 2. Append an event listener to the associated list of event listeners with type set to change,
  48. // callback set to listener, and capture set to false, unless there already is an event listener
  49. // in that list with the same type, callback, and capture.
  50. // (NOTE: capture is set to false by default)
  51. add_event_listener(HTML::EventNames::change, listener);
  52. }
  53. // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
  54. void MediaQueryList::remove_listener(RefPtr<DOM::EventListener> listener)
  55. {
  56. // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
  57. // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
  58. // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
  59. remove_event_listener(HTML::EventNames::change, listener);
  60. }
  61. }