@@ -787,15 +787,59 @@ String Window::status() const
787
787
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-close
788
788
void Window::close ()
789
789
{
790
- // FIXME: Implement this properly
791
- dbgln (" (STUBBED) Window::close()" );
790
+ // 1. Let thisTraversable be this's navigable.
791
+ auto traversable = navigable ();
792
+
793
+ // 2. If thisTraversable is not a top-level traversable, then return.
794
+ if (!traversable || !traversable->is_top_level_traversable ())
795
+ return ;
796
+
797
+ // 3. If thisTraversable's is closing is true, then return.
798
+ if (traversable->is_closing ())
799
+ return ;
800
+
801
+ // 4. Let browsingContext be thisTraversable's active browsing context.
802
+ auto browsing_context = traversable->active_browsing_context ();
803
+
804
+ // 5. Let sourceSnapshotParams be the result of snapshotting source snapshot params given thisTraversable's active document.
805
+ auto source_snapshot_params = traversable->active_document ()->snapshot_source_snapshot_params ();
806
+
807
+ auto & incumbent_global_object = verify_cast<HTML::Window>(HTML::incumbent_global_object ());
808
+
809
+ // 6. If all the following are true:
810
+ if (
811
+ // thisTraversable is script-closable;
812
+ traversable->is_script_closable ()
813
+
814
+ // the incumbent global object's browsing context is familiar with browsingContext; and
815
+ && incumbent_global_object.browsing_context ()->is_familiar_with (*browsing_context)
816
+
817
+ // the incumbent global object's navigable is allowed by sandboxing to navigate thisTraversable, given sourceSnapshotParams,
818
+ && incumbent_global_object.navigable ()->allowed_by_sandboxing_to_navigate (*traversable, source_snapshot_params))
819
+ // then:
820
+ {
821
+ // 1. Set thisTraversable's is closing to true.
822
+ traversable->set_closing (true );
823
+
824
+ // 2. Queue a task on the DOM manipulation task source to close thisTraversable.
825
+ HTML::queue_global_task (HTML::Task::Source::DOMManipulation, incumbent_global_object, JS::create_heap_function (heap (), [traversable] {
826
+ verify_cast<TraversableNavigable>(*traversable).close_top_level_traversable ();
827
+ }));
828
+ }
792
829
}
793
830
794
831
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-window-closed
795
832
bool Window::closed () const
796
833
{
797
- // FIXME: Implement this properly
798
- dbgln (" (STUBBED) Window::closed" );
834
+ // The closed getter steps are to return true if this's browsing context is null or its is closing is true;
835
+ // otherwise false.
836
+ if (!browsing_context ())
837
+ return true ;
838
+
839
+ // FIXME: The spec seems a bit out of date. The `is closing` flag is on the navigable, not the browsing context.
840
+ if (auto navigable = this ->navigable (); !navigable || navigable->is_closing ())
841
+ return true ;
842
+
799
843
return false ;
800
844
}
801
845
0 commit comments