From 58522f5088e84779425bbb97c46e76594179ddd2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 20 Oct 2022 15:52:41 +0200 Subject: [PATCH] LibWeb: Make window.{window,self,frames} return the WindowProxy These now follow the spec and return the WindowProxy rather than the Window itself. --- Userland/Libraries/LibWeb/HTML/Window.cpp | 41 +++++++++++++++++++---- Userland/Libraries/LibWeb/HTML/Window.h | 4 +++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/Window.cpp b/Userland/Libraries/LibWeb/HTML/Window.cpp index f50d4cc934..7e1cf5b686 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.cpp +++ b/Userland/Libraries/LibWeb/HTML/Window.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -707,10 +708,10 @@ void Window::initialize_web_interfaces(Badge) m_crypto = Crypto::Crypto::create(realm); // FIXME: These should be native accessors, not properties - define_direct_property("window", this, JS::Attribute::Enumerable); - define_direct_property("frames", this, JS::Attribute::Enumerable); - define_direct_property("self", this, JS::Attribute::Enumerable); define_native_accessor(realm, "top", top_getter, nullptr, JS::Attribute::Enumerable); + define_native_accessor(realm, "window", window_getter, {}, JS::Attribute::Enumerable); + define_native_accessor(realm, "frames", frames_getter, {}, JS::Attribute::Enumerable); + define_native_accessor(realm, "self", self_getter, {}, JS::Attribute::Enumerable); define_native_accessor(realm, "parent", parent_getter, {}, JS::Attribute::Enumerable); define_native_accessor(realm, "document", document_getter, {}, JS::Attribute::Enumerable); define_native_accessor(realm, "frameElement", frame_element_getter, {}, JS::Attribute::Enumerable); @@ -817,9 +818,13 @@ static JS::ThrowCompletionOr impl_from(JS::VM& vm) auto* this_object = MUST(this_value.to_object(vm)); - if (!is(*this_object)) - return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "Window"); - return static_cast(this_object); + if (is(*this_object)) + return static_cast(this_object)->window().ptr(); + + if (is(*this_object)) + return static_cast(this_object); + + return vm.throw_completion(JS::ErrorType::NotAnObjectOfType, "Window"); } JS_DEFINE_NATIVE_FUNCTION(Window::alert) @@ -1069,6 +1074,30 @@ JS_DEFINE_NATIVE_FUNCTION(Window::top_getter) return browsing_context->top_level_browsing_context().window_proxy(); } +// https://html.spec.whatwg.org/multipage/window-object.html#dom-self +JS_DEFINE_NATIVE_FUNCTION(Window::self_getter) +{ + auto* impl = TRY(impl_from(vm)); + // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]]. + return &relevant_realm(*impl).global_environment().global_this_value(); +} + +// https://html.spec.whatwg.org/multipage/window-object.html#dom-window +JS_DEFINE_NATIVE_FUNCTION(Window::window_getter) +{ + auto* impl = TRY(impl_from(vm)); + // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]]. + return &relevant_realm(*impl).global_environment().global_this_value(); +} + +// https://html.spec.whatwg.org/multipage/window-object.html#dom-frames +JS_DEFINE_NATIVE_FUNCTION(Window::frames_getter) +{ + auto* impl = TRY(impl_from(vm)); + // The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]]. + return &relevant_realm(*impl).global_environment().global_this_value(); +} + JS_DEFINE_NATIVE_FUNCTION(Window::parent_getter) { auto* impl = TRY(impl_from(vm)); diff --git a/Userland/Libraries/LibWeb/HTML/Window.h b/Userland/Libraries/LibWeb/HTML/Window.h index 13f1ad7888..ec6f46d0e5 100644 --- a/Userland/Libraries/LibWeb/HTML/Window.h +++ b/Userland/Libraries/LibWeb/HTML/Window.h @@ -203,6 +203,10 @@ private: JS_DECLARE_NATIVE_FUNCTION(inner_width_getter); JS_DECLARE_NATIVE_FUNCTION(inner_height_getter); + JS_DECLARE_NATIVE_FUNCTION(window_getter); + JS_DECLARE_NATIVE_FUNCTION(frames_getter); + JS_DECLARE_NATIVE_FUNCTION(self_getter); + JS_DECLARE_NATIVE_FUNCTION(parent_getter); JS_DECLARE_NATIVE_FUNCTION(device_pixel_ratio_getter);