1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 14:38:12 +00:00

LibWeb: Move get_an_elements_{target,noopener} to HTMLElement

This will be used by form submission on `<form>` elements.
This commit is contained in:
Luke Wilde 2023-06-18 16:22:10 +01:00 committed by Andreas Kling
parent c9c1ddd0bb
commit 995df8f565
6 changed files with 65 additions and 48 deletions

View file

@ -52,12 +52,18 @@ private:
virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) override; virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) override;
virtual bool hyperlink_element_utils_is_html_anchor_element() const final { return true; } virtual bool hyperlink_element_utils_is_html_anchor_element() const final { return true; }
virtual bool hyperlink_element_utils_is_connected() const final { return is_connected(); } virtual bool hyperlink_element_utils_is_connected() const final { return is_connected(); }
virtual DeprecatedString hyperlink_element_utils_target() const final { return target(); }
virtual DeprecatedString hyperlink_element_utils_rel() const final { return rel(); }
virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) override virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) override
{ {
queue_an_element_task(source, move(steps)); queue_an_element_task(source, move(steps));
} }
virtual DeprecatedString hyperlink_element_utils_get_an_elements_target() const override
{
return get_an_elements_target();
}
virtual TokenizedFeature::NoOpener hyperlink_element_utils_get_an_elements_noopener(StringView target) const override
{
return get_an_elements_noopener(target);
}
virtual Optional<ARIA::Role> default_role() const override; virtual Optional<ARIA::Role> default_role() const override;
}; };

View file

@ -35,12 +35,18 @@ private:
virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) override; virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) override;
virtual bool hyperlink_element_utils_is_html_anchor_element() const override { return false; } virtual bool hyperlink_element_utils_is_html_anchor_element() const override { return false; }
virtual bool hyperlink_element_utils_is_connected() const override { return is_connected(); } virtual bool hyperlink_element_utils_is_connected() const override { return is_connected(); }
virtual DeprecatedString hyperlink_element_utils_target() const override { return ""; }
virtual DeprecatedString hyperlink_element_utils_rel() const override { return ""; }
virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) override virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) override
{ {
queue_an_element_task(source, move(steps)); queue_an_element_task(source, move(steps));
} }
virtual DeprecatedString hyperlink_element_utils_get_an_elements_target() const override
{
return get_an_elements_target();
}
virtual TokenizedFeature::NoOpener hyperlink_element_utils_get_an_elements_noopener(StringView target) const override
{
return get_an_elements_noopener(target);
}
virtual Optional<ARIA::Role> default_role() const override; virtual Optional<ARIA::Role> default_role() const override;
}; };

View file

@ -17,11 +17,14 @@
#include <LibWeb/HTML/Focus.h> #include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/HTMLAnchorElement.h> #include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLAreaElement.h> #include <LibWeb/HTML/HTMLAreaElement.h>
#include <LibWeb/HTML/HTMLBaseElement.h>
#include <LibWeb/HTML/HTMLBodyElement.h> #include <LibWeb/HTML/HTMLBodyElement.h>
#include <LibWeb/HTML/HTMLElement.h> #include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/NavigableContainer.h> #include <LibWeb/HTML/NavigableContainer.h>
#include <LibWeb/HTML/VisibilityState.h> #include <LibWeb/HTML/VisibilityState.h>
#include <LibWeb/HTML/Window.h> #include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Layout/Box.h> #include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/BreakNode.h> #include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/TextNode.h> #include <LibWeb/Layout/TextNode.h>
@ -426,4 +429,41 @@ Optional<ARIA::Role> HTMLElement::default_role() const
return {}; return {};
} }
// https://html.spec.whatwg.org/multipage/semantics.html#get-an-element's-target
DeprecatedString HTMLElement::get_an_elements_target() const
{
// To get an element's target, given an a, area, or form element element, run these steps:
// 1. If element has a target attribute, then return that attribute's value.
if (has_attribute(AttributeNames::target))
return attribute(AttributeNames::target);
// FIXME: 2. If element's node document contains a base element with a
// target attribute, then return the value of the target attribute of the
// first such base element.
// 3. Return the empty string.
return DeprecatedString::empty();
}
// https://html.spec.whatwg.org/multipage/links.html#get-an-element's-noopener
TokenizedFeature::NoOpener HTMLElement::get_an_elements_noopener(StringView target) const
{
// To get an element's noopener, given an a, area, or form element element and a string target:
auto rel = attribute(HTML::AttributeNames::rel).to_lowercase();
auto link_types = rel.view().split_view_if(Infra::is_ascii_whitespace);
// 1. If element's link types include the noopener or noreferrer keyword, then return true.
if (link_types.contains_slow("noopener"sv) || link_types.contains_slow("noreferrer"sv))
return TokenizedFeature::NoOpener::Yes;
// 2. If element's link types do not include the opener keyword and
// target is an ASCII case-insensitive match for "_blank", then return true.
if (!link_types.contains_slow("opener"sv) && Infra::is_ascii_case_insensitive_match(target, "_blank"sv))
return TokenizedFeature::NoOpener::Yes;
// 3. Return false.
return TokenizedFeature::NoOpener::No;
}
} }

View file

