diff --git a/Userland/Libraries/LibWeb/URL/URL.cpp b/Userland/Libraries/LibWeb/URL/URL.cpp index 949910afae..40e6bdbec4 100644 --- a/Userland/Libraries/LibWeb/URL/URL.cpp +++ b/Userland/Libraries/LibWeb/URL/URL.cpp @@ -5,6 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include namespace Web::URL { @@ -111,6 +112,75 @@ void URL::set_password(String const& password) m_url.set_password(AK::URL::percent_encode(password, AK::URL::PercentEncodeSet::Userinfo)); } +String URL::host() const +{ + // 1. Let url be this’s URL. + auto& url = m_url; + // 2. If url’s host is null, then return the empty string. + if (url.host().is_null()) + return String::empty(); + // 3. If url’s port is null, return url’s host, serialized. + if (!url.port().has_value()) + return url.host(); + // 4. Return url’s host, serialized, followed by U+003A (:) and url’s port, serialized. + return String::formatted("{}:{}", url.host(), *url.port()); +} + +void URL::set_host(const String& host) +{ + // 1. If this’s URL’s cannot-be-a-base-URL is true, then return. + if (m_url.cannot_be_a_base_url()) + return; + // 2. Basic URL parse the given value with this’s URL as url and host state as state override. + auto result_url = URLParser::parse(host, nullptr, m_url, URLParser::State::Host); + if (result_url.is_valid()) + m_url = move(result_url); +} + +String URL::hostname() const +{ + // 1. If this’s URL’s host is null, then return the empty string. + if (m_url.host().is_null()) + return String::empty(); + // 2. Return this’s URL’s host, serialized. + return m_url.host(); +} + +void URL::set_hostname(String const& hostname) +{ + // 1. If this’s URL’s cannot-be-a-base-URL is true, then return. + if (m_url.cannot_be_a_base_url()) + return; + // 2. Basic URL parse the given value with this’s URL as url and hostname state as state override. + auto result_url = URLParser::parse(hostname, nullptr, m_url, URLParser::State::Hostname); + if (result_url.is_valid()) + m_url = move(result_url); +} + +String URL::port() const +{ + // 1. If this’s URL’s port is null, then return the empty string. + if (!m_url.port().has_value()) + return {}; + + // 2. Return this’s URL’s port, serialized. + return String::formatted("{}", *m_url.port()); +} + +void URL::set_port(String const& port) +{ + // 1. If this’s URL cannot have a username/password/port, then return. + if (m_url.cannot_have_a_username_or_password_or_port()) + return; + // 2. If the given value is the empty string, then set this’s URL’s port to null. + if (port.is_empty()) + m_url.set_port({}); + // 3. Otherwise, basic URL parse the given value with this’s URL as url and port state as state override. + auto result_url = URLParser::parse(port, nullptr, m_url, URLParser::State::Port); + if (result_url.is_valid()) + m_url = move(result_url); +} + URLSearchParams const* URL::search_params() const { return m_query; diff --git a/Userland/Libraries/LibWeb/URL/URL.h b/Userland/Libraries/LibWeb/URL/URL.h index 0b1b2461ec..cb6df8ae33 100644 --- a/Userland/Libraries/LibWeb/URL/URL.h +++ b/Userland/Libraries/LibWeb/URL/URL.h @@ -39,6 +39,15 @@ public: String password() const; void set_password(String const&); + String host() const; + void set_host(String const&); + + String hostname() const; + void set_hostname(String const&); + + String port() const; + void set_port(String const&); + URLSearchParams const* search_params() const; String to_json() const; diff --git a/Userland/Libraries/LibWeb/URL/URL.idl b/Userland/Libraries/LibWeb/URL/URL.idl index 7ea731e04f..fc668ebd3d 100644 --- a/Userland/Libraries/LibWeb/URL/URL.idl +++ b/Userland/Libraries/LibWeb/URL/URL.idl @@ -6,9 +6,9 @@ interface URL { // TODO: attribute USVString protocol; attribute USVString username; attribute USVString password; - // TODO: attribute USVString host; - // TODO: attribute USVString hostname; - // TODO: attribute USVString port; + attribute USVString host; + attribute USVString hostname; + attribute USVString port; // TODO: attribute USVString pathname; // TODO: attribute USVString search; [SameObject] readonly attribute URLSearchParams searchParams;