summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-09-13 00:18:28 +0100
committerAndreas Kling <kling@serenityos.org>2021-09-13 01:43:03 +0200
commite946316618cfa2bf47d7f0285962562112bf5e07 (patch)
tree3f22c1fbc7d156b346fe3f81d1a7ac01b755fdd3 /Userland/Libraries/LibWeb
parent4f362fc70323169de234f556cfae81bbeb5a257c (diff)
downloadserenity-e946316618cfa2bf47d7f0285962562112bf5e07.zip
LibWeb: Implement MediaQueryList.{addListener,removeListener}
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp24
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaQueryList.h3
-rw-r--r--Userland/Libraries/LibWeb/CSS/MediaQueryList.idl4
3 files changed, 29 insertions, 2 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp b/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
index 43f5695d91..0af48b7676 100644
--- a/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
+++ b/Userland/Libraries/LibWeb/CSS/MediaQueryList.cpp
@@ -8,6 +8,7 @@
#include <LibWeb/CSS/MediaQueryList.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/EventDispatcher.h>
+#include <LibWeb/DOM/EventListener.h>
namespace Web::CSS {
@@ -46,4 +47,27 @@ JS::Object* MediaQueryList::create_wrapper(JS::GlobalObject& global_object)
return wrap(global_object, *this);
}
+// https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
+void MediaQueryList::add_listener(RefPtr<DOM::EventListener> listener)
+{
+ // 1. If listener is null, terminate these steps.
+ if (!listener)
+ return;
+
+ // 2. Append an event listener to the associated list of event listeners with type set to change,
+ // callback set to listener, and capture set to false, unless there already is an event listener
+ // in that list with the same type, callback, and capture.
+ // (NOTE: capture is set to false by default)
+ add_event_listener(HTML::EventNames::change, listener);
+}
+
+// https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
+void MediaQueryList::remove_listener(RefPtr<DOM::EventListener> listener)
+{
+ // 1. Remove an event listener from the associated list of event listeners, whose type is change, callback is listener, and capture is false.
+ // NOTE: While the spec doesn't technically use remove_event_listener and instead manipulates the list directly, every major engine uses remove_event_listener.
+ // This means if an event listener removes another event listener that comes after it, the removed event listener will not be invoked.
+ remove_event_listener(HTML::EventNames::change, listener);
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQueryList.h b/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
index 67aa0c4e6f..7d503a7d3b 100644
--- a/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
+++ b/Userland/Libraries/LibWeb/CSS/MediaQueryList.h
@@ -42,6 +42,9 @@ public:
virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
+ void add_listener(RefPtr<DOM::EventListener> listener);
+ void remove_listener(RefPtr<DOM::EventListener> listener);
+
private:
MediaQueryList(DOM::Document&, String);
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQueryList.idl b/Userland/Libraries/LibWeb/CSS/MediaQueryList.idl
index d714211554..d5cb27209a 100644
--- a/Userland/Libraries/LibWeb/CSS/MediaQueryList.idl
+++ b/Userland/Libraries/LibWeb/CSS/MediaQueryList.idl
@@ -3,7 +3,7 @@ interface MediaQueryList : EventTarget {
readonly attribute CSSOMString media;
readonly attribute boolean matches;
// TODO:
- // undefined addListener(EventListener? callback);
- // undefined removeListener(EventListener? callback);
+ undefined addListener(EventListener? callback);
+ undefined removeListener(EventListener? callback);
// attribute EventHandler onchange;
};