diff options
author | Linus Groh <mail@linusgroh.de> | 2021-09-12 17:10:27 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-12 18:25:45 +0200 |
commit | 51da5d03da8e462ef91a331453c345c88f961726 (patch) | |
tree | 457817035c36f68d1ff28cc04b98bdf873866301 | |
parent | 4155cc7ed53b1339b772640eaf65f1386f8a7bda (diff) | |
download | serenity-51da5d03da8e462ef91a331453c345c88f961726.zip |
LibWeb: Implement window.matchMedia()
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/WindowObject.cpp | 13 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Bindings/WindowObject.h | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Window.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Window.h | 2 |
4 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 640024fff8..01e82d0d09 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -21,6 +21,7 @@ #include <LibWeb/Bindings/EventWrapperFactory.h> #include <LibWeb/Bindings/HistoryWrapper.h> #include <LibWeb/Bindings/LocationObject.h> +#include <LibWeb/Bindings/MediaQueryListWrapper.h> #include <LibWeb/Bindings/NavigatorObject.h> #include <LibWeb/Bindings/NodeWrapperFactory.h> #include <LibWeb/Bindings/PerformanceWrapper.h> @@ -78,6 +79,7 @@ void WindowObject::initialize_global_object() define_native_function("btoa", btoa, 1, attr); define_native_function("getComputedStyle", get_computed_style, 1, attr); + define_native_function("matchMedia", match_media, 1, attr); // FIXME: These properties should be [Replaceable] according to the spec, but [Writable+Configurable] is the closest we have. define_native_accessor("scrollX", scroll_x_getter, {}, attr); @@ -483,6 +485,17 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style) return wrap(global_object, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl())); } +JS_DEFINE_NATIVE_FUNCTION(WindowObject::match_media) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + auto media = vm.argument(0).to_string(global_object); + if (vm.exception()) + return {}; + return wrap(global_object, impl->match_media(move(media))); +} + // https://www.w3.org/TR/cssom-view/#dom-window-scrollx JS_DEFINE_NATIVE_GETTER(WindowObject::scroll_x_getter) { diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index 28a0a28980..213183cba6 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -96,6 +96,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(btoa); JS_DECLARE_NATIVE_FUNCTION(get_computed_style); + JS_DECLARE_NATIVE_FUNCTION(match_media); NonnullRefPtr<DOM::Window> m_impl; diff --git a/Userland/Libraries/LibWeb/DOM/Window.cpp b/Userland/Libraries/LibWeb/DOM/Window.cpp index 80ebd93750..bf969d811b 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.cpp +++ b/Userland/Libraries/LibWeb/DOM/Window.cpp @@ -195,4 +195,9 @@ NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& return CSS::CSSStyleDeclaration::create(move(properties), move(custom_properties)); } +NonnullRefPtr<CSS::MediaQueryList> Window::match_media(String media) +{ + return CSS::MediaQueryList::create(associated_document(), move(media)); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Window.h b/Userland/Libraries/LibWeb/DOM/Window.h index 7ad219c151..fb646f541c 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.h +++ b/Userland/Libraries/LibWeb/DOM/Window.h @@ -12,6 +12,7 @@ #include <AK/RefPtr.h> #include <LibWeb/Bindings/WindowObject.h> #include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/CSS/MediaQueryList.h> #include <LibWeb/CSS/Screen.h> #include <LibWeb/DOM/Event.h> #include <LibWeb/DOM/EventTarget.h> @@ -73,6 +74,7 @@ public: void set_current_event(Event* event) { m_current_event = event; } NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const; + NonnullRefPtr<CSS::MediaQueryList> match_media(String); private: explicit Window(Document&); |