Ver Fonte

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. :^)
Andreas Kling há 3 anos atrás
pai
commit
d69133e4ac

+ 21 - 0
Userland/Libraries/LibWeb/Bindings/WindowObject.cpp

@@ -11,7 +11,9 @@
 #include <LibJS/Runtime/FunctionObject.h>
 #include <LibJS/Runtime/Shape.h>
 #include <LibTextCodec/Decoder.h>
+#include <LibWeb/Bindings/CSSStyleDeclarationWrapper.h>
 #include <LibWeb/Bindings/DocumentWrapper.h>
+#include <LibWeb/Bindings/ElementWrapper.h>
 #include <LibWeb/Bindings/EventTargetConstructor.h>
 #include <LibWeb/Bindings/EventTargetPrototype.h>
 #include <LibWeb/Bindings/EventWrapper.h>
@@ -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<ElementWrapper>(object)) {
+        vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "DOM element");
+        return {};
+    }
+
+    return wrap(global_object, impl->get_computed_style(static_cast<ElementWrapper*>(object)->impl()));
+}
+
 }

+ 2 - 0
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<DOM::Window> m_impl;
 
     HashMap<String, JS::Object*> m_prototypes;

+ 8 - 0
Userland/Libraries/LibWeb/DOM/Window.cpp

@@ -187,4 +187,12 @@ Page const* Window::page() const
     return associated_document().page();
 }
 
+NonnullRefPtr<CSS::CSSStyleDeclaration> Window::get_computed_style(DOM::Element& element) const
+{
+    dbgln("Generating CSS computed style for {} @ {:p}", element.node_name(), &element);
+    Vector<CSS::StyleProperty> properties;
+    HashMap<String, CSS::StyleProperty> custom_properties;
+    return CSS::CSSStyleDeclaration::create(move(properties), move(custom_properties));
+}
+
 }

+ 2 - 0
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<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
+
 private:
     explicit Window(Document&);