summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-05-18 21:42:40 +0200
committerAndreas Kling <kling@serenityos.org>2020-05-18 21:42:40 +0200
commit1ec4db04cd4a04263c7081d95fbc804b2e610b42 (patch)
treeb1c70aff42dd6ab66300875b562db2420f40f794
parenta1e7aa330cf50326b99a398293e582a551cbd8fb (diff)
downloadserenity-1ec4db04cd4a04263c7081d95fbc804b2e610b42.zip
LibWeb: Add a simple window.location object with some getters :^)
-rw-r--r--Base/home/anon/www/location.html17
-rw-r--r--Base/home/anon/www/welcome.html1
-rw-r--r--Libraries/LibWeb/Bindings/LocationObject.cpp88
-rw-r--r--Libraries/LibWeb/Bindings/LocationObject.h53
-rw-r--r--Libraries/LibWeb/Bindings/WindowObject.cpp2
-rw-r--r--Libraries/LibWeb/CMakeLists.txt1
6 files changed, 162 insertions, 0 deletions
diff --git a/Base/home/anon/www/location.html b/Base/home/anon/www/location.html
new file mode 100644
index 0000000000..61a42351c6
--- /dev/null
+++ b/Base/home/anon/www/location.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>window.location test</title>
+ </head>
+ <body>
+ <pre></pre>
+ <script>
+ var pre = document.querySelectorAll("pre")[0];
+ pre.innerHTML += "href: " + location.href + '\n';
+ pre.innerHTML += "hostname: " + location.hostname + '\n';
+ pre.innerHTML += "pathname: " + location.pathname+ '\n';
+ pre.innerHTML += "hash: " + location.hash + '\n';
+ pre.innerHTML += "search: " + location.search + '\n';
+ </script>
+ </body>
+</html>
diff --git a/Base/home/anon/www/welcome.html b/Base/home/anon/www/welcome.html
index 48f42c30df..3ac63a9022 100644
--- a/Base/home/anon/www/welcome.html
+++ b/Base/home/anon/www/welcome.html
@@ -28,6 +28,7 @@ span#ua {
<p>Your user agent is: <b><span id="ua"></span></b></p>
<p>Some small test pages:</p>
<ul>
+ <li><a href="location.html">window.location property</a></li>
<li><a href="percent-css.html">CSS percentage values</a></li>
<li><a href="inline-block.html">display: inline-block; test</a></li>
<li><a href="canvas-path-quadratic-curve.html">canvas path quadratic curve test</a></li>
diff --git a/Libraries/LibWeb/Bindings/LocationObject.cpp b/Libraries/LibWeb/Bindings/LocationObject.cpp
new file mode 100644
index 0000000000..302f64db40
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/LocationObject.cpp
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <AK/FlyString.h>
+#include <LibJS/Interpreter.h>
+#include <LibWeb/Bindings/LocationObject.h>
+#include <LibWeb/Bindings/WindowObject.h>
+#include <LibWeb/DOM/Document.h>
+#include <LibWeb/DOM/Window.h>
+
+namespace Web {
+namespace Bindings {
+
+LocationObject::LocationObject()
+ : Object(interpreter().global_object().object_prototype())
+{
+ put_native_property("href", href_getter, href_setter);
+ put_native_property("hostname", hostname_getter, nullptr);
+ put_native_property("pathname", pathname_getter, nullptr);
+ put_native_property("hash", hash_getter, nullptr);
+ put_native_property("search", search_getter, nullptr);
+}
+
+LocationObject::~LocationObject()
+{
+}
+
+JS::Value LocationObject::href_getter(JS::Interpreter& interpreter)
+{
+ auto& window = static_cast<WindowObject&>(interpreter.global_object());
+ return JS::js_string(interpreter, window.impl().document().url().to_string());
+}
+
+void LocationObject::href_setter(JS::Interpreter&, JS::Value)
+{
+ // FIXME: Navigate to a new URL
+}
+
+JS::Value LocationObject::pathname_getter(JS::Interpreter& interpreter)
+{
+ auto& window = static_cast<WindowObject&>(interpreter.global_object());
+ return JS::js_string(interpreter, window.impl().document().url().path());
+}
+
+JS::Value LocationObject::hostname_getter(JS::Interpreter& interpreter)
+{
+ auto& window = static_cast<WindowObject&>(interpreter.global_object());
+ return JS::js_string(interpreter, window.impl().document().url().host());
+}
+
+JS::Value LocationObject::hash_getter(JS::Interpreter& interpreter)
+{
+ auto& window = static_cast<WindowObject&>(interpreter.global_object());
+ return JS::js_string(interpreter, window.impl().document().url().fragment());
+}
+
+JS::Value LocationObject::search_getter(JS::Interpreter& interpreter)
+{
+ auto& window = static_cast<WindowObject&>(interpreter.global_object());
+ return JS::js_string(interpreter, window.impl().document().url().query());
+}
+
+}
+
+}
diff --git a/Libraries/LibWeb/Bindings/LocationObject.h b/Libraries/LibWeb/Bindings/LocationObject.h
new file mode 100644
index 0000000000..e34d228068
--- /dev/null
+++ b/Libraries/LibWeb/Bindings/LocationObject.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <LibJS/Runtime/Object.h>
+#include <LibWeb/Forward.h>
+
+namespace Web {
+namespace Bindings {
+
+class LocationObject final : public JS::Object {
+public:
+ LocationObject();
+ virtual ~LocationObject() override;
+
+private:
+ virtual const char* class_name() const override { return "LocationObject"; }
+
+ static JS::Value href_getter(JS::Interpreter&);
+ static void href_setter(JS::Interpreter&, JS::Value);
+
+ static JS::Value hostname_getter(JS::Interpreter&);
+ static JS::Value pathname_getter(JS::Interpreter&);
+ static JS::Value hash_getter(JS::Interpreter&);
+ static JS::Value search_getter(JS::Interpreter&);
+};
+
+}
+}
diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp
index c63169ba15..659229c46a 100644
--- a/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -31,6 +31,7 @@
#include <LibJS/Runtime/Function.h>
#include <LibJS/Runtime/Shape.h>
#include <LibWeb/Bindings/DocumentWrapper.h>
+#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/NavigatorObject.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
@@ -60,6 +61,7 @@ void WindowObject::initialize()
put_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
put("navigator", heap().allocate<NavigatorObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable);
+ put("location", heap().allocate<LocationObject>(), JS::Attribute::Enumerable | JS::Attribute::Configurable);
m_xhr_prototype = heap().allocate<XMLHttpRequestPrototype>();
m_xhr_constructor = heap().allocate<XMLHttpRequestConstructor>();
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 2483e7332d..2a53d4ed56 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -8,6 +8,7 @@ set(SOURCES
Bindings/HTMLCanvasElementWrapper.cpp
Bindings/HTMLImageElementWrapper.cpp
Bindings/ImageDataWrapper.cpp
+ Bindings/LocationObject.cpp
Bindings/MouseEventWrapper.cpp
Bindings/NavigatorObject.cpp
Bindings/NodeWrapper.cpp