Truly independent web browser
/*
* Copyright (c) 2021, Andreas Kling <[email protected]>
* Copyright (c) 2024-2025, Shannon Booth <[email protected]>
* Copyright (c) 2026, Sam Atkins <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Utf16String.h>
#include <LibURL/Parser.h>
#include <LibWeb/CSS/Invalidation/LinkInvalidator.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/HTML/AttributeNames.h>
#include <LibWeb/HTML/HyperlinkElementUtils.h>
#include <LibWeb/Infra/SerializedURL.h>
namespace Web::HTML {
HyperlinkElementUtils::~HyperlinkElementUtils() = default;
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
Utf16String HyperlinkElementUtils::origin() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. If this element's url is null, return the empty string.
if (!m_url.has_value())
return Utf16String {};
// 3. Return the serialization of this element's url's origin.
return utf16_string_from_url_ascii(m_url->origin().serialize());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
Utf16String HyperlinkElementUtils::protocol() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. If this element's url is null, return ":".
if (!m_url.has_value())
return ":"_utf16;
// 3. Return this element's url's scheme, followed by ":".
return Utf16String::formatted("{}:", m_url->scheme());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
void HyperlinkElementUtils::set_protocol(Utf16View protocol)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. If this element's url is null, terminate these steps.
if (!m_url.has_value())
return;
// 3. Basic URL parse the given value, followed by ":", with this element's url as url and scheme start state as state override.
auto protocol_with_colon = Utf16String::formatted("{}:", protocol);
(void)URL::Parser::basic_parse(protocol_with_colon.utf16_view(), m_url.value(), URL::Parser::State::SchemeStart);
// 4. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
Utf16String HyperlinkElementUtils::username() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. If this element's url is null, return the empty string.
if (!m_url.has_value())
return Utf16String {};
// 3. Return this element's url's username.
return utf16_string_from_url_ascii(m_url->username());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
void HyperlinkElementUtils::set_username(Utf16View username)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null or url cannot have a username/password/port, then return.
if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
return;
// 4. Set the username given this’s URL and the given value.
url->set_username(username);
// 5. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
Utf16String HyperlinkElementUtils::password() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null, then return the empty string.
if (!url.has_value())
return Utf16String {};
// 4. Return url's password.
return utf16_string_from_url_ascii(url->password());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
void HyperlinkElementUtils::set_password(Utf16View password)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null or url cannot have a username/password/port, then return.
if (!url.has_value() || url->cannot_have_a_username_or_password_or_port())
return;
// 4. Set the password, given url and the given value.
url->set_password(password);
// 5. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
Utf16String HyperlinkElementUtils::host() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto const& url = m_url;
// 3. If url or url's host is null, return the empty string.
if (!url.has_value() || !url->host().has_value())
return Utf16String {};
// 4. If url's port is null, return url's host, serialized.
if (!url->port().has_value())
return utf16_string_from_url_ascii(url->serialized_host());
// 5. Return url's host, serialized, followed by ":" and url's port, serialized.
return utf16_string_from_url_ascii_host_and_port(url->serialized_host(), url->port().value());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
void HyperlinkElementUtils::set_host(Utf16View host)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null or url has an opaque path, then return.
if (!url.has_value() || url->has_an_opaque_path())
return;
// 4. Basic URL parse the given value, with url as url and host state as state override.
(void)URL::Parser::basic_parse(host, url.value(), URL::Parser::State::Host);
// 5. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-hostname
Utf16String HyperlinkElementUtils::hostname() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto url = m_url;
// 3. If url or url's host is null, return the empty string.
if (!url.has_value() || !url->host().has_value())
return Utf16String {};
// 4. Return url's host, serialized.
return utf16_string_from_url_ascii(url->serialized_host());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-hostname
void HyperlinkElementUtils::set_hostname(Utf16View hostname)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null or url has an opaque path, then return.
if (!url.has_value() || url->has_an_opaque_path())
return;
// 4. Basic URL parse the given value, with url as url and hostname state as state override.
(void)URL::Parser::basic_parse(hostname, url.value(), URL::Parser::State::Hostname);
// 5. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
Utf16String HyperlinkElementUtils::port() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url or url's port is null, return the empty string.
if (!url.has_value() || !url->port().has_value())
return Utf16String {};
// 4. Return url's port, serialized.
return Utf16String::number(url->port().value());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
void HyperlinkElementUtils::set_port(Utf16View port)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null or url cannot have a username/password/port, then return.
if (!m_url.has_value() || m_url->cannot_have_a_username_or_password_or_port())
return;
// 4. If the given value is the empty string, then set url's port to null.
if (port.is_empty()) {
m_url->set_port({});
}
// 5. Otherwise, basic URL parse the given value, with url as url and port state as state override.
else {
(void)URL::Parser::basic_parse(port, m_url.value(), URL::Parser::State::Port);
}
// 6. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
Utf16String HyperlinkElementUtils::pathname() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null, return the empty string.
if (!m_url.has_value())
return Utf16String {};
// 4. Return the result of URL path serializing url.
return utf16_string_from_url_ascii(m_url->serialize_path());
}
// https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
void HyperlinkElementUtils::set_pathname(Utf16View pathname)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
auto& url = m_url;
// 3. If url is null or url has an opaque path, then return.
if (!url.has_value() || url->has_an_opaque_path())
return;
// 4. Set url's path to the empty list.
url->set_path({});
// 5. Basic URL parse the given value, with url as url and path start state as state override.
(void)URL::Parser::basic_parse(pathname, url.value(), URL::Parser::State::PathStart);
// 6. Update href.
update_href();
}
Utf16String HyperlinkElementUtils::search() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null, or url's query is either null or the empty string, return the empty string.
if (!m_url.has_value() || !m_url->query().has_value() || m_url->query()->is_empty())
return Utf16String {};
// 4. Return "?", followed by url's query.
return utf16_string_from_url_ascii_with_prefix('?', *m_url->query());
}
void HyperlinkElementUtils::set_search(Utf16View search)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null, terminate these steps.
if (!m_url.has_value())
return;
// 4. If the given value is the empty string, set url's query to null.
if (search.is_empty()) {
m_url->set_query({});
} else {
// 5. Otherwise:
// 1. Let input be the given value with a single leading "?" removed, if any.
auto input = search.substring_view(search.starts_with(u"?"sv));
// 2. Set url's query to the empty string.
m_url->set_query(""sv);
// 3. Basic URL parse input, with null, this element's node document's document's character encoding, url as url, and query state as state override.
(void)URL::Parser::basic_parse(input, m_url.value(), URL::Parser::State::Query);
}
// 6. Update href.
update_href();
}
Utf16String HyperlinkElementUtils::hash() const
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null, or url's fragment is either null or the empty string, return the empty string.
if (!m_url.has_value() || !m_url->fragment().has_value() || m_url->fragment()->is_empty())
return Utf16String {};
// 4. Return "#", followed by url's fragment.
return utf16_string_from_url_ascii_with_prefix('#', *m_url->fragment());
}
void HyperlinkElementUtils::set_hash(Utf16View hash)
{
// 1. Reinitialize url.
reinitialize_url();
// 2. Let url be this element's url.
// 3. If url is null, then return.
if (!m_url.has_value())
return;
// 4. If the given value is the empty string, set url's fragment to null.
if (hash.is_empty()) {
m_url->set_fragment({});
} else {
// 5. Otherwise:
// 1. Let input be the given value with a single leading "#" removed, if any.
auto input = hash.substring_view(hash.starts_with(u"#"sv));
// 2. Set url's fragment to the empty string.
m_url->set_fragment(""sv);
// 3. Basic URL parse input, with url as url and fragment state as state override.
(void)URL::Parser::basic_parse(input, m_url.value(), URL::Parser::State::Fragment);
}
// 6. Update href.
update_href();
}
// https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
void HyperlinkElementUtils::reinitialize_url() const
{
// 1. If the element's url is non-null, its scheme is "blob", and it has an opaque path, then terminate these steps.
if (m_url.has_value() && m_url->scheme() == "blob"sv && m_url->has_an_opaque_path())
return;
// 2. Set the url.
const_cast<HyperlinkElementUtils*>(this)->set_the_url();
}
void HyperlinkElementUtils::href_content_attribute_changed()
{
m_url = {};
m_should_invalidate_style_after_url_change = false;
hyperlink_element_utils_element().update_hover_link();
CSS::Invalidation::invalidate_style_after_hyperlink_state_change(hyperlink_element_utils_element());
}
// https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
void HyperlinkElementUtils::set_the_url()
{
auto should_invalidate_style = m_should_invalidate_style_after_url_change;
m_should_invalidate_style_after_url_change = true;
ScopeGuard invalidate_style_if_needed = [old_url = m_url, should_invalidate_style, this] {
if (should_invalidate_style && m_url != old_url)
CSS::Invalidation::invalidate_style_after_hyperlink_state_change(hyperlink_element_utils_element());
};
auto& element = hyperlink_element_utils_element();
// 1. Set this element's url to null.
m_url = {};
// 2. If this element's href content attribute is absent, then return.
auto href_content_attribute = element.attribute(HTML::AttributeNames::href);
if (!href_content_attribute.has_value()) {
return;
}
// 3. Let url be the result of encoding-parsing a URL given this element's href content attribute's value, relative to this element's node document.
auto url = element.document().encoding_parse_url(*href_content_attribute);
// 4. If url is not failure, then set this element's url to url.
if (url.has_value())
m_url = url.release_value();
}
// https://html.spec.whatwg.org/multipage/links.html#api-for-hyperlink-elements:extract-an-origin
Optional<URL::Origin> HyperlinkElementUtils::hyperlink_element_utils_extract_an_origin() const
{
reinitialize_url();
// 1. If this's url is null, then return null.
if (!m_url.has_value())
return {};
// 2. Return this's url's origin.
return m_url->origin();
}
}