1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:57:44 +00:00

LibWeb: Replace usage of native properties with accessors in Location

This is required by the WebIDL specification.
This commit is contained in:
Idan Horowitz 2021-07-05 15:01:57 +03:00 committed by Linus Groh
parent 073071c634
commit 6468a2bf21
2 changed files with 27 additions and 26 deletions

View file

@ -23,13 +23,13 @@ void LocationObject::initialize(JS::GlobalObject& global_object)
{
Object::initialize(global_object);
u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
define_native_property("href", href_getter, href_setter, attr);
define_native_property("host", host_getter, nullptr, attr);
define_native_property("hostname", hostname_getter, nullptr, attr);
define_native_property("pathname", pathname_getter, nullptr, attr);
define_native_property("hash", hash_getter, nullptr, attr);
define_native_property("search", search_getter, nullptr, attr);
define_native_property("protocol", protocol_getter, nullptr, attr);
define_native_accessor("href", href_getter, href_setter, attr);
define_native_accessor("host", host_getter, {}, attr);
define_native_accessor("hostname", hostname_getter, {}, attr);
define_native_accessor("pathname", pathname_getter, {}, attr);
define_native_accessor("hash", hash_getter, {}, attr);
define_native_accessor("search", search_getter, {}, attr);
define_native_accessor("protocol", protocol_getter, {}, attr);
define_native_function("reload", reload, 0, JS::Attribute::Enumerable);
}
@ -38,46 +38,47 @@ LocationObject::~LocationObject()
{
}
JS_DEFINE_NATIVE_GETTER(LocationObject::href_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
return JS::js_string(vm, window.impl().document().url().to_string());
}
JS_DEFINE_NATIVE_SETTER(LocationObject::href_setter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
{
auto& window = static_cast<WindowObject&>(global_object);
auto new_href = value.to_string(global_object);
auto new_href = vm.argument(0).to_string(global_object);
if (vm.exception())
return;
return {};
auto href_url = window.impl().document().complete_url(new_href);
if (!href_url.is_valid()) {
vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
return;
return {};
}
window.impl().did_set_location_href({}, href_url);
return JS::js_undefined();
}
JS_DEFINE_NATIVE_GETTER(LocationObject::pathname_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::pathname_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
return JS::js_string(vm, window.impl().document().url().path());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::hostname_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::hostname_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
return JS::js_string(vm, window.impl().document().url().host());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::host_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::host_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
auto url = window.impl().document().url();
return JS::js_string(vm, String::formatted("{}:{}", url.host(), url.port()));
}
JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::hash_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
auto fragment = window.impl().document().url().fragment();
@ -89,7 +90,7 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter)
return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::search_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
auto query = window.impl().document().url().query();
@ -101,7 +102,7 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter)
return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::protocol_getter)
JS_DEFINE_NATIVE_FUNCTION(LocationObject::protocol_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
StringBuilder builder;