From efdfdbabdbef1354424d0d9aafac232ee2dc17a0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 18 May 2020 21:52:50 +0200 Subject: [PATCH] LibWeb: Allow navigating to a new URL by setting window.location.href --- Base/home/anon/www/location.html | 10 +++++++++- Libraries/LibWeb/Bindings/LocationObject.cpp | 8 ++++++-- Libraries/LibWeb/DOM/Window.cpp | 14 ++++++++++++++ Libraries/LibWeb/DOM/Window.h | 2 ++ Libraries/LibWeb/Forward.h | 1 + 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/Base/home/anon/www/location.html b/Base/home/anon/www/location.html index 61a42351c6..9405fd93fc 100644 --- a/Base/home/anon/www/location.html +++ b/Base/home/anon/www/location.html @@ -2,9 +2,13 @@ window.location test + -

+    

+    
Click me to navigate away!
diff --git a/Libraries/LibWeb/Bindings/LocationObject.cpp b/Libraries/LibWeb/Bindings/LocationObject.cpp index 302f64db40..f819903195 100644 --- a/Libraries/LibWeb/Bindings/LocationObject.cpp +++ b/Libraries/LibWeb/Bindings/LocationObject.cpp @@ -54,9 +54,13 @@ JS::Value LocationObject::href_getter(JS::Interpreter& interpreter) return JS::js_string(interpreter, window.impl().document().url().to_string()); } -void LocationObject::href_setter(JS::Interpreter&, JS::Value) +void LocationObject::href_setter(JS::Interpreter& interpreter, JS::Value value) { - // FIXME: Navigate to a new URL + auto& window = static_cast(interpreter.global_object()); + auto new_href = value.to_string(interpreter); + if (interpreter.exception()) + return; + window.impl().did_set_location_href({}, new_href); } JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter) diff --git a/Libraries/LibWeb/DOM/Window.cpp b/Libraries/LibWeb/DOM/Window.cpp index adac5c5068..01bf4ca225 100644 --- a/Libraries/LibWeb/DOM/Window.cpp +++ b/Libraries/LibWeb/DOM/Window.cpp @@ -30,7 +30,10 @@ #include #include #include +#include #include +#include +#include namespace Web { @@ -105,4 +108,15 @@ void Window::cancel_animation_frame(i32 id) GUI::DisplayLink::unregister_callback(id); } +void Window::did_set_location_href(Badge, const String& new_href) +{ + auto* frame = document().frame(); + if (!frame) + return; + auto* view = frame->html_view(); + if (!view) + return; + view->load(new_href); +} + } diff --git a/Libraries/LibWeb/DOM/Window.h b/Libraries/LibWeb/DOM/Window.h index 03d548b71a..77214a7de6 100644 --- a/Libraries/LibWeb/DOM/Window.h +++ b/Libraries/LibWeb/DOM/Window.h @@ -49,6 +49,8 @@ public: void set_interval(JS::Function&, i32); void set_timeout(JS::Function&, i32); + void did_set_location_href(Badge, const String& new_href); + private: explicit Window(Document&); diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index b54f45f958..f84af285ec 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -67,6 +67,7 @@ class EventTargetWrapper; class HTMLCanvasElementWrapper; class HTMLImageElementWrapper; class ImageDataWrapper; +class LocationObject; class MouseEventWrapper; class NodeWrapper; class WindowObject;