LibWeb: Add an alternative_text() getter

This change adds an alternative_text()·getter, for use in computing
accessible names.
This commit is contained in:
sideshowbarker 2024-11-09 23:45:09 +09:00 committed by Andrew Kaster
parent 6d29afaa6c
commit dfd50afa4e
Notes: github-actions[bot] 2024-11-11 21:57:57 +00:00
3 changed files with 16 additions and 5 deletions

View file

@ -147,6 +147,11 @@ const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(FlyString c
return nullptr;
}
Optional<String> Node::alternative_text() const
{
return {};
}
// https://dom.spec.whatwg.org/#concept-descendant-text-content
String Node::descendant_text_content() const
{
@ -2320,11 +2325,8 @@ ErrorOr<String> Node::name_or_description(NameOrDescription target, Document con
// element (e.g. HTML label or SVG title) that defines a text alternative, return that alternative in the form
// of a flat string as defined by the host language, unless the element is marked as presentational
// (role="presentation" or role="none").
if (role != ARIA::Role::presentation && role != ARIA::Role::none) {
if (is<HTML::HTMLImageElement>(*element)) {
if (auto alt = element->get_attribute(HTML::AttributeNames::alt); alt.has_value())
return alt.release_value();
}
if (role != ARIA::Role::presentation && role != ARIA::Role::none && is<HTML::HTMLImageElement>(*element)) {
return element->alternative_text().release_value();
// TODO: Add handling for SVGTitleElement, and also confirm (through existing WPT test cases) whether
// HTMLLabelElement is already handled (by the code for step C. “Embedded Control” above) in conformance
// with the spec requirements — and if not, then add handling for it here.

View file

@ -200,6 +200,8 @@ public:
String base_uri() const;
virtual Optional<String> alternative_text() const;
String descendant_text_content() const;
Optional<String> text_content() const;
void set_text_content(Optional<String> const&);

View file

@ -40,6 +40,13 @@ public:
virtual void form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value) override;
Optional<String> alternative_text() const override
{
if (auto alt = get_attribute(HTML::AttributeNames::alt); alt.has_value())
return alt.release_value();
return {};
}
String alt() const { return get_attribute_value(HTML::AttributeNames::alt); }
String src() const { return get_attribute_value(HTML::AttributeNames::src); }