LibWeb: Add location.protocol and location.host

This commit is contained in:
Andreas Kling 2020-05-18 21:59:16 +02:00
parent efdfdbabdb
commit 71007f6ebb
Notes: sideshowbarker 2024-07-19 06:29:00 +09:00
3 changed files with 27 additions and 0 deletions

View file

@ -12,6 +12,8 @@
<script>
var pre = document.querySelectorAll("pre")[0];
pre.innerHTML += "href: " + location.href + '\n';
pre.innerHTML += "protocol: " + location.protocol + '\n';
pre.innerHTML += "host: " + location.host + '\n';
pre.innerHTML += "hostname: " + location.hostname + '\n';
pre.innerHTML += "pathname: " + location.pathname+ '\n';
pre.innerHTML += "hash: " + location.hash + '\n';

View file

@ -25,6 +25,7 @@
*/
#include <AK/FlyString.h>
#include <AK/StringBuilder.h>
#include <LibJS/Interpreter.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/WindowObject.h>
@ -38,10 +39,12 @@ LocationObject::LocationObject()
: Object(interpreter().global_object().object_prototype())
{
put_native_property("href", href_getter, href_setter);
put_native_property("host", host_getter, nullptr);
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);
put_native_property("protocol", protocol_getter, nullptr);
}
LocationObject::~LocationObject()
@ -75,6 +78,17 @@ JS::Value LocationObject::hostname_getter(JS::Interpreter& interpreter)
return JS::js_string(interpreter, window.impl().document().url().host());
}
JS::Value LocationObject::host_getter(JS::Interpreter& interpreter)
{
auto& window = static_cast<WindowObject&>(interpreter.global_object());
auto& url = window.impl().document().url();
StringBuilder builder;
builder.append(url.host());
builder.append(':');
builder.appendf("%u", url.port());
return JS::js_string(interpreter, builder.to_string());
}
JS::Value LocationObject::hash_getter(JS::Interpreter& interpreter)
{
auto& window = static_cast<WindowObject&>(interpreter.global_object());
@ -87,6 +101,15 @@ JS::Value LocationObject::search_getter(JS::Interpreter& interpreter)
return JS::js_string(interpreter, window.impl().document().url().query());
}
JS::Value LocationObject::protocol_getter(JS::Interpreter& interpreter)
{
auto& window = static_cast<WindowObject&>(interpreter.global_object());
StringBuilder builder;
builder.append(window.impl().document().url().protocol());
builder.append(':');
return JS::js_string(interpreter, builder.to_string());
}
}
}

View file

@ -43,10 +43,12 @@ private:
static JS::Value href_getter(JS::Interpreter&);
static void href_setter(JS::Interpreter&, JS::Value);
static JS::Value host_getter(JS::Interpreter&);
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&);
static JS::Value protocol_getter(JS::Interpreter&);
};
}