mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:57:35 +00:00
LibWeb: Add the URL::host, URL::hostname & URL:port attributes
This commit is contained in:
parent
e89320887e
commit
7f9818bcbc
3 changed files with 82 additions and 3 deletions
|
@ -5,6 +5,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/URLParser.h>
|
||||||
#include <LibWeb/URL/URL.h>
|
#include <LibWeb/URL/URL.h>
|
||||||
|
|
||||||
namespace Web::URL {
|
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));
|
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
|
URLSearchParams const* URL::search_params() const
|
||||||
{
|
{
|
||||||
return m_query;
|
return m_query;
|
||||||
|
|
|
@ -39,6 +39,15 @@ public:
|
||||||
String password() const;
|
String password() const;
|
||||||
void set_password(String 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;
|
URLSearchParams const* search_params() const;
|
||||||
|
|
||||||
String to_json() const;
|
String to_json() const;
|
||||||
|
|
|
@ -6,9 +6,9 @@ interface URL {
|
||||||
// TODO: attribute USVString protocol;
|
// TODO: attribute USVString protocol;
|
||||||
attribute USVString username;
|
attribute USVString username;
|
||||||
attribute USVString password;
|
attribute USVString password;
|
||||||
// TODO: attribute USVString host;
|
attribute USVString host;
|
||||||
// TODO: attribute USVString hostname;
|
attribute USVString hostname;
|
||||||
// TODO: attribute USVString port;
|
attribute USVString port;
|
||||||
// TODO: attribute USVString pathname;
|
// TODO: attribute USVString pathname;
|
||||||
// TODO: attribute USVString search;
|
// TODO: attribute USVString search;
|
||||||
[SameObject] readonly attribute URLSearchParams searchParams;
|
[SameObject] readonly attribute URLSearchParams searchParams;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue