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
Notes: sideshowbarker 2024-07-18 03:59:21 +09:00
8 changed files with 103 additions and 1 deletions

View file

@ -227,6 +227,8 @@
#include <LibWeb/Bindings/TextPrototype.h>
#include <LibWeb/Bindings/UIEventConstructor.h>
#include <LibWeb/Bindings/UIEventPrototype.h>
#include <LibWeb/Bindings/URLConstructor.h>
#include <LibWeb/Bindings/URLPrototype.h>
#include <LibWeb/Bindings/URLSearchParamsConstructor.h>
#include <LibWeb/Bindings/URLSearchParamsPrototype.h>
#include <LibWeb/Bindings/WebSocketConstructor.h>
@ -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) \

View file

@ -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(

View file

@ -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;

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;
}
}

View file

@ -7,5 +7,36 @@
#pragma once
#include <AK/String.h>
#include <AK/URL.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
class URL : public Bindings::Wrappable
, public RefCounted<URL>
, public Weakable<URL> {
public:
using WrapperType = Bindings::URLWrapper;
static NonnullRefPtr<URL> create(AK::URL url, NonnullRefPtr<URLSearchParams> query)
{
return adopt_ref(*new URL(move(url), move(query)));
}
static DOM::ExceptionOr<NonnullRefPtr<URL>> create_with_global_object(Bindings::WindowObject&, const String& url, const String& base);
void set_query(Badge<URLSearchParams>, String query) { m_url.set_query(move(query)); }
private:
explicit URL(AK::URL url, NonnullRefPtr<URLSearchParams> query)
: m_url(move(url))
, m_query(move(query)) {};
AK::URL m_url;
NonnullRefPtr<URLSearchParams> m_query;
};
}

View file

@ -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();
};

View file

@ -7,6 +7,7 @@
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibWeb/URL/URL.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
@ -97,11 +98,16 @@ void URLSearchParams::append(String const& name, String const& value)
void URLSearchParams::update()
{
// TODO
// 1. If querys URL object is null, then return.
if (m_url.is_null())
return;
// 2. Let serializedQuery be the serialization of querys 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 querys URL objects URLs query to serializedQuery.
m_url->set_query({}, move(serialized_query));
}
void URLSearchParams::delete_(String const& name)

View file

@ -43,12 +43,15 @@ public:
String to_string();
private:
friend class URL;
explicit URLSearchParams(Vector<QueryParam> list)
: m_list(move(list)) {};
void update();
Vector<QueryParam> m_list;
WeakPtr<URL> m_url;
};
}