Browse Source

LibWeb: Implement window.top

This simply returns the main frame's window object. If accessed inside
the main frame, it will return itself.
Andreas Kling 4 năm trước cách đây
mục cha
commit
2f9321a0d4

+ 14 - 0
Userland/Libraries/LibWeb/Bindings/WindowObject.cpp

@@ -46,6 +46,7 @@
 #include <LibWeb/DOM/Event.h>
 #include <LibWeb/DOM/Window.h>
 #include <LibWeb/Origin.h>
+#include <LibWeb/Page/Frame.h>
 
 #include <LibWeb/Bindings/WindowObjectHelper.h>
 
@@ -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);

+ 2 - 0
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);