LibWeb: Convert the Location object to ThrowCompletionOr
This commit is contained in:
parent
1939c72ecc
commit
585e420707
Notes:
sideshowbarker
2024-07-18 01:41:48 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/585e420707b Pull-request: https://github.com/SerenityOS/serenity/pull/10727 Reviewed-by: https://github.com/linusg ✅
2 changed files with 37 additions and 39 deletions
Userland/Libraries/LibWeb/Bindings
|
@ -25,64 +25,62 @@ void LocationObject::initialize(JS::GlobalObject& global_object)
|
|||
|
||||
Object::initialize(global_object);
|
||||
u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
|
||||
define_old_native_accessor("href", href_getter, href_setter, attr);
|
||||
define_old_native_accessor("host", host_getter, {}, attr);
|
||||
define_old_native_accessor("hostname", hostname_getter, {}, attr);
|
||||
define_old_native_accessor("pathname", pathname_getter, {}, attr);
|
||||
define_old_native_accessor("hash", hash_getter, {}, attr);
|
||||
define_old_native_accessor("search", search_getter, {}, attr);
|
||||
define_old_native_accessor("protocol", protocol_getter, {}, attr);
|
||||
define_old_native_accessor("port", port_getter, {}, 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_accessor("port", port_getter, {}, attr);
|
||||
|
||||
define_old_native_function("reload", reload, 0, JS::Attribute::Enumerable);
|
||||
define_old_native_function("replace", replace, 1, JS::Attribute::Enumerable);
|
||||
define_native_function("reload", reload, 0, JS::Attribute::Enumerable);
|
||||
define_native_function("replace", replace, 1, JS::Attribute::Enumerable);
|
||||
|
||||
define_old_native_function(vm.names.toString, href_getter, 0, JS::Attribute::Enumerable);
|
||||
define_native_function(vm.names.toString, href_getter, 0, JS::Attribute::Enumerable);
|
||||
}
|
||||
|
||||
LocationObject::~LocationObject()
|
||||
{
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::href_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
return JS::js_string(vm, window.impl().associated_document().url().to_string());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::href_setter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::href_setter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
auto new_href = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
|
||||
auto new_href = TRY(vm.argument(0).to_string(global_object));
|
||||
auto href_url = window.impl().associated_document().parse_url(new_href);
|
||||
if (!href_url.is_valid()) {
|
||||
vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
|
||||
return {};
|
||||
}
|
||||
if (!href_url.is_valid())
|
||||
return vm.throw_completion<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
|
||||
window.impl().did_set_location_href({}, href_url);
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::pathname_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::pathname_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
return JS::js_string(vm, window.impl().associated_document().url().path());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::hostname_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::hostname_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
return JS::js_string(vm, window.impl().associated_document().url().host());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::host_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::host_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
auto url = window.impl().associated_document().url();
|
||||
return JS::js_string(vm, String::formatted("{}:{}", url.host(), url.port_or_default()));
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::hash_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::hash_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
auto fragment = window.impl().associated_document().url().fragment();
|
||||
|
@ -94,7 +92,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::hash_getter)
|
|||
return JS::js_string(vm, builder.to_string());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::search_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::search_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
auto query = window.impl().associated_document().url().query();
|
||||
|
@ -106,7 +104,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::search_getter)
|
|||
return JS::js_string(vm, builder.to_string());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::protocol_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::protocol_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
StringBuilder builder;
|
||||
|
@ -115,23 +113,23 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::protocol_getter)
|
|||
return JS::js_string(vm, builder.to_string());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::port_getter)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::port_getter)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
return JS::Value(window.impl().associated_document().url().port_or_default());
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::reload)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
window.impl().did_call_location_reload({});
|
||||
return JS::js_undefined();
|
||||
}
|
||||
|
||||
JS_DEFINE_OLD_NATIVE_FUNCTION(LocationObject::replace)
|
||||
JS_DEFINE_NATIVE_FUNCTION(LocationObject::replace)
|
||||
{
|
||||
auto& window = static_cast<WindowObject&>(global_object);
|
||||
auto url = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
|
||||
auto url = TRY(vm.argument(0).to_string(global_object));
|
||||
// FIXME: This needs spec compliance work.
|
||||
window.impl().did_call_location_replace({}, move(url));
|
||||
return JS::js_undefined();
|
||||
|
|
|
@ -29,19 +29,19 @@ public:
|
|||
// but we don't have the infrastructure in place to implement them yet.
|
||||
|
||||
private:
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(reload);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(replace);
|
||||
JS_DECLARE_NATIVE_FUNCTION(reload);
|
||||
JS_DECLARE_NATIVE_FUNCTION(replace);
|
||||
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(href_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(href_setter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(href_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(href_setter);
|
||||
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(host_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(hostname_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(pathname_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(hash_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(search_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(protocol_getter);
|
||||
JS_DECLARE_OLD_NATIVE_FUNCTION(port_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(host_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(hostname_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(pathname_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(hash_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(search_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(protocol_getter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(port_getter);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue