From d69133e4acecae9faef1b803685f19a6ec4cb29f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 11 Sep 2021 00:33:30 +0200 Subject: [PATCH] LibWeb: Stub out a dummy window.getComputedStyle() This just returns an empty CSSStyleDeclaration for now. The real thing needs to be a live object that provides a view onto the computed style of a given element. This is far from that, but it's something. :^) --- .../LibWeb/Bindings/WindowObject.cpp | 21 +++++++++++++++++++ .../Libraries/LibWeb/Bindings/WindowObject.h | 2 ++ Userland/Libraries/LibWeb/DOM/Window.cpp | 8 +++++++ Userland/Libraries/LibWeb/DOM/Window.h | 2 ++ 4 files changed, 33 insertions(+) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index c0cdd3234b..e643a9b0e0 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -11,7 +11,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -70,6 +72,8 @@ void WindowObject::initialize_global_object() define_native_function("atob", atob, 1, attr); define_native_function("btoa", btoa, 1, attr); + define_native_function("getComputedStyle", get_computed_style, 1, attr); + // Legacy define_native_accessor("event", event_getter, {}, JS::Attribute::Enumerable); @@ -437,4 +441,21 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::inner_height_getter) return JS::Value(impl->inner_height()); } +JS_DEFINE_NATIVE_FUNCTION(WindowObject::get_computed_style) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + auto* object = vm.argument(0).to_object(global_object); + if (vm.exception()) + return {}; + + if (!is(object)) { + vm.throw_exception(global_object, JS::ErrorType::NotA, "DOM element"); + return {}; + } + + return wrap(global_object, impl->get_computed_style(static_cast(object)->impl())); +} + } diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index 36d88aff8f..c2e7ead848 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -84,6 +84,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(atob); JS_DECLARE_NATIVE_FUNCTION(btoa); + JS_DECLARE_NATIVE_FUNCTION(get_computed_style); + NonnullRefPtr m_impl; HashMap m_prototypes; diff --git a/Userland/Libraries/LibWeb/DOM/Window.cpp b/Userland/Libraries/LibWeb/DOM/Window.cpp index a787da2e1c..80ebd93750 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.cpp +++ b/Userland/Libraries/LibWeb/DOM/Window.cpp @@ -187,4 +187,12 @@ Page const* Window::page() const return associated_document().page(); } +NonnullRefPtr Window::get_computed_style(DOM::Element& element) const +{ + dbgln("Generating CSS computed style for {} @ {:p}", element.node_name(), &element); + Vector properties; + HashMap custom_properties; + return CSS::CSSStyleDeclaration::create(move(properties), move(custom_properties)); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Window.h b/Userland/Libraries/LibWeb/DOM/Window.h index 028651ba26..7ad219c151 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.h +++ b/Userland/Libraries/LibWeb/DOM/Window.h @@ -72,6 +72,8 @@ public: Event const* current_event() const { return m_current_event; } void set_current_event(Event* event) { m_current_event = event; } + NonnullRefPtr get_computed_style(DOM::Element&) const; + private: explicit Window(Document&);