123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- /*
- * Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <LibURL/Parser.h>
- #include <LibWeb/DOM/Document.h>
- #include <LibWeb/HTML/HTMLHyperlinkElementUtils.h>
- #include <LibWeb/HTML/Navigable.h>
- namespace Web::HTML {
- HTMLHyperlinkElementUtils::~HTMLHyperlinkElementUtils() = default;
- // https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
- void HTMLHyperlinkElementUtils::reinitialize_url() const
- {
- // 1. If element's url is non-null, its scheme is "blob", and its cannot-be-a-basFe-URL is true, terminate these steps.
- if (m_url.has_value() && m_url->scheme() == "blob"sv && m_url->cannot_be_a_base_url())
- return;
- // 2. Set the url.
- const_cast<HTMLHyperlinkElementUtils*>(this)->set_the_url();
- }
- // https://html.spec.whatwg.org/multipage/links.html#concept-hyperlink-url-set
- void HTMLHyperlinkElementUtils::set_the_url()
- {
- // 1. If this element's href content attribute is absent, set this element's url to null.
- auto href_content_attribute = hyperlink_element_utils_href();
- if (!href_content_attribute.has_value()) {
- m_url = {};
- hyperlink_element_utils_element().invalidate_style(DOM::StyleInvalidationReason::HTMLHyperlinkElementHrefChange);
- return;
- }
- // 2. Otherwise, parse this element's href content attribute value relative to this element's node document.
- // If parsing is successful, set this element's url to the result; otherwise, set this element's url to null.
- m_url = hyperlink_element_utils_document().parse_url(*href_content_attribute);
- hyperlink_element_utils_element().invalidate_style(DOM::StyleInvalidationReason::HTMLHyperlinkElementHrefChange);
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-origin
- String HTMLHyperlinkElementUtils::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 String {};
- // 3. Return the serialization of this element's url's origin.
- return m_url->origin().serialize();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
- String HTMLHyperlinkElementUtils::protocol() const
- {
- // 1. Reinitialize url.
- reinitialize_url();
- // 2. If this element's url is null, return ":".
- if (!m_url.has_value())
- return ":"_string;
- // 3. Return this element's url's scheme, followed by ":".
- return MUST(String::formatted("{}:", m_url->scheme()));
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol
- void HTMLHyperlinkElementUtils::set_protocol(StringView 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.
- (void)URL::Parser::basic_parse(MUST(String::formatted("{}:", protocol)), {}, &m_url.value(), URL::Parser::State::SchemeStart);
- // 4. Update href.
- update_href();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
- String HTMLHyperlinkElementUtils::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 String {};
- // 3. Return this element's url's username.
- return m_url->username();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-username
- void HTMLHyperlinkElementUtils::set_username(StringView 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
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return url's password.
- return url->password();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-password
- void HTMLHyperlinkElementUtils::set_password(StringView 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
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. If url's port is null, return url's host, serialized.
- if (!url->port().has_value())
- return url->serialized_host();
- // 5. Return url's host, serialized, followed by ":" and url's port, serialized.
- return MUST(String::formatted("{}:{}", url->serialized_host(), url->port().value()));
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-host
- void HTMLHyperlinkElementUtils::set_host(StringView 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's cannot-be-a-base-URL is true, then return.
- if (!url.has_value() || url->cannot_be_a_base_url())
- 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
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return url's host, serialized.
- return url->serialized_host();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-hostname
- void HTMLHyperlinkElementUtils::set_hostname(StringView 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's cannot-be-a-base-URL is true, then return.
- if (!url.has_value() || url->cannot_be_a_base_url())
- 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
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return url's port, serialized.
- return String::number(url->port().value());
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-port
- void HTMLHyperlinkElementUtils::set_port(StringView 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
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return the result of URL path serializing url.
- return m_url->serialize_path();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-pathname
- void HTMLHyperlinkElementUtils::set_pathname(StringView pathname)
- {
- // 1. Reinitialize url.
- reinitialize_url();
- // 2. Let url be this element's url.
- // 3. If url is null or url's cannot-be-a-base-URL is true, then return.
- if (!m_url.has_value() || m_url->cannot_be_a_base_url())
- return;
- // 4. Set url's path to the empty list.
- m_url->set_paths({});
- // 5. Basic URL parse the given value, with url as url and path start state as state override.
- (void)URL::Parser::basic_parse(pathname, {}, &m_url.value(), URL::Parser::State::PathStart);
- // 6. Update href.
- update_href();
- }
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return "?", followed by url's query.
- return MUST(String::formatted("?{}", m_url->query()));
- }
- void HTMLHyperlinkElementUtils::set_search(StringView 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('?'));
- // 2. Set url's query to the empty string.
- m_url->set_query(String {});
- // 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();
- }
- String HTMLHyperlinkElementUtils::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 String {};
- // 4. Return "#", followed by url's fragment.
- return MUST(String::formatted("#{}", *m_url->fragment()));
- }
- void HTMLHyperlinkElementUtils::set_hash(StringView 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('#'));
- // 2. Set url's fragment to the empty string.
- m_url->set_fragment(String {});
- // 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#dom-hyperlink-href
- String HTMLHyperlinkElementUtils::href() const
- {
- // 1. Reinitialize url.
- reinitialize_url();
- // 2. Let url be this element's url.
- auto const& url = m_url;
- // 3. If url is null and this element has no href content attribute, return the empty string.
- auto href_content_attribute = hyperlink_element_utils_href();
- if (!url.has_value() && !href_content_attribute.has_value())
- return String {};
- // 4. Otherwise, if url is null, return this element's href content attribute's value.
- if (!url->is_valid())
- return href_content_attribute.release_value();
- // 5. Return url, serialized.
- return url->serialize();
- }
- // https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
- WebIDL::ExceptionOr<void> HTMLHyperlinkElementUtils::set_href(String href)
- {
- // The href attribute's setter must set this element's href content attribute's value to the given value.
- return set_hyperlink_element_utils_href(move(href));
- }
- // https://html.spec.whatwg.org/multipage/links.html#update-href
- void HTMLHyperlinkElementUtils::update_href()
- {
- // To update href, set the element's href content attribute's value to the element's url, serialized.
- MUST(set_hyperlink_element_utils_href(m_url->serialize()));
- }
- bool HTMLHyperlinkElementUtils::cannot_navigate() const
- {
- // An element element cannot navigate if one of the following is true:
- // 1. element's node document is not fully active
- auto const& document = const_cast<HTMLHyperlinkElementUtils*>(this)->hyperlink_element_utils_document();
- if (!document.is_fully_active())
- return true;
- // 2. element is not an a element and is not connected.
- if (!hyperlink_element_utils_is_html_anchor_element() && !hyperlink_element_utils_is_connected())
- return true;
- return false;
- }
- // https://html.spec.whatwg.org/multipage/links.html#following-hyperlinks-2
- void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_suffix, UserNavigationInvolvement user_involvement)
- {
- // 1. If subject cannot navigate, then return.
- if (cannot_navigate())
- return;
- // 2. Let replace be false.
- [[maybe_unused]] auto replace = false;
- // 3. Let targetAttributeValue be the empty string.
- String target_attribute_value;
- // 4. If subject is an a or area element, then set targetAttributeValue to the result of getting an element's target given subject.
- target_attribute_value = hyperlink_element_utils_get_an_elements_target();
- // 5. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue.
- auto noopener = hyperlink_element_utils_get_an_elements_noopener(target_attribute_value);
- // 6. Let targetNavigable be the first return value of applying the rules for choosing a navigable given
- // targetAttributeValue, subject's node navigable, and noopener.
- auto target_navigable = hyperlink_element_utils_document().navigable()->choose_a_navigable(target_attribute_value, noopener).navigable;
- // 7. If targetNavigable is null, then return.
- if (!target_navigable)
- return;
- // 8. Let urlString be the result of encoding-parsing-and-serializing a URL given subject's href attribute value,
- // relative to subject's node document.
- auto url_string = hyperlink_element_utils_document().encoding_parse_and_serialize_url(href());
- // 9. If urlString is failure, then return.
- if (!url_string.has_value())
- return;
- // 10. If hyperlinkSuffix is non-null, then append it to urlString.
- if (hyperlink_suffix.has_value())
- url_string = MUST(String::formatted("{}{}", *url_string, *hyperlink_suffix));
- // 11. Let referrerPolicy be the current state of subject's referrerpolicy content attribute.
- auto referrer_policy = ReferrerPolicy::from_string(hyperlink_element_utils_referrerpolicy().value_or({})).value_or(ReferrerPolicy::ReferrerPolicy::EmptyString);
- // FIXME: 12. If subject's link types includes the noreferrer keyword, then set referrerPolicy to "no-referrer".
- // 13. Navigate targetNavigable to urlString using subject's node document, with referrerPolicy set to referrerPolicy and userInvolvement set to userInvolvement.
- MUST(target_navigable->navigate({ .url = *url_string, .source_document = hyperlink_element_utils_document(), .referrer_policy = referrer_policy, .user_involvement = user_involvement }));
- }
- }
|