diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 54a8de9598..db0aee4847 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -396,22 +396,13 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::top_getter) return top_window.wrapper(); } -// https://html.spec.whatwg.org/multipage/browsers.html#dom-parent JS_DEFINE_NATIVE_FUNCTION(WindowObject::parent_getter) { auto* impl = TRY(impl_from(vm, global_object)); - - auto* this_browsing_context = impl->associated_document().browsing_context(); - if (!this_browsing_context) + auto* parent = impl->parent(); + if (!parent) return JS::js_null(); - - if (this_browsing_context->parent()) { - VERIFY(this_browsing_context->parent()->active_document()); - auto& parent_window = this_browsing_context->parent()->active_document()->window(); - return parent_window.wrapper(); - } - VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context()); - return impl->wrapper(); + return parent->wrapper(); } JS_DEFINE_NATIVE_FUNCTION(WindowObject::document_getter) diff --git a/Userland/Libraries/LibWeb/DOM/Window.cpp b/Userland/Libraries/LibWeb/DOM/Window.cpp index 9e24ff671c..711c1b8ac8 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.cpp +++ b/Userland/Libraries/LibWeb/DOM/Window.cpp @@ -451,4 +451,29 @@ RefPtr Window::local_storage() }); } +// https://html.spec.whatwg.org/multipage/browsers.html#dom-parent +Window* Window::parent() +{ + // 1. Let current be this Window object's browsing context. + auto* current = associated_document().browsing_context(); + + // 2. If current is null, then return null. + if (!current) + return nullptr; + + // 3. If current is a child browsing context of another browsing context parent, + // then return parent's WindowProxy object. + if (current->parent()) { + VERIFY(current->parent()->active_document()); + return ¤t->parent()->active_document()->window(); + } + + // 4. Assert: current is a top-level browsing context. + VERIFY(current->is_top_level()); + + // FIXME: 5. Return current's WindowProxy object. + VERIFY(current->active_document()); + return ¤t->active_document()->window(); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Window.h b/Userland/Libraries/LibWeb/DOM/Window.h index 99fa996d6e..21cc82abbd 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.h +++ b/Userland/Libraries/LibWeb/DOM/Window.h @@ -100,6 +100,8 @@ public: RefPtr local_storage(); + Window* parent(); + private: explicit Window(Document&);