From 2f9321a0d43f3543a50aabb75bb5b6f601ac7b94 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 7 Apr 2021 11:19:51 +0200 Subject: [PATCH] LibWeb: Implement window.top This simply returns the main frame's window object. If accessed inside the main frame, it will return itself. --- .../Libraries/LibWeb/Bindings/WindowObject.cpp | 14 ++++++++++++++ Userland/Libraries/LibWeb/Bindings/WindowObject.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 6c2cdee4fa..063d9fa609 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include @@ -66,6 +67,7 @@ void WindowObject::initialize_global_object() define_property("window", this, JS::Attribute::Enumerable); define_property("frames", this, JS::Attribute::Enumerable); define_property("self", this, JS::Attribute::Enumerable); + define_native_property("top", top_getter, JS::Attribute::Enumerable); define_native_property("document", document_getter, nullptr, JS::Attribute::Enumerable); define_native_property("performance", performance_getter, nullptr, JS::Attribute::Enumerable); define_native_property("screen", screen_getter, nullptr, JS::Attribute::Enumerable); @@ -352,6 +354,18 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa) return JS::js_string(vm, move(encoded)); } +JS_DEFINE_NATIVE_GETTER(WindowObject::top_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + auto* this_frame = impl->document().frame(); + VERIFY(this_frame); + VERIFY(this_frame->main_frame().document()); + auto& top_window = this_frame->main_frame().document()->window(); + return top_window.wrapper(); +} + JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter) { auto* impl = impl_from(vm, global_object); diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index 7c2deee1cf..0332ca11b2 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -78,6 +78,8 @@ public: private: virtual void visit_edges(Visitor&) override; + JS_DECLARE_NATIVE_GETTER(top_getter); + JS_DECLARE_NATIVE_GETTER(document_getter); JS_DECLARE_NATIVE_GETTER(performance_getter);