diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h index a2b24ed93f..a16f1883a9 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h @@ -227,6 +227,8 @@ #include #include #include +#include +#include #include #include #include @@ -359,6 +361,7 @@ ADD_WINDOW_OBJECT_INTERFACE(Text) \ ADD_WINDOW_OBJECT_INTERFACE(UIEvent) \ ADD_WINDOW_OBJECT_INTERFACE(URLSearchParams) \ + ADD_WINDOW_OBJECT_INTERFACE(URL) \ ADD_WINDOW_OBJECT_INTERFACE(WebSocket) \ ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest) \ ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequestEventTarget) \ diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 49e2164104..be00a699de 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -422,6 +422,7 @@ libweb_js_wrapper(UIEvents/UIEvent) libweb_js_wrapper(XHR/ProgressEvent) libweb_js_wrapper(XHR/XMLHttpRequest) libweb_js_wrapper(XHR/XMLHttpRequestEventTarget) +libweb_js_wrapper(URL/URL) libweb_js_wrapper(URL/URLSearchParams) add_custom_command( diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index 8e115e93d6..840a33817c 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -206,6 +206,7 @@ class XMLHttpRequestEventTarget; } namespace Web::URL { +class URL; class URLSearchParams; } @@ -334,6 +335,9 @@ class XMLHttpRequestEventTargetWrapper; class RangeConstructor; class RangePrototype; class RangeWrapper; +class URLConstructor; +class URLPrototype; +class URLWrapper; class URLSearchParamsConstructor; class URLSearchParamsPrototype; class URLSearchParamsWrapper; diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 5388032042..2998cc60bf 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -8,4 +8,40 @@ #include namespace Web::URL { + +DOM::ExceptionOr> URL::create_with_global_object(Bindings::WindowObject& window_object, String const& url, String const& base) +{ + // 1. Let parsedBase be null. + Optional 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 parsedURL’s 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 this’s URL to parsedURL. + // 7. Set this’s 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 this’s query object with query. + auto result_url = URL::create(move(parsed_url), query_object.release_value()); + // 9. Set this’s query object’s URL object to this. + result_url->m_query->m_url = result_url; + + return result_url; +} + } diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index 2376cd6580..adcdb00fb4 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -7,5 +7,36 @@ #pragma once +#include +#include +#include +#include +#include + namespace Web::URL { + +class URL : public Bindings::Wrappable + , public RefCounted + , public Weakable { +public: + using WrapperType = Bindings::URLWrapper; + + static NonnullRefPtr create(AK::URL url, NonnullRefPtr query) + { + return adopt_ref(*new URL(move(url), move(query))); + } + + static DOM::ExceptionOr> create_with_global_object(Bindings::WindowObject&, const String& url, const String& base); + + void set_query(Badge, String query) { m_url.set_query(move(query)); } + +private: + explicit URL(AK::URL url, NonnullRefPtr query) + : m_url(move(url)) + , m_query(move(query)) {}; + + AK::URL m_url; + NonnullRefPtr m_query; +}; + } diff --git a/Userland/Libraries/LibWeb/URL/URL.idl b/Userland/Libraries/LibWeb/URL/URL.idl new file mode 100644 index 0000000000..d4b647626d --- /dev/null +++ b/Userland/Libraries/LibWeb/URL/URL.idl @@ -0,0 +1,18 @@ +interface URL { + constructor(USVString url, optional USVString base); + + // TODO: stringifier attribute USVString href; + // TODO: readonly attribute USVString origin; + // TODO: attribute USVString protocol; + // TODO: attribute USVString username; + // TODO: attribute USVString password; + // TODO: attribute USVString host; + // TODO: attribute USVString hostname; + // TODO: attribute USVString port; + // TODO: attribute USVString pathname; + // TODO: attribute USVString search; + // TODO: [SameObject] readonly attribute URLSearchParams searchParams; + // TODO: attribute USVString hash; + + // TODO: USVString toJSON(); +}; diff --git a/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp b/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp index 8be8b95184..3ff4368c89 100644 --- a/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp +++ b/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace Web::URL { @@ -97,11 +98,16 @@ void URLSearchParams::append(String const& name, String const& value) void URLSearchParams::update() { - // TODO // 1. If query’s URL object is null, then return. + if (m_url.is_null()) + return; // 2. Let serializedQuery be the serialization of query’s list. + auto serialized_query = to_string(); // 3. If serializedQuery is the empty string, then set serializedQuery to null. + if (serialized_query.is_empty()) + serialized_query = {}; // 4. Set query’s URL object’s URL’s query to serializedQuery. + m_url->set_query({}, move(serialized_query)); } void URLSearchParams::delete_(String const& name) diff --git a/Userland/Libraries/LibWeb/URL/URLSearchParams.h b/Userland/Libraries/LibWeb/URL/URLSearchParams.h index 5e1dd6d913..f5ca945851 100644 --- a/Userland/Libraries/LibWeb/URL/URLSearchParams.h +++ b/Userland/Libraries/LibWeb/URL/URLSearchParams.h @@ -43,12 +43,15 @@ public: String to_string(); private: + friend class URL; + explicit URLSearchParams(Vector list) : m_list(move(list)) {}; void update(); Vector m_list; + WeakPtr m_url; }; }