From 9ec4defdd2452319ca2a633e56a72e5c836fe22a Mon Sep 17 00:00:00 2001 From: Luke Date: Sun, 11 Apr 2021 22:34:57 +0100 Subject: [PATCH] LibWeb: Add Window.parent and fix Window.top attributes This returns the parent frame of the current frame. If it's the main frame, it returns itself. Also fixes the attributes of Window.top, as they were accidentally being passed in as the setter. Required by Web Platform Tests. --- .../LibWeb/Bindings/WindowObject.cpp | 19 ++++++++++++++++++- .../Libraries/LibWeb/Bindings/WindowObject.h | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 063d9fa609..fffd3e9df7 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -67,7 +67,8 @@ 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("top", top_getter, nullptr, JS::Attribute::Enumerable); + define_native_property("parent", parent_getter, nullptr, 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); @@ -366,6 +367,22 @@ JS_DEFINE_NATIVE_GETTER(WindowObject::top_getter) return top_window.wrapper(); } +JS_DEFINE_NATIVE_GETTER(WindowObject::parent_getter) +{ + auto* impl = impl_from(vm, global_object); + if (!impl) + return {}; + auto* this_frame = impl->document().frame(); + VERIFY(this_frame); + if (this_frame->parent()) { + VERIFY(this_frame->parent()->document()); + auto& parent_window = this_frame->parent()->document()->window(); + return parent_window.wrapper(); + } + VERIFY(this_frame == &this_frame->main_frame()); + return impl->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 0332ca11b2..3d73cdf590 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -90,6 +90,8 @@ private: JS_DECLARE_NATIVE_GETTER(inner_width_getter); JS_DECLARE_NATIVE_GETTER(inner_height_getter); + JS_DECLARE_NATIVE_GETTER(parent_getter); + JS_DECLARE_NATIVE_FUNCTION(alert); JS_DECLARE_NATIVE_FUNCTION(confirm); JS_DECLARE_NATIVE_FUNCTION(prompt);