From 2ad25aa8f86b21f824b4349703aaadf8f5e45831 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 31 May 2021 10:10:05 +0100 Subject: [PATCH] LibWeb: Return null in Window.{top,parent} if browsing context is null We were asserting that it exists, but the spec says to return null in this case. Top: https://html.spec.whatwg.org/multipage/browsers.html#dom-top Parent: https://html.spec.whatwg.org/multipage/browsers.html#dom-parent --- Userland/Libraries/LibWeb/Bindings/WindowObject.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index 4d85f0a6b6..902e8aeebf 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -339,25 +339,33 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa) return JS::js_string(vm, move(encoded)); } +// https://html.spec.whatwg.org/multipage/browsers.html#dom-top JS_DEFINE_NATIVE_GETTER(WindowObject::top_getter) { auto* impl = impl_from(vm, global_object); if (!impl) return {}; + auto* this_browsing_context = impl->document().browsing_context(); - VERIFY(this_browsing_context); + if (!this_browsing_context) + return JS::js_null(); + VERIFY(this_browsing_context->top_level_browsing_context().document()); auto& top_window = this_browsing_context->top_level_browsing_context().document()->window(); return top_window.wrapper(); } +// https://html.spec.whatwg.org/multipage/browsers.html#dom-parent JS_DEFINE_NATIVE_GETTER(WindowObject::parent_getter) { auto* impl = impl_from(vm, global_object); if (!impl) return {}; + auto* this_browsing_context = impl->document().browsing_context(); - VERIFY(this_browsing_context); + if (!this_browsing_context) + return JS::js_null(); + if (this_browsing_context->parent()) { VERIFY(this_browsing_context->parent()->document()); auto& parent_window = this_browsing_context->parent()->document()->window();