Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / dom / node_7.lucb

#==============================================================================================
#
#   node_7 - DOM::Node: the accessibility tree and the accessible name computation
#
#   DESCRIPTION:
#       Port of LibWeb/DOM/Node.cpp (lines 2834-3311): building the accessibility tree and
#       name_or_description, the "Accessible Name and Description Computation" of accname-1.2
#       with the HTML-AAM and SVG-AAM host-language rules.
#
#==============================================================================================

# mark: Helpers =================================================================================

## `string.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace)`.
func dom_node_split_on_ascii_whitespace(string: const ak.String*) -> ak.Vector[ak.StringView]:
    let view = ak.string_bytes_as_string_view(string)
    let predicate = ak.CharPredicateFunction(function = web_infra.is_ascii_whitespace)
    return ak.string_view_split_view_if(&view, &predicate)

## `aria_label.has_value() && !aria_label->is_empty() && !aria_label->bytes_as_string_view().is_whitespace()`.
func dom_node_has_non_whitespace_text(maybe_text: ak.String?) -> bool:
    let text = maybe_text else return false
    if ak.string_is_empty(&text):
        return false
    let view = ak.string_bytes_as_string_view(&text)
    return not ak.string_view_is_whitespace(&view)

## `builder.append(String)`.
func dom_node_append_string(builder: ak.StringBuilder*, string: ak.String):
    ak.string_builder_append_string_view(builder, ak.string_bytes_as_string_view(&string))

## `builder.append(Utf16String)`.
func dom_node_append_utf16_string(builder: ak.StringBuilder*, string: ak.Utf16String):
    ak.string_builder_append_utf16_view(builder, ak.utf16_string_base_utf16_view(&string.utf16_string_base))

## Steps ii.a/ii.b of name-from-content for one generated-content pseudo-element: the content's
## alt text if it has one, otherwise its strings.
func dom_node_append_generated_content(builder: ak.StringBuilder*, pseudo_element_node: LayoutNodeWithStyle*):
    # NB: We know that content has a value since we set it immediately when creating a ::before or ::after pseudo
    #     element node.
    let computed_values = layout_node_with_style_computed_values(pseudo_element_node)
    var content = css_computed_values_content(&computed_values.computed_values) else trap("Optional::value: no content")

    if let alt_text = content.alt_text:
        dom_node_append_string(builder, alt_text)
    else:
        for item in ak.vector_span(&content.data):
            match item:
                .string(string):
                    dom_node_append_string(builder, string)
                .css_image_style_value(_):
                    continue

# mark: The accessibility tree ==================================================================

## Ported from Node.cpp:2835 Web::DOM::Node::build_accessibility_tree.
pub func dom_node_build_accessibility_tree(this: DomNode*, parent: DomAccessibilityTreeNode*):
    if dom_node_is_uninteresting_whitespace_node(this):
        return

    if dom_node_is_document(this):
        let document = (DomDocument*)this
        if let document_element = dom_document_document_element(document):
            if aria_mixin_include_in_accessibility_tree(&document_element.aria_mixin):
                dom_accessibility_tree_node_set_value(parent, document_element.dom_node())
                if dom_node_has_child_nodes(document_element.dom_node()):
                    # for_each_child
                    for child in dom_node_children(document_element.dom_node()):
                        dom_node_build_accessibility_tree(child, parent)
    elif dom_node_is_element(this):
        let element = (DomElement*)this

        if is_html_script_element(element) or is_html_style_element(element):
            return

        if aria_mixin_include_in_accessibility_tree(&element.aria_mixin):
            let current_node = dom_accessibility_tree_node_create(dom_node_document(this), this)
            dom_accessibility_tree_node_append_child(parent, current_node)
            if dom_node_has_child_nodes(this):
                # for_each_child
                for child in dom_node_children(this):
                    dom_node_build_accessibility_tree(child, current_node)
        elif dom_node_has_child_nodes(this):
            # for_each_child
            for child in dom_node_children(this):
                dom_node_build_accessibility_tree(child, parent)
    elif dom_node_is_text(this):
        dom_accessibility_tree_node_append_child(parent, dom_accessibility_tree_node_create(dom_node_document(this), this))
        if dom_node_has_child_nodes(this):
            # for_each_child
            for child in dom_node_children(this):
                dom_node_build_accessibility_tree(child, parent)

