mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-27 10:07:15 +02:00
LibWeb: Remove unnecessary methods from HTMLHyperlinkElementUtils
These could all be implemented in terms of `hyperlink_element_utils_element()`.
This commit is contained in:
committed by
Jelle Raaijmakers
parent
ba4a58fc0a
commit
0448731059
Notes:
github-actions[bot]
2026-01-13 09:07:14 +00:00
Author: https://github.com/tcl3 Commit: https://github.com/LadybirdBrowser/ladybird/commit/04487310595 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/7445 Reviewed-by: https://github.com/gmta ✅ Reviewed-by: https://github.com/shannonbooth ✅
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <LibURL/Parser.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/HTML/AttributeNames.h>
|
||||
#include <LibWeb/HTML/HTMLElement.h>
|
||||
#include <LibWeb/HTML/HTMLHyperlinkElementUtils.h>
|
||||
#include <LibWeb/HTML/Navigable.h>
|
||||
|
||||
@@ -41,17 +43,19 @@ void HTMLHyperlinkElementUtils::set_the_url()
|
||||
}
|
||||
};
|
||||
|
||||
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 = hyperlink_element_utils_href();
|
||||
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 = hyperlink_element_utils_document().encoding_parse_url(*href_content_attribute);
|
||||
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())
|
||||
@@ -433,7 +437,7 @@ String HTMLHyperlinkElementUtils::href() const
|
||||
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();
|
||||
auto href_content_attribute = hyperlink_element_utils_element().attribute(HTML::AttributeNames::href);
|
||||
if (!url.has_value() && !href_content_attribute.has_value())
|
||||
return String {};
|
||||
|
||||
@@ -449,14 +453,14 @@ String HTMLHyperlinkElementUtils::href() const
|
||||
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.
|
||||
set_hyperlink_element_utils_href(move(href));
|
||||
hyperlink_element_utils_element().set_attribute_value(HTML::AttributeNames::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.
|
||||
set_hyperlink_element_utils_href(m_url->serialize());
|
||||
hyperlink_element_utils_element().set_attribute_value(HTML::AttributeNames::href, m_url->serialize());
|
||||
}
|
||||
|
||||
bool HTMLHyperlinkElementUtils::cannot_navigate() const
|
||||
@@ -464,12 +468,12 @@ 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())
|
||||
auto const& element = hyperlink_element_utils_element();
|
||||
if (!element.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())
|
||||
if (!element.is_html_anchor_element() && !element.is_connected())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
@@ -482,14 +486,17 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_
|
||||
if (cannot_navigate())
|
||||
return;
|
||||
|
||||
auto& element = hyperlink_element_utils_element();
|
||||
|
||||
// 2. Let targetAttributeValue be the empty string.
|
||||
String target_attribute_value;
|
||||
|
||||
// 3. 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();
|
||||
if (element.is_html_anchor_element() || element.is_html_area_element())
|
||||
target_attribute_value = hyperlink_element_utils_get_an_elements_target();
|
||||
|
||||
// 4. Let urlRecord be the result of encoding-parsing a URL given subject's href attribute value, relative to subject's node document.
|
||||
auto url_record = hyperlink_element_utils_document().encoding_parse_url(href());
|
||||
auto url_record = element.document().encoding_parse_url(href());
|
||||
|
||||
// 5. If urlRecord is failure, then return.
|
||||
if (!url_record.has_value())
|
||||
@@ -500,7 +507,7 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_
|
||||
|
||||
// 7. 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;
|
||||
auto target_navigable = element.document().navigable()->choose_a_navigable(target_attribute_value, noopener).navigable;
|
||||
|
||||
// 8. If targetNavigable is null, then return.
|
||||
if (!target_navigable)
|
||||
@@ -514,14 +521,14 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<String> hyperlink_
|
||||
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);
|
||||
auto referrer_policy = ReferrerPolicy::from_string(element.attribute(HTML::AttributeNames::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.
|
||||
auto url = URL::Parser::basic_parse(url_string);
|
||||
VERIFY(url.has_value());
|
||||
MUST(target_navigable->navigate({ .url = url.release_value(), .source_document = hyperlink_element_utils_document(), .referrer_policy = referrer_policy, .user_involvement = user_involvement }));
|
||||
MUST(target_navigable->navigate({ .url = url.release_value(), .source_document = element.document(), .referrer_policy = referrer_policy, .user_involvement = user_involvement }));
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/links.html#api-for-a-and-area-elements:extract-an-origin
|
||||
|
||||
Reference in New Issue
Block a user