瀏覽代碼

LibWeb: Allow navigating to a new URL by setting window.location.href

Andreas Kling 5 年之前
父節點
當前提交
efdfdbabdb

+ 9 - 1
Base/home/anon/www/location.html

@@ -2,9 +2,13 @@
 <html>
 <html>
     <head>
     <head>
         <title>window.location test</title>
         <title>window.location test</title>
+        <style>
+            #clickme { background-color: red; color: yellow; }
+        </style>
     </head>
     </head>
     <body>
     <body>
-        <pre></pre>
+    <pre></pre>
+    <div id="clickme">Click me to navigate away!</div>
     <script>
     <script>
         var pre = document.querySelectorAll("pre")[0];
         var pre = document.querySelectorAll("pre")[0];
         pre.innerHTML += "href: " + location.href + '\n';
         pre.innerHTML += "href: " + location.href + '\n';
@@ -12,6 +16,10 @@
         pre.innerHTML += "pathname: " + location.pathname+ '\n';
         pre.innerHTML += "pathname: " + location.pathname+ '\n';
         pre.innerHTML += "hash: " + location.hash + '\n';
         pre.innerHTML += "hash: " + location.hash + '\n';
         pre.innerHTML += "search: " + location.search + '\n';
         pre.innerHTML += "search: " + location.search + '\n';
+
+        document.getElementById("clickme").addEventListener("mousedown", function() {
+            window.location.href = 'http://serenityos.org/';
+        });
     </script>
     </script>
     </body>
     </body>
 </html>
 </html>

+ 6 - 2
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());
     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<WindowObject&>(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)
 JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter)

+ 14 - 0
Libraries/LibWeb/DOM/Window.cpp

@@ -30,7 +30,10 @@
 #include <LibJS/Interpreter.h>
 #include <LibJS/Interpreter.h>
 #include <LibJS/Runtime/Function.h>
 #include <LibJS/Runtime/Function.h>
 #include <LibJS/Runtime/MarkedValueList.h>
 #include <LibJS/Runtime/MarkedValueList.h>
+#include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Window.h>
 #include <LibWeb/DOM/Window.h>
+#include <LibWeb/Frame.h>
+#include <LibWeb/HtmlView.h>
 
 
 namespace Web {
 namespace Web {
 
 
@@ -105,4 +108,15 @@ void Window::cancel_animation_frame(i32 id)
     GUI::DisplayLink::unregister_callback(id);
     GUI::DisplayLink::unregister_callback(id);
 }
 }
 
 
+void Window::did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href)
+{
+    auto* frame = document().frame();
+    if (!frame)
+        return;
+    auto* view = frame->html_view();
+    if (!view)
+        return;
+    view->load(new_href);
+}
+
 }
 }

+ 2 - 0
Libraries/LibWeb/DOM/Window.h

@@ -49,6 +49,8 @@ public:
     void set_interval(JS::Function&, i32);
     void set_interval(JS::Function&, i32);
     void set_timeout(JS::Function&, i32);
     void set_timeout(JS::Function&, i32);
 
 
+    void did_set_location_href(Badge<Bindings::LocationObject>, const String& new_href);
+
 private:
 private:
     explicit Window(Document&);
     explicit Window(Document&);
 
 

+ 1 - 0
Libraries/LibWeb/Forward.h

@@ -67,6 +67,7 @@ class EventTargetWrapper;
 class HTMLCanvasElementWrapper;
 class HTMLCanvasElementWrapper;
 class HTMLImageElementWrapper;
 class HTMLImageElementWrapper;
 class ImageDataWrapper;
 class ImageDataWrapper;
+class LocationObject;
 class MouseEventWrapper;
 class MouseEventWrapper;
 class NodeWrapper;
 class NodeWrapper;
 class WindowObject;
 class WindowObject;