From e48c45af4386b18300cce6c26fc29b86c336109c Mon Sep 17 00:00:00 2001 From: sideshowbarker Date: Thu, 21 Nov 2024 05:03:28 +0900 Subject: [PATCH] LibWeb: Support accessible-name computation for SVG elements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds support for computing accessible names for SVG elements, per the https://w3c.github.io/svg-aam/#mapping_additional_nd spec requirements. Otherwise, without this change, accessible names for SVG elements don’t get exposed as expected. --- Libraries/LibWeb/DOM/Node.cpp | 24 +++++-- .../svg-aam/name/comp_host_language_label.txt | 22 ++++++ .../name/comp_host_language_label.html | 71 +++++++++++++++++++ 3 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/svg-aam/name/comp_host_language_label.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/svg-aam/name/comp_host_language_label.html diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index bc267ef7298..3fc1ecc6b0c 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -51,6 +51,8 @@ #include #include #include +#include +#include namespace Web::DOM { @@ -2354,11 +2356,25 @@ 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 && is(*element)) { + // TODO: 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. + 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. + // https://w3c.github.io/svg-aam/#mapping_additional_nd + Optional title_element_text; + if (element->is_svg_element()) { + // If the current node has at least one direct child title element, select the appropriate title based on + // the language rules for the SVG specification, and return the title text alternative as a flat string. + element->for_each_child_of_type([&](SVG::SVGTitleElement const& title) mutable { + title_element_text = title.text_content(); + return IterationDecision::Break; + }); + if (title_element_text.has_value()) + return title_element_text.value(); + // If the current node is a link, and there was no child title element, but it has an xlink:title attribute, + // return the value of that attribute. + if (auto title_attribute = element->get_attribute_ns(Namespace::XLink, XLink::AttributeNames::title); title_attribute.has_value()) + return title_attribute.value(); } // F. Otherwise, if the current node's role allows name from content, or if the current node is referenced by aria-labelledby, aria-describedby, or is a native host language text alternative element (e.g. label in HTML), or is a descendant of a native host language text alternative element: diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg-aam/name/comp_host_language_label.txt b/Tests/LibWeb/Text/expected/wpt-import/svg-aam/name/comp_host_language_label.txt new file mode 100644 index 00000000000..43fad3ab8e0 --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/svg-aam/name/comp_host_language_label.txt @@ -0,0 +1,22 @@ +Summary + +Harness status: OK + +Rerun + +Found 12 tests + +12 Pass +Details +Result Test Name MessagePass circle > title +Pass rect > title +Pass polygon > title +Pass g > title +Pass [xlink:title][href] > circle +Pass [xlink:title][href] > rect +Pass [xlink:title][href] > polygon +Pass [xlink:title][href] > g +Pass [xlink:title][xlink:href] > circle +Pass [xlink:title][xlink:href] > rect +Pass [xlink:title][xlink:href] > polygon +Pass [xlink:title][xlink:href] > g \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/svg-aam/name/comp_host_language_label.html b/Tests/LibWeb/Text/input/wpt-import/svg-aam/name/comp_host_language_label.html new file mode 100644 index 00000000000..8922f789f6b --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/svg-aam/name/comp_host_language_label.html @@ -0,0 +1,71 @@ + + + + Name Comp: Host Language Label + + + + + + + + + +

SVG-AAM: Label Tests

+ +

Tests SVG-specific host language label rules (title element and xlink:title attr) in SVG-AAM §8.1 Name and Description, but note the open issues in SVG-AAM #31. + + +

SVG * > title

+ + circle label + rect label + polygon label +
+ + + group label + + + + +
+ + +

SVG a[xlink:title][href]

+ + + + +
+ + + + + + + + +
+ +

SVG a[xlink:title][xlink:href]:not([href])

+ + + + +
+ + + + + + + + +
+ + + +