Selaa lähdekoodia

LibWeb: Implement `navigate()` for Location object

Aliaksandr Kalenik 1 vuosi sitten
vanhempi
commit
43da0701fc

+ 20 - 0
Userland/Libraries/LibWeb/HTML/Location.cpp

@@ -58,6 +58,26 @@ JS::GCPtr<DOM::Document> Location::relevant_document() const
     return browsing_context ? browsing_context->active_document() : nullptr;
 }
 
+// https://html.spec.whatwg.org/multipage/nav-history-apis.html#location-object-navigate
+WebIDL::ExceptionOr<void> Location::navigate(AK::URL url, HistoryHandlingBehavior history_handling)
+{
+    // 1. Let navigable be location's relevant global object's navigable.
+    auto navigable = verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).navigable();
+
+    // 2. Let sourceDocument be the incumbent global object's associated Document.
+    auto& source_document = verify_cast<HTML::Window>(incumbent_global_object()).associated_document();
+
+    // 3. If location's relevant Document is not yet completely loaded, and the incumbent global object does not have transient activation, then set historyHandling to "replace".
+    if (!relevant_document()->is_completely_loaded() && !verify_cast<HTML::Window>(incumbent_global_object()).has_transient_activation()) {
+        history_handling = HistoryHandlingBehavior::Replace;
+    }
+
+    // 4. Navigate navigable to url using sourceDocument, with exceptionsEnabled set to true and historyHandling set to historyHandling.
+    TRY(navigable->navigate(url, source_document, {}, nullptr, true, history_handling));
+
+    return {};
+}
+
 // https://html.spec.whatwg.org/multipage/history.html#concept-location-url
 AK::URL Location::url() const
 {

+ 2 - 0
Userland/Libraries/LibWeb/HTML/Location.h

@@ -13,6 +13,7 @@
 #include <LibWeb/Bindings/PlatformObject.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/HTML/CrossOrigin/CrossOriginPropertyDescriptorMap.h>
+#include <LibWeb/HTML/HistoryHandlingBehavior.h>
 
 namespace Web::HTML {
 
@@ -74,6 +75,7 @@ private:
 
     JS::GCPtr<DOM::Document> relevant_document() const;
     AK::URL url() const;
+    WebIDL::ExceptionOr<void> navigate(AK::URL, HistoryHandlingBehavior = HistoryHandlingBehavior::Default);
 
     // [[CrossOriginPropertyDescriptorMap]], https://html.spec.whatwg.org/multipage/browsers.html#crossoriginpropertydescriptormap
     HTML::CrossOriginPropertyDescriptorMap m_cross_origin_property_descriptor_map;