From dfd50afa4e591d3e467374ff7f6a7338d1d96a7c Mon Sep 17 00:00:00 2001 From: sideshowbarker Date: Sat, 9 Nov 2024 23:45:09 +0900 Subject: [PATCH] LibWeb: Add an alternative_text() getter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds an alternative_text()·getter, for use in computing accessible names. --- Libraries/LibWeb/DOM/Node.cpp | 12 +++++++----- Libraries/LibWeb/DOM/Node.h | 2 ++ Libraries/LibWeb/HTML/HTMLImageElement.h | 7 +++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index ebf2e3d0393..88db05910f8 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -147,6 +147,11 @@ const HTML::HTMLElement* Node::enclosing_html_element_with_attribute(FlyString c return nullptr; } +Optional 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 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(*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(*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. diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.h index 8270114306a..da2dc15cb40 100644 --- a/Libraries/LibWeb/DOM/Node.h +++ b/Libraries/LibWeb/DOM/Node.h @@ -200,6 +200,8 @@ public: String base_uri() const; + virtual Optional alternative_text() const; + String descendant_text_content() const; Optional text_content() const; void set_text_content(Optional const&); diff --git a/Libraries/LibWeb/HTML/HTMLImageElement.h b/Libraries/LibWeb/HTML/HTMLImageElement.h index 2a2d9ed8473..0252894a2a7 100644 --- a/Libraries/LibWeb/HTML/HTMLImageElement.h +++ b/Libraries/LibWeb/HTML/HTMLImageElement.h @@ -40,6 +40,13 @@ public: virtual void form_associated_element_attribute_changed(FlyString const& name, Optional const& value) override; + Optional 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); }