LibWeb: Add a simple window.location object with some getters :^)
This commit is contained in:
parent
a1e7aa330c
commit
1ec4db04cd
Notes:
sideshowbarker
2024-07-19 06:29:05 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/1ec4db04cd4
6 changed files with 162 additions and 0 deletions
Base/home/anon/www
Libraries/LibWeb
17
Base/home/anon/www/location.html
Normal file
17
Base/home/anon/www/location.html
Normal file
|
@ -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>
|
|
@ -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>
|
||||
|
|
88
Libraries/LibWeb/Bindings/LocationObject.cpp
Normal file
88
Libraries/LibWeb/Bindings/LocationObject.cpp
Normal file
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
53
Libraries/LibWeb/Bindings/LocationObject.h
Normal file
53
Libraries/LibWeb/Bindings/LocationObject.h
Normal file
|
@ -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&);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
|
@ -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>();
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue