Browse Source

LibWeb: Update `Location::replace()` to use navigables

Aliaksandr Kalenik 1 year ago
parent
commit
d45f2a4952

+ 14 - 4
Userland/Libraries/LibWeb/HTML/Location.cpp

@@ -372,11 +372,21 @@ void Location::reload() const
 }
 }
 
 
 // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
 // https://html.spec.whatwg.org/multipage/history.html#dom-location-replace
-void Location::replace(String const& url) const
+WebIDL::ExceptionOr<void> Location::replace(String const& url)
 {
 {
-    auto& window = verify_cast<HTML::Window>(HTML::current_global_object());
-    // FIXME: This needs spec compliance work.
-    window.did_call_location_replace({}, url.to_deprecated_string());
+    // 1. If this's relevant Document is null, then return.
+    if (!relevant_document())
+        return {};
+
+    // 2. Parse url relative to the entry settings object. If that failed, throw a "SyntaxError" DOMException.
+    auto replace_url = entry_settings_object().parse_url(url);
+    if (!replace_url.is_valid())
+        return WebIDL::SyntaxError::create(realm(), MUST(String::formatted("Invalid URL '{}'", url)));
+
+    // 3. Location-object navigate this to the resulting URL record given "replace".
+    TRY(navigate(replace_url, HistoryHandlingBehavior::Replace));
+
+    return {};
 }
 }
 
 
 // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign
 // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-assign

+ 1 - 1
Userland/Libraries/LibWeb/HTML/Location.h

@@ -49,7 +49,7 @@ public:
     WebIDL::ExceptionOr<String> hash() const;
     WebIDL::ExceptionOr<String> hash() const;
     WebIDL::ExceptionOr<void> set_hash(String const&);
     WebIDL::ExceptionOr<void> set_hash(String const&);
 
 
-    void replace(String const& url) const;
+    WebIDL::ExceptionOr<void> replace(String const& url);
     void reload() const;
     void reload() const;
     WebIDL::ExceptionOr<void> assign(String const& url);
     WebIDL::ExceptionOr<void> assign(String const& url);
 
 

+ 0 - 9
Userland/Libraries/LibWeb/HTML/Window.cpp

@@ -421,15 +421,6 @@ WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> Window::open_impl(StringView url, St
     return target_browsing_context->window_proxy();
     return target_browsing_context->window_proxy();
 }
 }
 
 
-void Window::did_call_location_replace(Badge<Location>, DeprecatedString url)
-{
-    auto* browsing_context = associated_document().browsing_context();
-    if (!browsing_context)
-        return;
-    auto new_url = associated_document().parse_url(url);
-    browsing_context->loader().load(move(new_url), FrameLoader::Type::Navigation);
-}
-
 bool Window::dispatch_event(DOM::Event& event)
 bool Window::dispatch_event(DOM::Event& event)
 {
 {
     return DOM::EventDispatcher::dispatch(*this, event, true);
     return DOM::EventDispatcher::dispatch(*this, event, true);

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

@@ -95,8 +95,6 @@ public:
     WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
     WebIDL::ExceptionOr<JS::GCPtr<WindowProxy>> open_impl(StringView url, StringView target, StringView features);
     bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
     bool has_animation_frame_callbacks() const { return m_animation_frame_callback_driver.has_callbacks(); }
 
 
-    void did_call_location_replace(Badge<Location>, DeprecatedString url);
-
     DOM::Event* current_event() { return m_current_event.ptr(); }
     DOM::Event* current_event() { return m_current_event.ptr(); }
     DOM::Event const* current_event() const { return m_current_event.ptr(); }
     DOM::Event const* current_event() const { return m_current_event.ptr(); }
     void set_current_event(DOM::Event* event);
     void set_current_event(DOM::Event* event);