Skip to content

Truly independent web browser

LibWeb: Implement MediaQueryList in Rust

Andreas Kling97f8d934f008
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
index e3f22dcafbd..0fb64f231cc 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/event_target.rs
@@ -18,6 +18,7 @@ use liburl_rust::FfiOrigin;
use liburl_rust::origin::Origin;
use crate::bindings::CallContext;
+use crate::bindings::callback::CallbackInterface;
use crate::bindings::cpp_cell::CppCell;
use crate::bindings::interface::{Interface, Ref, Wrappable, cell_from_abi, cell_into_abi};
@@ -42,6 +43,16 @@ unsafe extern "C" {
r#type: ak::Span<'_, u8>,
callback: *mut c_void,
);
+ fn web_bindings_add_event_listener_without_options(
+ event_target: *mut c_void,
+ r#type: ak::Span<'_, u8>,
+ callback: *mut c_void,
+ );
+ fn web_bindings_remove_event_listener_without_options(
+ event_target: *mut c_void,
+ r#type: ak::Span<'_, u8>,
+ callback: *mut c_void,
+ );
fn web_bindings_create_event(
realm: *mut c_void,
r#type: ak::Span<'_, u8>,
@@ -165,6 +176,32 @@ impl CppEventTarget {
}
}
+ /// Adds an event listener for the event type whose callback is the EventListener, with no options,
+ /// https://dom.spec.whatwg.org/#add-an-event-listener
+ pub(crate) fn add_event_listener_without_options(&self, event_type: &str, callback: &CallbackInterface) {
+ // SAFETY: The EventTarget and the callback are live, and the type is lent for the call.
+ unsafe {
+ web_bindings_add_event_listener_without_options(
+ self.as_ptr(),
+ ak::Span::new(event_type.as_bytes()),
+ callback.as_ptr(),
+ );
+ }
+ }
+
+ /// Removes the event listener for the event type whose callback is the EventListener and whose capture is false,
+ /// https://dom.spec.whatwg.org/#remove-an-event-listener
+ pub(crate) fn remove_event_listener_without_options(&self, event_type: &str, callback: &CallbackInterface) {
+ // SAFETY: The EventTarget and the callback are live, and the type is lent for the call.
+ unsafe {
+ web_bindings_remove_event_listener_without_options(
+ self.as_ptr(),
+ ak::Span::new(event_type.as_bytes()),
+ callback.as_ptr(),
+ );
+ }
+ }
+
/// https://dom.spec.whatwg.org/#concept-event-dispatch
pub(crate) fn dispatch_event(&self, event: &CppEvent) -> bool {
// SAFETY: The EventTarget and the event are live.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs b/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
index e00d6660aa0..50740481416 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/exports.rs
@@ -57,7 +57,7 @@ use super::css_transform_value::CSSTransformValue;
use super::css_unit_value::CSSUnitValue;
use super::css_unparsed_value::CSSUnparsedValue;
use super::media_list::MediaList;
-use super::media_query_list_event::{MediaQueryListEvent, MediaQueryListEventInit};
+use super::media_query_list::MediaQueryList;
use super::style_sheet_list::StyleSheetList;
use super::transition_event::{TransitionEvent, TransitionEventInit};
use super::typed_om;
@@ -457,34 +457,43 @@ pub unsafe extern "C" fn web_css_keyword_value_value(value: *mut c_void) -> usiz
value.raw_value().into_raw()
}
-/// A new MediaQueryListEvent named `change`, as the Event that C++ dispatches, given a raw string that C++ hands over.
+/// A new MediaQueryList of the document, whose media query list shares the queries of the one that C++ passes.
///
/// # Safety
///
-/// `vm` must be the live VM, `realm` a live realm, and `media` a raw string that C++ hands over.
+/// `vm` must be the live VM, `realm` a live realm, and `document` a live document.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn web_media_query_list_event_create(
+pub unsafe extern "C" fn web_media_query_list_create(
vm: *mut c_void,
realm: *mut c_void,
- media: usize,
- matches: bool,
+ document: *mut c_void,
+ media: &NativeMediaList,
) -> *mut c_void {
- // SAFETY: The caller passes the live VM and a live realm.
- let (vm, realm) = unsafe { realm_arguments(vm, realm) };
- // SAFETY: The caller hands over the string.
- let media = unsafe { Utf16String::from_raw_owned(media) };
- let event_init = MediaQueryListEventInit {
- parent: EventInit {
- bubbles: false,
- cancelable: false,
- composed: false,
- },
- matches,
- media,
+ // SAFETY: The caller passes the live VM, a live realm and a live document.
+ let (vm, realm, document) = unsafe {
+ let (vm, realm) = realm_arguments(vm, realm);
+ (vm, realm, CppCell::from_raw(document))
};
- MediaQueryListEvent::create(vm, realm, &Utf16String::from_ascii(b"change"), event_init)
- .event()
- .as_ptr()
+ cell_into_abi(MediaQueryList::create(vm, realm, document, media.share()))
+}
+
+/// # Safety
+///
+/// `list` must be a live MediaQueryList.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_media_query_list_has_change_listener(list: *mut c_void) -> bool {
+ // SAFETY: The caller passes a live MediaQueryList.
+ unsafe { cell_from_abi::<MediaQueryList>(list) }.has_change_listener()
+}
+
+/// # Safety
+///
+/// `vm` must be the live VM, and `list` a live MediaQueryList.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn web_media_query_list_evaluate_and_report_change(vm: *mut c_void, list: *mut c_void) {
+ // SAFETY: The caller passes the live VM and a live MediaQueryList.
+ let (vm, list) = unsafe { (vm_from_abi(vm), cell_from_abi::<MediaQueryList>(list)) };
+ list.evaluate_and_report_change(vm);
}
/// The object of a wrapper that C++ passes, if it passes one.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/media_query_list.rs b/Libraries/LibWeb/Bindings/Rust/src/css/media_query_list.rs
new file mode 100644
index 00000000000..4376dd46384
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/media_query_list.rs
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2021-2022, Linus Groh <[email protected]>
+ * Copyright (c) 2021, Sam Atkins <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! MediaQueryList, https://drafts.csswg.org/cssom-view/#the-mediaquerylist-interface
+
+use core::cell::Cell;
+use core::ffi::c_void;
+use core::ptr::NonNull;
+
+use ak::Utf16String;
+use libjs_rust::api::{Gc, Realm, Trace, Vm};
+use libweb_rust::NativeMediaList;
+
+use crate::bindings::callback::CallbackInterface;
+use crate::bindings::cpp_cell::CppCell;
+use crate::bindings::event_target::{CppEventTarget, GetTheParent};
+use crate::bindings::interface::{Ref, Wrappable, allocate};
+use crate::bindings::{CallContext, WebIdlResult};
+use crate::css::media_query_list_event::{MediaQueryListEvent, MediaQueryListEventInit};
+use crate::dom::event::EventInit;
+
+unsafe extern "C" {
+ fn web_css_media_query_list_evaluate(document: *mut c_void, media: *const c_void) -> bool;
+ fn web_css_media_query_list_update_container_layout(document: *mut c_void) -> bool;
+}
+
+const CHANGE: &str = "change";
+
+// https://drafts.csswg.org/cssom-view/#the-mediaquerylist-interface
+#[derive(Trace)]
+#[gc(crate = libjs_rust)]
+pub(crate) struct MediaQueryList {
+ event_target: CppEventTarget,
+ realm: Gc<Realm>,
+
+ // A MediaQueryList object has an associated media query list and an associated document set on creation.
+ document: CppCell,
+ #[gc(untraced)]
+ media: NativeMediaList,
+
+ has_changed_state: Cell<bool>,
+}
+
+impl Wrappable for MediaQueryList {
+ fn relevant_realm(&self) -> Option<Gc<Realm>> {
+ Some(self.realm)
+ }
+
+ fn cpp_parent(&self) -> Option<NonNull<c_void>> {
+ self.event_target.as_cpp_parent()
+ }
+}
+
+impl GetTheParent for MediaQueryList {
+ fn get_the_parent(&self) -> Option<CppEventTarget> {
+ None
+ }
+}
+
+impl MediaQueryList {
+ pub(crate) fn create(vm: &Vm, realm: Gc<Realm>, document: CppCell, media: NativeMediaList) -> Ref<Self> {
+ let object = allocate(vm, |_| MediaQueryList {
+ event_target: CppEventTarget::new(),
+ realm,
+ document,
+ media,
+ has_changed_state: Cell::new(false),
+ });
+ object.event_target.set_owner(object);
+ object.evaluate();
+ object
+ }
+
+ fn evaluate(&self) -> bool {
+ // SAFETY: The document is live, and the media query list is lent for the call.
+ unsafe { web_css_media_query_list_evaluate(self.document.as_ptr(), (&raw const self.media).cast()) }
+ }
+
+ fn current_matches(&self) -> bool {
+ let did_match = self.media.matches();
+
+ // NB: If our document is inside a frame, we need to update layout since that may cause our frame (and thus
+ // viewport) to resize.
+ // SAFETY: The document is live.
+ if unsafe { web_css_media_query_list_update_container_layout(self.document.as_ptr()) } {
+ self.evaluate();
+ }
+
+ let now_matches = self.media.matches();
+ if did_match != now_matches {
+ self.has_changed_state.set(true);
+ }
+ now_matches
+ }
+
+ /// Whether the list has to outlive its last script reference, to receive change events.
+ pub(crate) fn has_change_listener(&self) -> bool {
+ self.event_target.has_event_listener(CHANGE)
+ }
+
+ // https://drafts.csswg.org/cssom-view/#evaluate-media-queries-and-report-changes
+ pub(crate) fn evaluate_and_report_change(&self, vm: &Vm) {
+ let did_match = self.current_matches();
+ let now_matches = self.evaluate();
+ let did_change_internally = self.has_changed_state.replace(false);
+
+ // 1. If target’s matches state has changed since the last time these steps were run, fire an event at target
+ // using the MediaQueryListEvent constructor, with its type attribute initialized to change, its isTrusted
+ // attribute initialized to true, its media attribute initialized to target’s media, and its matches
+ // attribute initialized to target’s matches state.
+ if did_change_internally || did_match != now_matches {
+ let event_init = MediaQueryListEventInit {
+ parent: EventInit {
+ bubbles: false,
+ cancelable: false,
+ composed: false,
+ },
+ matches: now_matches,
+ media: Utf16String::from_utf16(&self.media.media_text()),
+ };
+ let event =
+ MediaQueryListEvent::create(vm, self.realm, &Utf16String::from_ascii(CHANGE.as_bytes()), event_init);
+ event.event().set_is_trusted(true);
+ self.event_target.dispatch_event(event.event());
+ }
+ }
+
+ // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
+ pub(crate) fn media(&self, _: &mut CallContext) -> WebIdlResult<Utf16String> {
+ Ok(Utf16String::from_utf16(&self.media.media_text()))
+ }
+
+ // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
+ pub(crate) fn matches(&self, _: &mut CallContext) -> WebIdlResult<bool> {
+ Ok(self.current_matches())
+ }
+
+ // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
+ pub(crate) fn add_listener(&self, _: &mut CallContext, callback: Option<CallbackInterface>) -> WebIdlResult<()> {
+ // 1. If listener is null, terminate these steps.
+ let Some(callback) = callback else {
+ return Ok(());
+ };
+
+ // 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)
+ self.event_target.add_event_listener_without_options(CHANGE, &callback);
+ Ok(())
+ }
+
+ // https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
+ pub(crate) fn remove_listener(&self, _: &mut CallContext, callback: Option<CallbackInterface>) -> WebIdlResult<()> {
+ // 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.
+ if let Some(callback) = callback {
+ self.event_target
+ .remove_event_listener_without_options(CHANGE, &callback);
+ }
+ Ok(())
+ }
+}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
index 4d30802fd25..6995bf214f9 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/mod.rs
@@ -65,6 +65,7 @@ pub(crate) mod css_unparsed_value;
pub(crate) mod css_variable_reference_value;
pub(crate) mod exports;
pub(crate) mod media_list;
+pub(crate) mod media_query_list;
pub(crate) mod media_query_list_event;
pub(crate) mod style_sheet;
pub(crate) mod style_sheet_list;
diff --git a/Libraries/LibWeb/Bindings/RustEventTarget.cpp b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
index 7e57829312f..1610d7996c1 100644
--- a/Libraries/LibWeb/Bindings/RustEventTarget.cpp
+++ b/Libraries/LibWeb/Bindings/RustEventTarget.cpp
@@ -113,6 +113,8 @@ bool web_bindings_has_event_listener(DOM::EventTarget*, ReadonlyBytes type);
JSValue web_bindings_event_handler_attribute(RustEventTarget*, ReadonlyBytes name);
void web_bindings_set_event_handler_attribute(RustEventTarget*, ReadonlyBytes name, JSValue);
void web_bindings_add_event_listener(void* realm, DOM::EventTarget*, ReadonlyBytes type, JSObject* callback);
+void web_bindings_add_event_listener_without_options(DOM::EventTarget*, ReadonlyBytes type, WebIDL::CallbackType*);
+void web_bindings_remove_event_listener_without_options(DOM::EventTarget*, ReadonlyBytes type, WebIDL::CallbackType*);
DOM::Event* web_bindings_create_event(void* realm, ReadonlyBytes type, bool bubbles, bool cancelable, bool composed);
RustEvent* web_bindings_create_rust_event(void* realm, FlatPtr type, bool bubbles, bool cancelable, bool composed);
DOM::Event* web_bindings_create_message_event(void* realm, ReadonlyBytes type, JSValue data, JSObject* const* ports, size_t count);
@@ -173,6 +175,16 @@ void web_bindings_add_event_listener(void* realm, DOM::EventTarget* target, Read
target->add_event_listener_without_options(Utf16FlyString::from_utf8_without_validation(type), DOM::IDLEventListener::create(callback_type));
}
+void web_bindings_add_event_listener_without_options(DOM::EventTarget* target, ReadonlyBytes type, WebIDL::CallbackType* callback)
+{
+ target->add_event_listener_without_options(Utf16FlyString::from_utf8_without_validation(type), DOM::IDLEventListener::create(*callback));
+}
+
+void web_bindings_remove_event_listener_without_options(DOM::EventTarget* target, ReadonlyBytes type, WebIDL::CallbackType* callback)
+{
+ target->remove_event_listener_without_options(Utf16FlyString::from_utf8_without_validation(type), DOM::IDLEventListener::create(*callback));
+}
+
DOM::Event* web_bindings_create_event(void* realm, ReadonlyBytes type, bool bubbles, bool cancelable, bool composed)
{
auto& js_realm = *static_cast<JS::Realm*>(realm);
diff --git a/Libraries/LibWeb/CSS/MediaQueryList.cpp b/Libraries/LibWeb/CSS/MediaQueryList.cpp
index f6a25c2d442..b69a4864d1a 100644
--- a/Libraries/LibWeb/CSS/MediaQueryList.cpp
+++ b/Libraries/LibWeb/CSS/MediaQueryList.cpp
@@ -5,120 +5,59 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <LibGC/Heap.h>
#include <LibJS/HostClassBuilder.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/CSS/MediaQueryList.h>
#include <LibWeb/DOM/Document.h>
-#include <LibWeb/DOM/EventDispatcher.h>
-#include <LibWeb/DOM/IDLEventListener.h>
-#include <LibWeb/HTML/EventHandler.h>
-#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/Environments.h>
-#include <LibWeb/HTML/Window.h>
namespace Web::CSS {
extern "C" {
// Defined in Bindings/Rust/src/css/exports.rs.
-void* web_media_query_list_event_create(JSVM*, void* realm, FlatPtr media, bool matches);
-}
-
-GC_DEFINE_ALLOCATOR(MediaQueryList);
-
-GC::Ref<MediaQueryList> MediaQueryList::create(DOM::Document& document, RustMediaList media)
-{
- return GC::Heap::the().allocate<MediaQueryList>(document, move(media));
-}
-
-MediaQueryList::MediaQueryList(DOM::Document& document, RustMediaList media)
- : DOM::EventTarget()
- , m_document(document)
- , m_media(move(media))
-{
- evaluate();
-}
-
-GC::Ptr<Bindings::Wrappable> MediaQueryList::relevant_global_impl() const
-{
- return m_document->window();
-}
+void* web_media_query_list_create(JSVM*, void* realm, void* document, Parser::ValueParserFFI::MediaList const*);
+bool web_media_query_list_has_change_listener(void* list);
+void web_media_query_list_evaluate_and_report_change(JSVM*, void* list);
-void MediaQueryList::visit_edges(Cell::Visitor& visitor)
-{
- Base::visit_edges(visitor);
- visitor.visit(m_document);
+// Declared in Bindings/Rust/src/css/media_query_list.rs.
+bool web_css_media_query_list_evaluate(void* document, Parser::ValueParserFFI::MediaList const*);
+bool web_css_media_query_list_update_container_layout(void* document);
}
-// https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media
-Utf16String MediaQueryList::media() const
+static DOM::Document& document_from_abi(void* document)
{
- return m_media.media_text();
+ return *static_cast<DOM::Document*>(static_cast<GC::Cell*>(document));
}
-// https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches
-bool MediaQueryList::matches() const
+bool web_css_media_query_list_evaluate(void* document, Parser::ValueParserFFI::MediaList const* media)
{
- bool did_match = m_media.matches();
-
- // NOTE: If our document is inside a frame, we need to update layout
- // since that may cause our frame (and thus viewport) to resize.
- if (auto container_document = m_document->container_document()) {
- container_document->update_layout(DOM::UpdateLayoutReason::MediaQueryListMatches);
- m_media.evaluate(m_document);
- }
-
- bool now_matches = m_media.matches();
- if (did_match != now_matches)
- m_has_changed_state = true;
-
- return now_matches;
+ return RustMediaList { media, RustMediaList::Ownership::Borrow }.evaluate(document_from_abi(document));
}
-GC::Ref<DOM::Event> MediaQueryList::create_change_event(bool matches) const
+bool web_css_media_query_list_update_container_layout(void* document)
{
- auto* event = web_media_query_list_event_create(JS::HostABI::vm_to_abi(JS::VM::the()), &HTML::relevant_realm(*m_document), media().into_raw(), matches);
- return GC::Ref { *static_cast<DOM::Event*>(static_cast<GC::Cell*>(event)) };
-}
-
-bool MediaQueryList::evaluate()
-{
- return m_media.evaluate(m_document);
-}
-
-// https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-addlistener
-void MediaQueryList::add_listener(GC::Ptr<DOM::IDLEventListener> 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_without_options(HTML::EventNames::change, *listener);
+ auto container_document = document_from_abi(document).container_document();
+ if (!container_document)
+ return false;
+ container_document->update_layout(DOM::UpdateLayoutReason::MediaQueryListMatches);
+ return true;
}
-// https://www.w3.org/TR/cssom-view/#dom-mediaquerylist-removelistener
-void MediaQueryList::remove_listener(GC::Ptr<DOM::IDLEventListener> listener)
+GC::Ref<MediaQueryList> MediaQueryList::create(DOM::Document& document, RustMediaList const& media)
{
- // 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.
- if (listener)
- remove_event_listener_without_options(HTML::EventNames::change, *listener);
+ auto* list = web_media_query_list_create(JS::HostABI::vm_to_abi(JS::VM::the()), &HTML::relevant_realm(document), static_cast<GC::Cell*>(&document), media.handle());
+ return GC::Ref { *static_cast<MediaQueryList*>(list) };
}
-void MediaQueryList::set_onchange(WebIDL::CallbackType* event_handler)
+bool MediaQueryList::has_change_listener() const
{
- set_event_handler_attribute(HTML::EventNames::change, event_handler);
+ return web_media_query_list_has_change_listener(const_cast<MediaQueryList*>(this));
}
-WebIDL::CallbackType* MediaQueryList::onchange()
+void MediaQueryList::evaluate_and_report_change()
{
- return event_handler_attribute(HTML::EventNames::change);
+ web_media_query_list_evaluate_and_report_change(JS::HostABI::vm_to_abi(JS::VM::the()), this);
}
}
diff --git a/Libraries/LibWeb/CSS/MediaQueryList.h b/Libraries/LibWeb/CSS/MediaQueryList.h
index 47f9df7c312..f1fb916e5c3 100644
--- a/Libraries/LibWeb/CSS/MediaQueryList.h
+++ b/Libraries/LibWeb/CSS/MediaQueryList.h
@@ -6,48 +6,23 @@
#pragma once
-#include <AK/Forward.h>
+#include <LibGC/Ptr.h>
+#include <LibWeb/Bindings/MediaQueryList.h>
+#include <LibWeb/Bindings/RustInterface.h>
#include <LibWeb/CSS/RustMediaList.h>
-#include <LibWeb/DOM/EventTarget.h>
-#include <LibWeb/Export.h>
+#include <LibWeb/Forward.h>
namespace Web::CSS {
// 4.2. The MediaQueryList Interface, https://drafts.csswg.org/cssom-view/#the-mediaquerylist-interface
-class MediaQueryList final : public DOM::EventTarget {
- WEB_WRAPPABLE(MediaQueryList, DOM::EventTarget);
- GC_DECLARE_ALLOCATOR(MediaQueryList);
+class MediaQueryList final : public Bindings::RustInterfaceCell {
+ WEB_RUST_INTERFACE(MediaQueryList, media_query_list)
public:
- [[nodiscard]] static GC::Ref<MediaQueryList> create(DOM::Document&, RustMediaList);
+ [[nodiscard]] static GC::Ref<MediaQueryList> create(DOM::Document&, RustMediaList const&);
- virtual ~MediaQueryList() override = default;
-
- Utf16String media() const;
- bool matches() const;
- bool evaluate();
-
- // A new MediaQueryListEvent named `change` for this list, which is implemented in Rust.
- GC::Ref<DOM::Event> create_change_event(bool matches) const;
-
- void add_listener(GC::Ptr<DOM::IDLEventListener>);
- void remove_listener(GC::Ptr<DOM::IDLEventListener>);
-
- void set_onchange(WebIDL::CallbackType*);
- WebIDL::CallbackType* onchange();
-
- [[nodiscard]] Optional<bool> const& has_changed_state() const { return m_has_changed_state; }
- void set_has_changed_state(bool has_changed_state) { m_has_changed_state = has_changed_state; }
-
-private:
- MediaQueryList(DOM::Document&, RustMediaList);
- virtual void visit_edges(Cell::Visitor&) override;
- virtual GC::Ptr<Bindings::Wrappable> relevant_global_impl() const override;
-
- GC::Ref<DOM::Document> m_document;
- RustMediaList m_media;
-
- mutable Optional<bool> m_has_changed_state { false };
+ bool has_change_listener() const;
+ void evaluate_and_report_change();
};
}
diff --git a/Libraries/LibWeb/CSS/MediaQueryList.idl b/Libraries/LibWeb/CSS/MediaQueryList.idl
index 61f1e3dcbd2..6ceab50b401 100644
--- a/Libraries/LibWeb/CSS/MediaQueryList.idl
+++ b/Libraries/LibWeb/CSS/MediaQueryList.idl
@@ -1,5 +1,5 @@
// https://drafts.csswg.org/cssom-view/#the-mediaquerylist-interface
-[Exposed=Window]
+[Exposed=Window, ImplementedIn=Rust]
interface MediaQueryList : EventTarget {
readonly attribute CSSOMString media;
readonly attribute boolean matches;
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index ce40939fec3..d0e66b35649 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -1039,7 +1039,7 @@ void Document::visit_edges(Cell::Visitor& visitor)
visitor.visit(pending_scroll_event.event_target);
// A MediaQueryList with change listeners must outlive its last script reference to receive change events.
for (auto const& media_query_list : m_media_query_lists) {
- if (media_query_list && media_query_list->has_event_listener(HTML::EventNames::change))
+ if (media_query_list && media_query_list->has_change_listener())
visitor.visit(media_query_list.ptr());
}
m_scroll_state_query_containers.visit_edges(visitor);
@@ -5821,25 +5821,8 @@ void Document::evaluate_media_queries_and_report_changes()
// 1. For each MediaQueryList object target that has doc as its document,
// in the order they were created, oldest first, run these substeps:
for (auto& media_query_list_ptr : m_media_query_lists) {
- // 1. If target’s matches state has changed since the last time these steps
- // were run, fire an event at target using the MediaQueryListEvent constructor,
- // with its type attribute initialized to change, its isTrusted attribute
- // initialized to true, its media attribute initialized to target’s media,
- // and its matches attribute initialized to target’s matches state.
- if (!media_query_list_ptr)
- continue;
- GC::Ptr<CSS::MediaQueryList> media_query_list = media_query_list_ptr.ptr();
- bool did_match = media_query_list->matches();
- bool now_matches = media_query_list->evaluate();
-
- auto did_change_internally = media_query_list->has_changed_state();
- media_query_list->set_has_changed_state(false);
- if (did_change_internally == true || did_match != now_matches) {
- auto event = media_query_list->create_change_event(now_matches);
- event->set_time_stamp(HighResolutionTime::current_high_resolution_time(relevant_global_object(*this)));
- event->set_is_trusted(true);
- media_query_list->dispatch_event(*event);
- }
+ if (GC::Ptr<CSS::MediaQueryList> media_query_list = media_query_list_ptr.ptr())
+ media_query_list->evaluate_and_report_change();
}
}
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index 39d79f443ea..de2485a95ff 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -1842,7 +1842,7 @@ WebIDL::ExceptionOr<GC::Ref<CSS::MediaQueryList>> Window::match_media(Utf16View
auto parsed_media_query_list = CSS::RustMediaList::parse(query);
// 2. Return a new MediaQueryList object, with this's associated Document as the document, with parsed media query list as its associated media query list.
- auto media_query_list = CSS::MediaQueryList::create(associated_document(), move(parsed_media_query_list));
+ auto media_query_list = CSS::MediaQueryList::create(associated_document(), parsed_media_query_list);
associated_document().add_media_query_list(media_query_list);
return media_query_list;
}