From 1841fbd3e42555234056b7776560cd8c8fbf7ac0 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 14 Sep 2021 00:13:32 +0300 Subject: [PATCH] LibWeb: Add the URL::href attribute and URL::to_json method --- Userland/Libraries/LibWeb/URL/URL.cpp | 31 +++++++++++++++++++++++++++ Userland/Libraries/LibWeb/URL/URL.h | 5 +++++ Userland/Libraries/LibWeb/URL/URL.idl | 4 ++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 2998cc60bf..c790aad675 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -44,4 +44,35 @@ DOM::ExceptionOr> URL::create_with_global_object(Bindings::Wi return result_url; } +String URL::href() const +{ + // return the serialization of this’s URL. + return m_url.serialize(); +} + +String URL::to_json() const +{ + // return the serialization of this’s URL. + return m_url.serialize(); +} + +DOM::ExceptionOr URL::set_href(String const& href) +{ + // 1. Let parsedURL be the result of running the basic URL parser on the given value. + AK::URL parsed_url = href; + // 2. If parsedURL is failure, then throw a TypeError. + if (!parsed_url.is_valid()) + return DOM::SimpleException { DOM::SimpleExceptionType::TypeError, "Invalid URL" }; + // 3. Set this’s URL to parsedURL. + m_url = move(parsed_url); + // 4. Empty this’s query object’s list. + m_query->m_list.clear(); + // 5. Let query be this’s URL’s query. + auto& query = m_url.query(); + // 6. If query is non-null, then set this’s query object’s list to the result of parsing query. + if (!query.is_null()) + m_query->m_list = url_decode(query); + return {}; +} + } diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index adcdb00fb4..8b7588d748 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -28,6 +28,11 @@ public: static DOM::ExceptionOr> create_with_global_object(Bindings::WindowObject&, const String& url, const String& base); + String href() const; + DOM::ExceptionOr set_href(String const&); + + String to_json() const; + void set_query(Badge, String query) { m_url.set_query(move(query)); } private: diff --git a/Userland/Libraries/LibWeb/URL/URL.idl b/Userland/Libraries/LibWeb/URL/URL.idl index d4b647626d..933b03a2c0 100644 --- a/Userland/Libraries/LibWeb/URL/URL.idl +++ b/Userland/Libraries/LibWeb/URL/URL.idl @@ -1,7 +1,7 @@ interface URL { constructor(USVString url, optional USVString base); - // TODO: stringifier attribute USVString href; + stringifier attribute USVString href; // TODO: readonly attribute USVString origin; // TODO: attribute USVString protocol; // TODO: attribute USVString username; @@ -14,5 +14,5 @@ interface URL { // TODO: [SameObject] readonly attribute URLSearchParams searchParams; // TODO: attribute USVString hash; - // TODO: USVString toJSON(); + USVString toJSON(); };