Truly independent web browser
LibWeb: Index the ids that ARIA references in a tree
Luke Wilde21629ccbb2f5
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 0923470426f..0c00d11fb23 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -11334,6 +11334,27 @@ QuerySelectorResultCache& Document::query_selector_result_cache()return *m_query_selector_result_cache;}+bool Document::tree_references_id_from_aria(Node const& root, Utf16FlyString const& id) const+{+ auto dom_tree_version = root.dom_tree_version();+ if (auto entry = m_ids_referenced_from_aria.get(&root); entry.has_value() && entry->root.ptr().ptr() == &root && entry->dom_tree_version == dom_tree_version)+ return entry->ids.contains(id);++ m_ids_referenced_from_aria.remove_all_matching([](auto const&, auto const& entry) { return !entry.root; });++ IdsReferencedFromAria entry { root, dom_tree_version, {} };+ root.for_each_in_subtree_of_type<HTML::HTMLElement>([&](auto const& element) {+ for (auto& reference : element.parse_id_reference_list(element.aria_labelled_by()))+ entry.ids.set(Utf16FlyString { move(reference) });+ for (auto& reference : element.parse_id_reference_list(element.aria_described_by()))+ entry.ids.set(Utf16FlyString { move(reference) });+ return TraversalDecision::Continue;+ });+ auto is_referenced = entry.ids.contains(id);+ m_ids_referenced_from_aria.set(&root, move(entry));+ return is_referenced;+}+}namespace Web::Bindings {diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 080cf5394b5..d25ce516c20 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1586,6 +1586,9 @@ public:RefPtr<SelectorQuery const> selector_query_for(Utf16View) const;QuerySelectorResultCache& query_selector_result_cache();+ // Whether the aria-labelledby or aria-describedby of an element in the tree of root references id.+ bool tree_references_id_from_aria(Node const& root, Utf16FlyString const& id) const;+GC::Ptr<HTML::CustomElementRegistry> custom_element_registry() const;void set_custom_element_registry(GC::Ptr<HTML::CustomElementRegistry> custom_element_registry) { m_custom_element_registry = custom_element_registry; }GC::Ptr<HTML::CustomElementRegistry> effective_global_custom_element_registry() const;@@ -2249,6 +2252,15 @@ private:// Cache of querySelectorAll results, validated lazily against the query root's dom_tree_version/character_data_version.OwnPtr<QuerySelectorResultCache> m_query_selector_result_cache;+ // The ids that aria-labelledby and aria-describedby reference in a tree, validated lazily against the tree's+ // dom_tree_version. Computing accessible names asks this for the parent and the ancestors of every text node.+ struct IdsReferencedFromAria {+ GC::Weak<Node> root;+ u64 dom_tree_version { 0 };+ HashTable<Utf16FlyString> ids;+ };+ mutable HashMap<GC::RawPtr<Node const>, IdsReferencedFromAria> m_ids_referenced_from_aria;+// https://fullscreen.spec.whatwg.org/#list-of-pending-fullscreen-eventsVector<PendingFullscreenEvent> m_pending_fullscreen_events;diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 8c4d585d17a..f9209e1a004 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -4645,27 +4645,7 @@ bool Element::has_hidden_ancestor(Layout::BegunRead const& read) constbool Element::is_referenced() const{- bool is_referenced = false;- if (id().has_value()) {- auto id_view = id()->view();- root().for_each_in_subtree_of_type<HTML::HTMLElement>([&](auto& element) {- auto aria_data = MUST(Web::ARIA::AriaData::build_data(element));- auto references_id = [&](Vector<Utf16String> const& references) {- for (auto const& id_reference : references) {- if (id_reference.utf16_view() == id_view)- return true;- }- return false;- };- if (references_id(aria_data->aria_labelled_by_or_default())- || references_id(aria_data->aria_described_by_or_default())) {- is_referenced = true;- return TraversalDecision::Break;- }- return TraversalDecision::Continue;- });- }- return is_referenced;+ return id().has_value() && document().tree_references_id_from_aria(root(), *id());}bool Element::has_referenced_and_hidden_ancestor(Layout::BegunRead const& read) constdiff --git a/Tests/LibWeb/Text/expected/accessible-name-follows-a-reference-added-after-the-tree-was-built.txt b/Tests/LibWeb/Text/expected/accessible-name-follows-a-reference-added-after-the-tree-was-built.txtnew file mode 100644index 00000000000..fd0a54c2d88--- /dev/null+++ b/Tests/LibWeb/Text/expected/accessible-name-follows-a-reference-added-after-the-tree-was-built.txt@@ -0,0 +1,2 @@+button "Save"+button "Save the draft"diff --git a/Tests/LibWeb/Text/input/accessible-name-follows-a-reference-added-after-the-tree-was-built.html b/Tests/LibWeb/Text/input/accessible-name-follows-a-reference-added-after-the-tree-was-built.htmlnew file mode 100644index 00000000000..dff323508f4--- /dev/null+++ b/Tests/LibWeb/Text/input/accessible-name-follows-a-reference-added-after-the-tree-was-built.html@@ -0,0 +1,20 @@+<!DOCTYPE html>+<script src="include.js"></script>+<button id="button">Save</button>+<div id="label" hidden><span>Save the draft</span></div>+<script>+ test(() => {+ const print_button = () => {+ const visit = (node) => {+ if (node.role === "button")+ println(`button "${node.name}"`);+ for (const child of node.children ?? [])+ visit(child);+ };+ visit(JSON.parse(internals.dumpAccessibilityTree()));+ };+ print_button();+ document.getElementById("button").setAttribute("aria-labelledby", "label");+ print_button();+ });+</script>