1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibWeb: Add a bare implementation of the URL built-in

This only has the constructor implemented for now.
This commit is contained in:
Idan Horowitz 2021-09-14 00:10:22 +03:00 committed by Andreas Kling
parent 30849b10d5
commit e6abc1b44e
8 changed files with 103 additions and 1 deletions

View file

@ -8,4 +8,40 @@
#include <LibWeb/URL/URL.h>
namespace Web::URL {
DOM::ExceptionOr<NonnullRefPtr<URL>> URL::create_with_global_object(Bindings::WindowObject& window_object, String const& url, String const& base)
{
// 1. Let parsedBase be null.
Optional<AK::URL> parsed_base;
// 2. If base is given, then:
if (!base.is_null()) {
// 1. Let parsedBase be the result of running the basic URL parser on base.
parsed_base = base;
// 2. If parsedBase is failure, then throw a TypeError.
if (!parsed_base->is_valid())
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid base URL" };
}
// 3. Let parsedURL be the result of running the basic URL parser on url with parsedBase.
AK::URL parsed_url;
if (parsed_base.has_value())
parsed_url = parsed_base->complete_url(url);
else
parsed_url = url;
// 4. If parsedURL is failure, then throw a TypeError.
if (!parsed_url.is_valid())
return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" };
// 5. Let query be parsedURLs query, if that is non-null, and the empty string otherwise.
auto& query = parsed_url.query().is_null() ? String::empty() : parsed_url.query();
// 6. Set thiss URL to parsedURL.
// 7. Set thiss query object to a new URLSearchParams object.
auto query_object = URLSearchParams::create_with_global_object(window_object, query);
VERIFY(!query_object.is_exception()); // The string variant of the constructor can't throw.
// 8. Initialize thiss query object with query.
auto result_url = URL::create(move(parsed_url), query_object.release_value());
// 9. Set thiss query objects URL object to this.
result_url->m_query->m_url = result_url;
return result_url;
}
}