# mark: The accessible name and description =====================================================

## Ported from Node.cpp:2884 Web::DOM::Node::name_or_description.
# https://www.w3.org/TR/accname-1.2/#mapping_additional_nd_te
pub func dom_node_name_or_description(this: DomNode*, target: DomNameOrDescription, document: DomDocument*, visited_nodes: ak.HashTable[i64, ak.I64Traits]*, is_descendant: DomIsDescendant = DomIsDescendant.no, should_compute_role: DomShouldComputeRole = DomShouldComputeRole.yes) -> ak.String!:
    # The text alternative for a given element is computed as follows:
    # 1. Set the root node to the given element, the current node to the root node, and the total accumulated text to the
    #    empty string (""). If the root node's role prohibits naming, return the empty string ("").
    let root_node = this
    var current_node = root_node
    var total_accumulated_text = ak.string_builder_new()
    _ = ak.hash_table_set(visited_nodes, dom_node_unique_id(this))

    if dom_node_is_element(this):
        let element = (DomElement*)this
        var role: AriaRole? = none
        # Per https://w3c.github.io/aria/#document-handling_author-errors_roles, determining whether to ignore certain
        # specified landmark roles requires first determining, in the ARIAMixin code, whether the element for which the
        # role is specified has an accessible name — that is, calling into this name_or_description code. But if we
        # then try to retrieve a role for such elements here, that’d then end up calling right back into this
        # name_or_description code — which would cause the calls to loop infinitely. So to avoid that, the caller
        # in the ARIAMixin code can pass the shouldComputeRole parameter to indicate we must skip the role lookup.
        if should_compute_role == DomShouldComputeRole.yes:
            role = aria_mixin_role_from_role_attribute_value(&element.aria_mixin)
        # Per https://w3c.github.io/html-aam/#el-aside and https://w3c.github.io/html-aam/#el-section, computing a
        # default role for an aside element or section element requires first computing its accessible name — that is,
        # calling into this name_or_description code. But if we then try to determine a default role for the aside
        # element or section element here, that’d then end up calling right back into this name_or_description code —
        # which would cause the calls to loop infinitely. So to avoid that, we only compute a default role here if this
        # isn’t an aside element or section element.
        # https://github.com/w3c/aria/issues/2391
        let local_name = dom_element_local_name(element)
        if role == none and not ak.fly_string_eq_fly_string(&local_name, &html_tag_names_aside) and not ak.fly_string_eq_fly_string(&local_name, &html_tag_names_section):
            role = aria_mixin_default_role(&element.aria_mixin)

        # 2. Compute the text alternative for the current node:

        # A. Hidden Not Referenced: If the current node is hidden and is:
        # i. Not part of an aria-labelledby or aria-describedby traversal, where the node directly referenced by that
        #    relation was hidden.
        # ii. Nor part of a native host language text alternative element (e.g. label in HTML) or attribute traversal,
        #     where the root of that traversal was hidden.
        # Return the empty string.
        #
        # NOTE: Nodes with CSS properties display:none, visibility:hidden, visibility:collapse or content-visibility:hidden:
        #       They are considered hidden, as they match the guidelines "not perceivable" and "explicitly hidden".
        #
        # AD-HOC: We don’t implement this step here — because strictly implementing this would cause us to return early
        # whenever encountering a node (element, actually) that “is hidden and is not directly referenced by
        # aria-labelledby or aria-describedby”, without traversing down through that element’s subtree to see if it has
        # (1) any descendant elements that are directly referenced and/or (2) any un-hidden nodes. So we instead (in
        # substep G below) traverse upward through ancestor nodes of every text node, and check in that way to do the
        # equivalent of what this step seems to have been intended to do.
        # https://github.com/w3c/aria/issues/2387

        # B. Otherwise:
        # - if computing a name, and the current node has an aria-labelledby attribute that contains at least one valid
        #   IDREF, and the current node is not already part of an aria-labelledby traversal, process its IDREFs in the
        #   order they occur:
        # - or, if computing a description, and the current node has an aria-describedby attribute that contains at least
        #   one valid IDREF, and the current node is not already part of an aria-describedby traversal, process its IDREFs
        #   in the order they occur:
        let aria_labelled_by = aria_mixin_aria_labelled_by(&element.aria_mixin)
        let aria_described_by = aria_mixin_aria_described_by(&element.aria_mixin)

        var has_valid_idref = false
        if target == DomNameOrDescription.name:
            if let labelled_by = aria_labelled_by:
                has_valid_idref = dom_node_first_valid_id(ak.string_bytes_as_string_view(&labelled_by), document) != none
        if not has_valid_idref and target == DomNameOrDescription.description:
            if let described_by = aria_described_by:
                has_valid_idref = dom_node_first_valid_id(ak.string_bytes_as_string_view(&described_by), document) != none
        if has_valid_idref:
            # i. Set the accumulated text to the empty string.
            ak.string_builder_clear(&total_accumulated_text)

            var id_list: ak.Vector[ak.StringView]
            if target == DomNameOrDescription.name:
                let labelled_by = aria_labelled_by else trap("unreachable")
                id_list = dom_node_split_on_ascii_whitespace(&labelled_by)
            else:
                let described_by = aria_described_by else trap("unreachable")
                id_list = dom_node_split_on_ascii_whitespace(&described_by)

            # ii. For each IDREF:
            for id_ref in ak.vector_span(&id_list):
                let node = dom_parent_node_get_element_by_id(document.dom_parent_node(), ak.fly_string_from_utf8(id_ref) else trap("MUST: FlyString::from_utf8")) else continue
                # AD-HOC: The “For each IDREF” substep in the spec doesn’t seem to explicitly require the following
                # check for an aria-label value; but the “div group explicitly labelledby self and heading” subtest at
                # https://wpt.fyi/results/accname/name/comp_labelledby.html won’t pass unless we do this check.
                # https://github.com/w3c/aria/issues/2388
                if target == DomNameOrDescription.name and dom_node_has_non_whitespace_text(aria_mixin_aria_label(&node.aria_mixin)):
                    ak.string_builder_append_u8(&total_accumulated_text, ' ')
                    dom_node_append_string(&total_accumulated_text, aria_mixin_aria_label(&node.aria_mixin) else trap("unreachable"))
                if ak.hash_table_contains_t(visited_nodes, dom_node_unique_id(node.dom_node())):
                    continue

                # a. Set the current node to the node referenced by the IDREF.
                current_node = node.dom_node()
                # b. Compute the text alternative of the current node beginning with step 2. Set the result to that text alternative.
                let result = try dom_node_name_or_description(node.dom_node(), target, document, visited_nodes)
                # c. Append the result, with a space, to the accumulated text.
                ak.string_builder_append_u8(&total_accumulated_text, ' ')
                dom_node_append_string(&total_accumulated_text, result)

            # iii. Return the accumulated text.
            # AD-HOC: This substep in the spec doesn’t seem to explicitly require the following check for an aria-label
            # value; but the “button's hidden referenced name (visibility:hidden) with hidden aria-labelledby traversal
            # falls back to aria-label” subtest at https://wpt.fyi/results/accname/name/comp_labelledby.html won’t pass
            # unless we do this check.
            # https://github.com/w3c/aria/issues/2388
            let accumulated_view = ak.string_builder_string_view(&total_accumulated_text)
            if ak.string_view_is_whitespace(&accumulated_view) and target == DomNameOrDescription.name and dom_node_has_non_whitespace_text(aria_mixin_aria_label(&element.aria_mixin)):
                return aria_mixin_aria_label(&element.aria_mixin) else trap("unreachable")
            return try ak.string_builder_to_string(&total_accumulated_text)

        # D. AriaLabel: Otherwise, if the current node has an aria-label attribute whose value is not undefined, not
        #    the empty string, nor, when trimmed of whitespace, is not the empty string:
        #
        # AD-HOC: We’ve reordered substeps C and D from https://w3c.github.io/accname/#step2 — because
        # the more-specific per-HTML-element requirements at https://w3c.github.io/html-aam/#accname-computation
        # necessitate doing so, and the “input with label for association is superceded by aria-label” subtest at
        # https://wpt.fyi/results/accname/name/comp_label.html won’t pass unless we do this reordering.
        # Spec PR: https://github.com/w3c/aria/pull/2377
        if target == DomNameOrDescription.name and dom_node_has_non_whitespace_text(aria_mixin_aria_label(&element.aria_mixin)):
            # TODO: - If traversal of the current node is due to recursion and the current node is an embedded control as defined in step 2E, ignore aria-label and skip to rule 2E.
            # https://github.com/w3c/aria/pull/2385 and https://github.com/w3c/accname/issues/173
            if not dom_node_is_html_slot_element(element.dom_node()):
                return aria_mixin_aria_label(&element.aria_mixin) else trap("unreachable")

        # C. Embedded Control: Otherwise, if the current node is a control embedded within the label (e.g. any element
        #    directly referenced by aria-labelledby) for another widget, where the user can adjust the embedded control's
        #    value, then return the embedded control as part of the text alternative in the following manner:
        var labels: DomNodeList*? = none
        if is_html_element(this):
            labels = html_element_labels((HtmlElement*)current_node)
        var label_count: u32 = 0
        if let label_list = labels:
            label_count = dom_node_list_length(label_list)
        if label_count > 0:
            let label_list = labels else trap("unreachable")
            var builder = ak.string_builder_new()
            for i: u32 in 0..<dom_node_list_length(label_list):
                if not ak.string_builder_is_empty(&builder):
                    ak.string_builder_append(&builder, " ")
                let label = dom_node_list_item(label_list, i) else trap("NodeList::item out of range")
                var nodes = dom_node_children_as_vector(label)
                for maybe_node in ak.vector_span(&nodes):
                    let node = maybe_node else trap("null GC::Root<Node>")
                    # AD-HOC: https://wpt.fyi/results/accname/name/comp_host_language_label.html has “encapsulation”
                    # tests, from which can be induced a requirement that when computing the accessible name for a
                    # <label>-ed form control (“embedded control”), then any content (text content or attribute values)
                    # from the control itself that would otherwise be included in the accessible-name computation for
                    # it ancestor <label> must instead be skipped and not included. The HTML-AAM spec seems to maybe
                    # be trying to achieve that result by expressing specific steps for each particular type of form
                    # control. But what all that reduces/optimizes/simplifies down to is just, “skip over self”.
                    # https://github.com/w3c/aria/issues/2389
                    if node == this:
                        continue

                    if dom_node_is_element(node):
                        let node_element = (DomElement*)node
                        let node_role = aria_mixin_role_or_default(&node_element.aria_mixin)

                        if node_role == AriaRole.textbox:
                            # i. Textbox: If the embedded control has role textbox, return its value.
                            if is_html_input_element(node):
                                let input = (HtmlInputElement*)node
                                if dom_element_has_attribute(input.dom_element(), html_attribute_names_value):
                                    dom_node_append_utf16_string(&builder, html_input_element_value(input))
                            else:
                                dom_node_append_utf16_string(&builder, dom_node_text_content(node) else trap("Optional::value: no text content"))
                        elif node_role == AriaRole.combobox:
                            # ii. Combobox/Listbox: If the embedded control has role combobox or listbox, return the text
                            #     alternative of the chosen option.
                            if is_html_input_element(node):
                                let input = (HtmlInputElement*)node
                                if dom_element_has_attribute(input.dom_element(), html_attribute_names_value):
                                    dom_node_append_utf16_string(&builder, html_input_element_value(input))
                            elif is_html_select_element(node):
                                let select = (HtmlSelectElement*)node
                                dom_node_append_utf16_string(&builder, html_select_element_value(select))
                            else:
                                dom_node_append_utf16_string(&builder, dom_node_text_content(node) else trap("Optional::value: no text content"))
                        elif node_role == AriaRole.listbox:
                            # ii. Combobox/Listbox: If the embedded control has role combobox or listbox, return the text
                            #     alternative of the chosen option.
                            if is_html_select_element(node):
                                let select = (HtmlSelectElement*)node
                                dom_node_append_utf16_string(&builder, html_select_element_value(select))
                            var children = dom_node_children_as_vector(node)
                            for maybe_child in ak.vector_span(&children):
                                let child = maybe_child else trap("null GC::Root<Node>")
                                if dom_node_is_element(child):
                                    let child_element = (DomElement*)child
                                    let child_role = aria_mixin_role_or_default(&child_element.aria_mixin)
                                    if child_role == AriaRole.option and dom_node_optional_string_is(aria_mixin_aria_selected(&child_element.aria_mixin), "true"):
                                        dom_node_append_utf16_string(&builder, dom_node_text_content(child_element.dom_node()) else trap("Optional::value: no text content"))
                        elif node_role == AriaRole.spinbutton or node_role == AriaRole.slider:
                            let aria_valuenow = aria_mixin_aria_value_now(&node_element.aria_mixin)
                            let aria_valuetext = aria_mixin_aria_value_text(&node_element.aria_mixin)

                            # iii. Range: If the embedded control has role range (e.g., a spinbutton or slider):
                            # a. If the aria-valuetext property is present, return its value,
                            if let valuetext = aria_valuetext:
                                dom_node_append_string(&builder, valuetext)
                            # b. Otherwise, if the aria-valuenow property is present, return its value
                            elif let valuenow = aria_valuenow:
                                dom_node_append_string(&builder, valuenow)
                            # c. Otherwise, use the value as specified by a host language attribute.
                            elif is_html_input_element(node):
                                let input = (HtmlInputElement*)node
                                if dom_element_has_attribute(input.dom_element(), html_attribute_names_value):
                                    dom_node_append_utf16_string(&builder, html_input_element_value(input))
                    elif dom_node_is_text(node):
                        let text_node = (DomText*)node
                        dom_node_append_utf16_string(&builder, dom_character_data_data(text_node.dom_character_data()))
            return try ak.string_builder_to_string(&builder)

        # E. Host Language Label: Otherwise, if the current node's native markup provides an attribute (e.g. alt) or
        #    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.
        # 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.
        #
        # https://w3c.github.io/html-aam/#img-element-accessible-name-computation
        # use alt attribute, even if its value is the empty string.
        # See also https://wpt.fyi/results/accname/name/comp_tooltip.tentative.html.
        if is_html_image_element(element) and dom_element_has_attribute(element, html_attribute_names_alt):
            return dom_element_get_attribute(element, html_attribute_names_alt) else trap("Optional::value: no alt")

        # https://w3c.github.io/svg-aam/#mapping_additional_nd
        var title_element_text: ak.String? = none
        if dom_node_is_svg_element(element.dom_node()):
            # 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.
            # for_each_child_of_type<SVG::SVGTitleElement>
            for child in dom_node_children(element.dom_node()):
                let title = as_if_svg_title_element(child) else continue
                title_element_text = none
                if let title_text = dom_node_text_content(title.dom_node()):
                    title_element_text = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&title_text.utf16_string_base)
                break
            if let text = title_element_text:
                return text

            # 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 let title_attribute = dom_element_get_attribute_ns(element, namespace_x_link, x_link_attribute_names_title):
                return title_attribute

        # https://w3c.github.io/html-aam/#table-element-accessible-name-computation
        # 2. If the accessible name is still empty, then: if the table element has a child that is a caption element,
        #    then use the subtree of the first such element.
        if is_html_table_element(element):
            let table = (HtmlTableElement*)element
            if let caption = html_table_element_caption(table):
                let caption_text = dom_node_text_content(caption.dom_node()) else trap("Optional::value: no text content")
                return ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&caption_text.utf16_string_base)

        # https://w3c.github.io/html-aam/#fieldset-element-accessible-name-computation
        # 2. If the accessible name is still empty, then: if the fieldset element has a child that is a legend element,
        #    then use the subtree of the first such element.
        if is_html_field_set_element(element):
            var legend: ak.String? = none
            # for_each_child_of_type<HTML::HTMLLegendElement>
            for child in dom_node_children(element.dom_node()):
                let legend_element = as_if_html_legend_element(child) else continue
                let legend_text = dom_node_text_content(legend_element.dom_node()) else trap("Optional::value: no text content")
                legend = ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&legend_text.utf16_string_base)
                break
            if let legend_value = legend:
                return legend_value

        if is_html_input_element(element):
            let input = (HtmlInputElement*)element
            # https://w3c.github.io/html-aam/#input-type-button-input-type-submit-and-input-type-reset-accessible-name-computation
            # 3. Otherwise use the value attribute.
            let type_state = html_input_element_type_state(input)
            if type_state == HtmlInputElementTypeAttributeState.button or type_state == HtmlInputElementTypeAttributeState.submit_button or type_state == HtmlInputElementTypeAttributeState.reset_button:
                if let value = dom_element_get_attribute(input.dom_element(), html_attribute_names_value):
                    return value

            # https://w3c.github.io/html-aam/#input-type-image-accessible-name-computation
            # 3. Otherwise use alt attribute if present and its value is not the empty string.
            if html_input_element_type_state(input) == HtmlInputElementTypeAttributeState.image_button:
                if let alt = dom_element_get_attribute(element, html_attribute_names_alt):
                    return alt

        # F. Name From Content: 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:
        var role_allows_name_from_content = false
        if let role_value = role:
            role_allows_name_from_content = aria_allows_name_from_content(role_value)
        if role_allows_name_from_content or dom_element_is_referenced(element) or is_descendant == DomIsDescendant.yes:
            # i. Set the accumulated text to the empty string.
            ak.string_builder_clear(&total_accumulated_text)

            # ii. Name From Generated Content: Check for CSS generated textual content associated with the current node
            #     and include it in the accumulated text. The CSS ::before and ::after pseudo elements [CSS2] can provide
            #     textual content for elements that have a content model.
            # a. For ::before pseudo elements, User agents MUST prepend CSS textual content, without a space, to the textual
            #    content of the current node.
            # b. For ::after pseudo elements, User agents MUST append CSS textual content, without a space, to the textual
            #    content of the current node. NOTE: The code for handling the ::after pseudo elements case is further below,
            #    following the “iii. For each child node of the current node” code.
            if let before = dom_element_get_pseudo_element_node(element, css_data.PseudoElement.before):
                dom_node_append_generated_content(&total_accumulated_text, before)

            # iii. Determine Child Nodes: Determine the rendered child nodes of the current node:
            # iii. Determine Child Nodes: Determine the rendered child nodes of the current node:
            # c. [Otherwise,] set the rendered child nodes to be the child nodes of the current node.
            var child_nodes = dom_node_children_as_vector(current_node)

            # a. If the current node has an attached shadow root, set the rendered child nodes to be the child nodes of
            #    the shadow root.
            if dom_element_is_shadow_host(element):
                if let shadow_root = dom_element_shadow_root(element):
                    if dom_node_is_connected(shadow_root.dom_node()):
                        child_nodes = dom_node_children_as_vector(shadow_root.dom_node())

            # b. Otherwise, if the current node is a slot with assigned nodes, set the rendered child nodes to be the
            #    assigned nodes of the current node.
            if let slot_element = as_if_html_slot_element(element):
                dom_node_append_utf16_string(&total_accumulated_text, dom_node_text_content(element.dom_node()) else trap("Optional::value: no text content"))
                child_nodes = html_slot_element_assigned_nodes(slot_element, HtmlAssignedNodesOptions())

            # iv. Name From Each Child: For each rendered child node of the current node
            for maybe_child_node in ak.vector_span(&child_nodes):
                let child_node = maybe_child_node else trap("null GC::Root<Node>")
                if not dom_node_is_element(child_node) and not dom_node_is_text(child_node):
                    continue
                var should_add_space = true
                dom_document_update_layout(document, DomUpdateLayoutReason.node_name_or_description)
                if let layout_node = dom_node_layout_node(child_node):
                    let display = layout_node_display(layout_node)
                    if css_display_is_inline_outside(&display) and css_display_is_flow_inside(&display):
                        should_add_space = false
                if ak.hash_table_contains_t(visited_nodes, dom_node_unique_id(child_node)):
                    continue

                # a. Set the current node to the child node.
                current_node = child_node

                # b. Compute the text alternative of the current node beginning with step 2. Set the result to that text alternative.
                let result = dom_node_name_or_description(current_node, target, document, visited_nodes, DomIsDescendant.yes, should_compute_role) else trap("MUST: name_or_description")

                # J. Append a space character and the result of each step above to the total accumulated text.
                # AD-HOC: Doing the space-adding here is in a different order from what the spec states.
                if should_add_space:
                    ak.string_builder_append_u8(&total_accumulated_text, ' ')

                # c. Append the result to the accumulated text.
                dom_node_append_string(&total_accumulated_text, result)

            # NOTE: See step ii.b above.
            if let after = dom_element_get_pseudo_element_node(element, css_data.PseudoElement.after):
                dom_node_append_generated_content(&total_accumulated_text, after)

            # v. Return the accumulated text if it is not the empty string ("").
            if not ak.string_builder_is_empty(&total_accumulated_text):
                return try ak.string_builder_to_string(&total_accumulated_text)

            # Important: Each node in the subtree is consulted only once. If text has been collected from a descendant,
            # but is referenced by another IDREF in some descendant node, then that second, or subsequent, reference is
            # not followed. This is done to avoid infinite loops.

    # G. Text Node: Otherwise, if the current node is a Text Node, return its textual contents.
    #
    # AD-HOC: The spec doesn’t require ascending through the parent node and ancestor nodes of every text node we
    # reach — the way we’re doing there. But we implement it this way because the spec algorithm as written doesn’t
    # appear to achieve what it seems to be intended to achieve. Specifically, the spec algorithm as written doesn’t
    # cause traversal through element subtrees in way that’s necessary to check for descendants that are referenced by
    # aria-labelledby or aria-describedby and/or un-hidden. See the comment for substep A above.
    if dom_node_is_text(this):
        var text_is_visible = true
        if let parent_element = dom_node_parent_element(this):
            text_is_visible = dom_element_is_referenced(parent_element) or not dom_element_is_hidden(parent_element) or not dom_element_has_hidden_ancestor(parent_element) or dom_element_has_referenced_and_hidden_ancestor(parent_element)
        if text_is_visible:
            if let layout_node = dom_node_layout_node(this):
                if layout_node_is_text_node(layout_node):
                    let text_for_rendering = layout_text_node_text_for_rendering(as_layout_text_node(layout_node))
                    return ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&text_for_rendering.utf16_string_base)
            let text = dom_node_text_content(this) else trap("Optional::value: no text content")
            return ak.utf16_string_base_to_utf8_but_should_be_ported_to_utf16(&text.utf16_string_base)

    # H. Otherwise, if the current node is a descendant of an element whose Accessible Name or Accessible Description
    #    is being computed, and contains descendants, proceed to 2F.i.
    #
    # AD-HOC: We don’t implement this step here — because is essentially unreachable code in the spec algorithm.
    # We could never get here without descending through every subtree of an element whose Accessible Name or
    # Accessible Description is being computed. And in our implementation of substep F about, we’re anyway already
    # recursively descending through all the child nodes of every element whose Accessible Name or Accessible
    # Description is being computed, in a way that never leads to this substep H every being hit.

    # I. Otherwise, if the current node has a Tooltip attribute, return its value.
    #
    # https://www.w3.org/TR/accname-1.2/#dfn-tooltip-attribute
    # Any host language attribute that would result in a user agent generating a tooltip such as in response to a mouse
    # hover in desktop user agents.
    # FIXME: Support SVG tooltips and CSS tooltips
    if is_html_element(this):
        let element = (HtmlElement*)this
        if let tooltip = html_element_title(element):
            if not ak.string_is_empty(&tooltip):
                return tooltip

    # 3. After all steps are completed, the total accumulated text is used as the accessible name or accessible description
    #    of the element that initiated the computation.
    return try ak.string_builder_to_string(&total_accumulated_text)