@ -9,6 +9,7 @@
#include <LibWeb/DOM/Element.h> #include <LibWeb/DOM/Element.h>
#include <LibWeb/HTML/EventNames.h> #include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/GlobalEventHandlers.h> #include <LibWeb/HTML/GlobalEventHandlers.h>
#include <LibWeb/HTML/TokenizedFeatures.h>
namespace Web::HTML { namespace Web::HTML {
@ -61,6 +62,9 @@ public:
virtual Optional<ARIA::Role> default_role() const override; virtual Optional<ARIA::Role> default_role() const override;
DeprecatedString get_an_elements_target() const;
TokenizedFeature::NoOpener get_an_elements_noopener(StringView target) const;
protected: protected:
HTMLElement(DOM::Document&, DOM::QualifiedName); HTMLElement(DOM::Document&, DOM::QualifiedName);

View file

@ -7,8 +7,6 @@
#include <AK/URLParser.h> #include <AK/URLParser.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLHyperlinkElementUtils.h> #include <LibWeb/HTML/HTMLHyperlinkElementUtils.h>
#include <LibWeb/Infra/CharacterTypes.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Loader/FrameLoader.h> #include <LibWeb/Loader/FrameLoader.h>
namespace Web::HTML { namespace Web::HTML {
@ -487,10 +485,10 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<DeprecatedString>
// 4. Let targetAttributeValue be the empty string. // 4. Let targetAttributeValue be the empty string.
// 5. If subject is an a or area element, then set targetAttributeValue to // 5. If subject is an a or area element, then set targetAttributeValue to
// the result of getting an element's target given subject. // the result of getting an element's target given subject.
DeprecatedString target_attribute_value = get_an_elements_target(); DeprecatedString target_attribute_value = hyperlink_element_utils_get_an_elements_target();
// 6. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue. // 6. Let noopener be the result of getting an element's noopener with subject and targetAttributeValue.
auto noopener = get_an_elements_noopener(target_attribute_value); auto noopener = hyperlink_element_utils_get_an_elements_noopener(target_attribute_value);
// 7. Let target be the first return value of applying the rules for // 7. Let target be the first return value of applying the rules for
// choosing a browsing context given targetAttributeValue, source, and // choosing a browsing context given targetAttributeValue, source, and
@ -541,40 +539,4 @@ void HTMLHyperlinkElementUtils::follow_the_hyperlink(Optional<DeprecatedString>
}); });
} }
DeprecatedString HTMLHyperlinkElementUtils::get_an_elements_target() const
{
// To get an element's target, given an a, area, or form element element, run these steps:
// 1. If element has a target attribute, then return that attribute's value.
if (auto target = hyperlink_element_utils_target(); !target.is_empty())
return target;
// FIXME: 2. If element's node document contains a base element with a
// target attribute, then return the value of the target attribute of the
// first such base element.
// 3. Return the empty string.
return "";
}
// https://html.spec.whatwg.org/multipage/links.html#get-an-element's-noopener
TokenizedFeature::NoOpener HTMLHyperlinkElementUtils::get_an_elements_noopener(StringView target) const
{
// To get an element's noopener, given an a, area, or form element element and a string target:
auto rel = hyperlink_element_utils_rel().to_lowercase();
auto link_types = rel.view().split_view_if(Infra::is_ascii_whitespace);
// 1. If element's link types include the noopener or noreferrer keyword, then return true.
if (link_types.contains_slow("noopener"sv) || link_types.contains_slow("noreferrer"sv))
return TokenizedFeature::NoOpener::Yes;
// 2. If element's link types do not include the opener keyword and
// target is an ASCII case-insensitive match for "_blank", then return true.
if (!link_types.contains_slow("opener"sv) && Infra::is_ascii_case_insensitive_match(target, "_blank"sv))
return TokenizedFeature::NoOpener::Yes;
// 3. Return false.
return TokenizedFeature::NoOpener::No;
}
} }

View file

@ -55,8 +55,9 @@ protected:
virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) = 0; virtual WebIDL::ExceptionOr<void> set_hyperlink_element_utils_href(DeprecatedString) = 0;
virtual bool hyperlink_element_utils_is_html_anchor_element() const = 0; virtual bool hyperlink_element_utils_is_html_anchor_element() const = 0;
virtual bool hyperlink_element_utils_is_connected() const = 0; virtual bool hyperlink_element_utils_is_connected() const = 0;
virtual DeprecatedString hyperlink_element_utils_target() const = 0; virtual DeprecatedString hyperlink_element_utils_get_an_elements_target() const = 0;
virtual DeprecatedString hyperlink_element_utils_rel() const = 0; virtual TokenizedFeature::NoOpener hyperlink_element_utils_get_an_elements_noopener(StringView target) const = 0;
virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) = 0; virtual void hyperlink_element_utils_queue_an_element_task(HTML::Task::Source source, Function<void()> steps) = 0;
void set_the_url(); void set_the_url();
@ -66,8 +67,6 @@ private:
void reinitialize_url() const; void reinitialize_url() const;
void update_href(); void update_href();
bool cannot_navigate() const; bool cannot_navigate() const;
DeprecatedString get_an_elements_target() const;
TokenizedFeature::NoOpener get_an_elements_noopener(StringView target) const;
Optional<AK::URL> m_url; Optional<AK::URL> m_url;
}; };