Quellcode durchsuchen

LibWeb/HTML: Port Window.matchMedia() to IDL

Linus Groh vor 2 Jahren
Ursprung
Commit
2beb99b76e

+ 12 - 15
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -628,13 +628,6 @@ CSS::CSSStyleDeclaration* Window::get_computed_style_impl(DOM::Element& element)
     return CSS::ResolvedCSSStyleDeclaration::create(element).release_value_but_fixme_should_propagate_errors().ptr();
 }
 
-JS::NonnullGCPtr<CSS::MediaQueryList> Window::match_media_impl(DeprecatedString media)
-{
-    auto media_query_list = CSS::MediaQueryList::create(associated_document(), parse_media_query_list(CSS::Parser::ParsingContext(associated_document()), media)).release_value_but_fixme_should_propagate_errors();
-    associated_document().add_media_query_list(*media_query_list);
-    return media_query_list;
-}
-
 Optional<CSS::MediaFeatureValue> Window::query_media_feature(CSS::MediaFeatureID media_feature) const
 {
     // FIXME: Many of these should be dependent on the hardware
@@ -1063,7 +1056,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
     define_native_function(realm, "cancelIdleCallback", cancel_idle_callback, 1, attr);
 
     define_native_function(realm, "getComputedStyle", get_computed_style, 1, attr);
-    define_native_function(realm, "matchMedia", match_media, 1, attr);
     define_native_function(realm, "getSelection", get_selection, 0, attr);
 
     define_native_function(realm, "structuredClone", structured_clone, 1, attr);
@@ -1330,6 +1322,18 @@ Variant<JS::Handle<DOM::Event>, JS::Value> Window::event() const
     return JS::js_undefined();
 }
 
+// https://w3c.github.io/csswg-drafts/cssom-view/#dom-window-matchmedia
+WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> Window::match_media(String const& query)
+{
+    // 1. Let parsed media query list be the result of parsing query.
+    auto parsed_media_query_list = parse_media_query_list(CSS::Parser::ParsingContext(associated_document()), 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 = MUST_OR_THROW_OOM(heap().allocate<CSS::MediaQueryList>(realm(), associated_document(), move(parsed_media_query_list)));
+    associated_document().add_media_query_list(media_query_list);
+    return media_query_list;
+}
+
 // https://w3c.github.io/hr-time/#dom-windoworworkerglobalscope-performance
 WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> Window::performance()
 {
@@ -1573,13 +1577,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::get_selection)
     return impl->associated_document().get_selection();
 }
 
-JS_DEFINE_NATIVE_FUNCTION(Window::match_media)
-{
-    auto* impl = TRY(impl_from(vm));
-    auto media = TRY(vm.argument(0).to_deprecated_string(vm));
-    return impl->match_media_impl(move(media));
-}
-
 // https://www.w3.org/TR/cssom-view/#dom-window-scrollx
 JS_DEFINE_NATIVE_FUNCTION(Window::scroll_x_getter)
 {

+ 2 - 2
Userland/Libraries/LibWeb/HTML/Window.h

@@ -103,7 +103,6 @@ public:
     void set_current_event(DOM::Event* event);
 
     CSS::CSSStyleDeclaration* get_computed_style_impl(DOM::Element&) const;
-    JS::NonnullGCPtr<CSS::MediaQueryList> match_media_impl(DeprecatedString);
     Optional<CSS::MediaFeatureValue> query_media_feature(CSS::MediaFeatureID) const;
 
     float scroll_x() const;
@@ -165,6 +164,8 @@ public:
 
     Variant<JS::Handle<DOM::Event>, JS::Value> event() const;
 
+    WebIDL::ExceptionOr<JS::NonnullGCPtr<CSS::MediaQueryList>> match_media(String const& query);
+
     WebIDL::ExceptionOr<JS::NonnullGCPtr<HighResolutionTime::Performance>> performance();
 
     WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto::Crypto>> crypto();
@@ -266,7 +267,6 @@ private:
     JS_DECLARE_NATIVE_FUNCTION(focus);
 
     JS_DECLARE_NATIVE_FUNCTION(get_computed_style);
-    JS_DECLARE_NATIVE_FUNCTION(match_media);
     JS_DECLARE_NATIVE_FUNCTION(get_selection);
 
     JS_DECLARE_NATIVE_FUNCTION(queue_microtask);

+ 4 - 0
Userland/Libraries/LibWeb/HTML/Window.idl

@@ -1,4 +1,5 @@
 #import <Crypto/Crypto.idl>
+#import <CSS/MediaQueryList.idl>
 #import <DOM/Document.idl>
 #import <DOM/EventHandler.idl>
 #import <DOM/EventTarget.idl>
@@ -42,6 +43,9 @@ interface Window : EventTarget {
     // https://dom.spec.whatwg.org/#interface-window-extensions
     [Replaceable] readonly attribute (Event or undefined) event; // legacy
 
+    // https://w3c.github.io/csswg-drafts/cssom-view/#extensions-to-the-window-interface
+    [NewObject] MediaQueryList matchMedia(CSSOMString query);
+
     // FIXME: Everything from here on should be shared through WindowOrWorkerGlobalScope
     // https://w3c.github.io/hr-time/#the-performance-attribute
     [Replaceable] readonly attribute Performance performance;