Skip to content

Truly independent web browser

LibWeb+WebContent: Track accessibility-tree changes as the tree is built

sideshowbarker4876b7fa275f
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 2131aacc179..b4ddadcdef0 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -197,6 +197,7 @@ set(SOURCES
DOM/AbortSignal.cpp
DOM/AbstractElement.cpp
DOM/AbstractRange.cpp
+ DOM/AccessibilityChangeTracker.cpp
DOM/AccessibilityTreeNode.cpp
DOM/AdoptedStyleSheets.cpp
DOM/AnchorNameMap.cpp
diff --git a/Libraries/LibWeb/DOM/AccessibilityChangeTracker.cpp b/Libraries/LibWeb/DOM/AccessibilityChangeTracker.cpp
new file mode 100644
index 00000000000..083cb53d538
--- /dev/null
+++ b/Libraries/LibWeb/DOM/AccessibilityChangeTracker.cpp
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibWeb/DOM/AccessibilityChangeTracker.h>
+
+namespace Web::DOM {
+
+void AccessibilityChangeTracker::begin(i64 document_id, bool whole_tree)
+{
+ m_whole_tree = whole_tree || needs_whole_tree(document_id);
+ if (m_whole_tree)
+ m_states.clear();
+ m_document_id = document_id;
+ ++m_build;
+ m_known_at_begin = m_states.size();
+ m_known_noted = 0;
+}
+
+bool AccessibilityChangeTracker::note(WebView::AccessibilityNodeData const& data, u64 record_version, Optional<u64> geometry_version)
+{
+ auto it = m_states.find(data.id);
+ if (it == m_states.end()) {
+ m_states.set(data.id, {});
+ it = m_states.find(data.id);
+ } else if (it->value.build != m_build) {
+ ++m_known_noted;
+ auto const& state = it->value;
+ auto unchanged = !m_whole_tree && record_version != 0 && state.record_version == record_version
+ && geometry_version.has_value() && state.geometry_version == geometry_version
+ && state.parent_id == data.parent_id && state.child_ids == data.child_ids && state.bounds == data.bounds
+ && state.is_focused == data.is_focused && state.caret_offset == data.caret_offset
+ && state.selection_start == data.selection_start && state.selection_end == data.selection_end;
+ if (unchanged) {
+ it->value.build = m_build;
+ return false;
+ }
+ }
+ auto& state = it->value;
+ state.record_version = record_version;
+ state.geometry_version = geometry_version;
+ state.parent_id = data.parent_id;
+ state.child_ids = data.child_ids;
+ state.bounds = data.bounds;
+ state.is_focused = data.is_focused;
+ state.caret_offset = data.caret_offset;
+ state.selection_start = data.selection_start;
+ state.selection_end = data.selection_end;
+ state.build = m_build;
+ return true;
+}
+
+Vector<i64> AccessibilityChangeTracker::end()
+{
+ // Only when fewer of the nodes noted last time were noted this time are any of them gone.
+ Vector<i64> removed_ids;
+ if (m_known_noted == m_known_at_begin)
+ return removed_ids;
+ for (auto const& [id, state] : m_states) {
+ if (state.build != m_build)
+ removed_ids.append(id);
+ }
+ for (auto id : removed_ids)
+ m_states.remove(id);
+ return removed_ids;
+}
+
+}
diff --git a/Libraries/LibWeb/DOM/AccessibilityChangeTracker.h b/Libraries/LibWeb/DOM/AccessibilityChangeTracker.h
new file mode 100644
index 00000000000..b9f3ff2f0f3
--- /dev/null
+++ b/Libraries/LibWeb/DOM/AccessibilityChangeTracker.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/HashMap.h>
+#include <AK/Optional.h>
+#include <AK/Vector.h>
+#include <LibGfx/Rect.h>
+#include <LibWebCommon/WebView/AccessibilityNodeData.h>
+
+namespace Web::DOM {
+
+// Remembers what was last handed over of each node of a document's accessibility tree, so that a build can hand over
+// only the nodes whose data changed since then, and the IDs of the nodes that are gone: see Document::
+// build_accessibility_tree_change(). Each consumer of the tree keeps its own.
+class AccessibilityChangeTracker {
+public:
+ struct Change {
+ bool is_whole_tree { false };
+ // In tree order.
+ Vector<WebView::AccessibilityNodeData> nodes;
+ Vector<i64> removed_ids;
+ };
+
+ // Whether the next build has to hand over the whole tree: the first for a document.
+ bool needs_whole_tree(i64 document_id) const { return m_document_id != document_id; }
+
+ // For Document::build_accessibility_tree_change(): A build begins, notes each node it reaches, and ends.
+ void begin(i64 document_id, bool whole_tree);
+ // Returns whether the node goes in the change. A node's data is the same as last time when its kept record and
+ // geometry have the same versions (see Document::kept_accessibility_record()) and every field computed again on
+ // each build is the same; a geometry version that isn't known counts as changed.
+ bool note(WebView::AccessibilityNodeData const&, u64 record_version, Optional<u64> geometry_version);
+ // Returns the IDs of the nodes noted last time but not this time.
+ Vector<i64> end();
+
+private:
+ struct NodeState {
+ u64 record_version { 0 };
+ Optional<u64> geometry_version;
+ i64 parent_id { -1 };
+ Vector<i64> child_ids;
+ Gfx::IntRect bounds;
+ bool is_focused { false };
+ i32 caret_offset { -1 };
+ i32 selection_start { -1 };
+ i32 selection_end { -1 };
+ u64 build { 0 };
+ };
+
+ Optional<i64> m_document_id;
+ HashMap<i64, NodeState> m_states;
+ u64 m_build { 0 };
+ size_t m_known_at_begin { 0 };
+ size_t m_known_noted { 0 };
+ bool m_whole_tree { false };
+};
+
+}
diff --git a/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp b/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
index eba9340f80f..d7d93a3815a 100644
--- a/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
+++ b/Libraries/LibWeb/DOM/AccessibilityTreeNode.cpp
@@ -408,10 +408,22 @@ static Optional<Document::CachedTextGeometry> text_geometry_sources(Layout::Node
return geometry;
}
+// Keeps the geometry just computed into the node's data, with what layout computed it from, and returns the version it
+// gets: or nothing, when it can't be kept.
+static Optional<u64> keep_text_geometry(WebView::AccessibilityNodeData const& node_data, DOM::Text const& text_node, Document const& document, Document::CachedTextGeometry geometry)
+{
+ geometry.character_offsets = node_data.character_offsets;
+ geometry.line_break_character_offsets = node_data.line_break_character_offsets;
+ geometry.line_heights = node_data.line_heights;
+ if (auto version = document.cache_text_geometry(text_node, move(geometry)))
+ return version;
+ return {};
+}
+
// The fields that can change with nothing changing in the node itself or its style: its bounds and text geometry,
// which any layout can move, and whether it has the focus. They're computed again even for a node whose other fields
// are kept from the last build.
-static void fill_volatile_fields(WebView::AccessibilityNodeData& node_data, DOM::Node const& node, Document const& document, Layout::BegunRead const& read)
+static Optional<u64> fill_volatile_fields(WebView::AccessibilityNodeData& node_data, DOM::Node const& node, Document const& document, Layout::BegunRead const& read)
{
node_data.bounds = {};
node_data.is_focused = false;
@@ -421,17 +433,17 @@ static void fill_volatile_fields(WebView::AccessibilityNodeData& node_data, DOM:
if (auto const* element = as_if<DOM::Element>(node)) {
node_data.bounds = accessibility_bounds(read, *element);
node_data.is_focused = element->is_focused();
- return;
+ return 0;
}
auto const* text_node = as_if<DOM::Text>(node);
if (!text_node)
- return;
+ return 0;
auto parent = text_node->parent_element();
if (!parent)
- return;
+ return 0;
node_data.bounds = accessibility_bounds(read, *parent);
if (!parent->style_group<CSS::ComputedValues::FontValues>() || !parent->style_group<CSS::ComputedValues::InheritedTextValues>())
- return;
+ return 0;
Gfx::Font const* font = nullptr;
if (auto layout = parent->layout_node(read))
font = &layout->first_available_font();
@@ -447,18 +459,15 @@ static void fill_volatile_fields(WebView::AccessibilityNodeData& node_data, DOM:
node_data.character_offsets = kept->character_offsets;
node_data.line_break_character_offsets = kept->line_break_character_offsets;
node_data.line_heights = kept->line_heights;
- return;
+ return kept->version;
}
}
if (!fill_text_geometry(node_data, *text_node, font, document, read) || !geometry.has_value())
- return;
- geometry->character_offsets = node_data.character_offsets;
- geometry->line_break_character_offsets = node_data.line_break_character_offsets;
- geometry->line_heights = node_data.line_heights;
- document.cache_text_geometry(*text_node, geometry.release_value());
+ return {};
+ return keep_text_geometry(node_data, *text_node, document, geometry.release_value());
}
-void AccessibilityTreeNode::serialize_tree_as_node_data(Vector<WebView::AccessibilityNodeData>& out, Document const& document, Layout::BegunRead const& read, i64 parent_id) const
+void AccessibilityTreeNode::serialize_tree_as_node_data(Vector<WebView::AccessibilityNodeData>& out, Document const& document, Layout::BegunRead const& read, i64 parent_id, AccessibilityChangeTracker* tracker) const
{
WebView::AccessibilityNodeData node_data;
node_data.parent_id = parent_id;
@@ -467,10 +476,17 @@ void AccessibilityTreeNode::serialize_tree_as_node_data(Vector<WebView::Accessib
// node's data holds from one build to the next until a change to the node or its style could alter it, and compute
// again just the fields that can change without one.
auto keeps_records = document.page().accessibility_interested();
- if (auto const* record = keeps_records ? document.cached_accessibility_record(*value()) : nullptr) {
- node_data = *record;
- fill_volatile_fields(node_data, *value(), document, read);
+ // What a change tracker tells this build of the node from the last one by: see AccessibilityChangeTracker::note().
+ u64 record_version = 0;
+ Optional<u64> geometry_version;
+ if (auto kept = keeps_records ? document.kept_accessibility_record(*value()) : Document::KeptAccessibilityRecord {}; kept.data) {
+ node_data = *kept.data;
+ record_version = kept.version;
+ geometry_version = fill_volatile_fields(node_data, *value(), document, read);
} else {
+ if (!value()->is_text())
+ geometry_version = 0;
+ bool text_geometry_from_lines = false;
if (value()->is_element()) {
auto const& element = static_cast<DOM::Element const&>(*value());
@@ -869,12 +885,22 @@ void AccessibilityTreeNode::serialize_tree_as_node_data(Vector<WebView::Accessib
if (bg.alpha() > 0)
node_data.background_color = MUST(String::from_byte_string(bg.to_byte_string_without_alpha()));
- fill_text_geometry(node_data, text_node, font, document, read);
+ text_geometry_from_lines = fill_text_geometry(node_data, text_node, font, document, read);
+ }
+ }
+ }
+ if (keeps_records) {
+ record_version = document.cache_accessibility_record(*value(), node_data);
+ // Keep the text's geometry with its record right away, so the next build reuses it rather than computing it
+ // again.
+ if (text_geometry_from_lines) {
+ auto const& text_node = static_cast<DOM::Text const&>(*value());
+ if (auto* text_layout = text_node.layout_node(read)) {
+ if (auto sources = text_geometry_sources(*text_layout); sources.has_value())
+ geometry_version = keep_text_geometry(node_data, text_node, document, sources.release_value());
}
}
}
- if (keeps_records)
- document.cache_accessibility_record(*value(), node_data);
}
node_data.parent_id = parent_id;
@@ -922,12 +948,13 @@ void AccessibilityTreeNode::serialize_tree_as_node_data(Vector<WebView::Accessib
node_data.child_ids.append(static_cast<i64>(child->value()->unique_id().value()));
}
- out.append(move(node_data));
+ if (!tracker || tracker->note(node_data, record_version, geometry_version))
+ out.append(move(node_data));
for (auto const& child : children()) {
if (should_skip_child(*child))
continue;
- child->serialize_tree_as_node_data(out, document, read, my_id);
+ child->serialize_tree_as_node_data(out, document, read, my_id, tracker);
}
}
diff --git a/Libraries/LibWeb/DOM/AccessibilityTreeNode.h b/Libraries/LibWeb/DOM/AccessibilityTreeNode.h
index f48239274b2..442b35a47ed 100644
--- a/Libraries/LibWeb/DOM/AccessibilityTreeNode.h
+++ b/Libraries/LibWeb/DOM/AccessibilityTreeNode.h
@@ -32,7 +32,7 @@ public:
void serialize_tree_as_json(JsonObjectSerializer<Utf16StringBuilder>& object, Document const&) const;
static Document::CachedAccessibleText compute_accessible_text(Element const&, Document const&);
- void serialize_tree_as_node_data(Vector<WebView::AccessibilityNodeData>& out, Document const&, Layout::BegunRead const&, i64 parent_id = -1) const;
+ void serialize_tree_as_node_data(Vector<WebView::AccessibilityNodeData>& out, Document const&, Layout::BegunRead const&, i64 parent_id = -1, AccessibilityChangeTracker* = nullptr) const;
protected:
virtual void visit_edges(Visitor&) override;
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 28241362695..ed4d1168141 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -305,9 +305,11 @@ struct Document::AccessibilityRecordCache {
AK_ALLOC_WITH_KMALLOC;
struct Record {
WebView::AccessibilityNodeData data;
+ u64 version { 0 };
Optional<CachedTextGeometry> text_geometry;
};
HashMap<UniqueNodeID, Record> records;
+ u64 next_version { 1 };
};
static void release_text_geometry_sources(Vector<Document::CachedTextGeometry::Source>& sources)
@@ -323,6 +325,7 @@ Document::CachedTextGeometry& Document::CachedTextGeometry::operator=(CachedText
return *this;
release_text_geometry_sources(sources);
sources = move(other.sources);
+ version = other.version;
character_offsets = move(other.character_offsets);
line_break_character_offsets = move(other.line_break_character_offsets);
line_heights = move(other.line_heights);
@@ -7327,6 +7330,29 @@ Utf16String Document::dump_accessibility_tree_as_json()
return builder.to_string();
}
+AccessibilityChangeTracker::Change Document::build_accessibility_tree_change(AccessibilityChangeTracker& tracker, bool whole_tree)
+{
+ auto document_id = static_cast<i64>(unique_id().value());
+ AccessibilityChangeTracker::Change change;
+ change.is_whole_tree = whole_tree || tracker.needs_whole_tree(document_id);
+ tracker.begin(document_id, change.is_whole_tree);
+ auto accessibility_tree = AccessibilityTreeNode::create(nullptr);
+ build_accessibility_tree(*&accessibility_tree);
+ if (accessibility_tree->value()) {
+ // The tree reads what renders, as the caller's own read of the render state.
+ Layout::ForcedReadScope read { *this };
+ accessibility_tree->serialize_tree_as_node_data(change.nodes, *this, read, -1, &tracker);
+ } else {
+ WebView::AccessibilityNodeData root;
+ root.id = static_cast<i64>(unique_id().value());
+ root.role = "document"_string;
+ if (tracker.note(root, 0, 0))
+ change.nodes.append(move(root));
+ }
+ change.removed_ids = tracker.end();
+ return change;
+}
+
Vector<WebView::AccessibilityNodeData> Document::build_accessibility_node_data()
{
Vector<WebView::AccessibilityNodeData> nodes;
@@ -7399,10 +7425,20 @@ WebView::AccessibilityNodeData const* Document::cached_accessibility_record(Node
return it == m_accessibility_record_cache->records.end() ? nullptr : &it->value.data;
}
-void Document::cache_accessibility_record(Node const& node, WebView::AccessibilityNodeData const& record) const
+Document::KeptAccessibilityRecord Document::kept_accessibility_record(Node const& node) const
+{
+ if (!m_accessibility_record_cache || m_accessibility_caches_bypassed)
+ return {};
+ auto it = m_accessibility_record_cache->records.find(node.unique_id());
+ if (it == m_accessibility_record_cache->records.end())
+ return {};
+ return { &it->value.data, it->value.version };
+}
+
+u64 Document::cache_accessibility_record(Node const& node, WebView::AccessibilityNodeData const& record) const
{
if (m_accessibility_caches_bypassed)
- return;
+ return 0;
if (!m_accessibility_record_cache)
m_accessibility_record_cache = make<AccessibilityRecordCache>();
// Leave out what every build computes again anyway, so it isn't kept, and copied out, for nothing.
@@ -7412,7 +7448,9 @@ void Document::cache_accessibility_record(Node const& node, WebView::Accessibili
kept.character_offsets.clear();
kept.line_break_character_offsets.clear();
kept.line_heights.clear();
- m_accessibility_record_cache->records.set(node.unique_id(), { move(kept), {} });
+ auto version = m_accessibility_record_cache->next_version++;
+ m_accessibility_record_cache->records.set(node.unique_id(), { move(kept), version, {} });
+ return version;
}
Document::CachedTextGeometry const* Document::cached_text_geometry(Node const& node) const
@@ -7425,14 +7463,18 @@ Document::CachedTextGeometry const* Document::cached_text_geometry(Node const& n
return &*it->value.text_geometry;
}
-void Document::cache_text_geometry(Node const& node, CachedTextGeometry geometry) const
+u64 Document::cache_text_geometry(Node const& node, CachedTextGeometry geometry) const
{
// The geometry goes with the node's record, so whatever drops the record drops the geometry too.
if (!m_accessibility_record_cache || m_accessibility_caches_bypassed)
- return;
+ return 0;
auto it = m_accessibility_record_cache->records.find(node.unique_id());
- if (it != m_accessibility_record_cache->records.end())
- it->value.text_geometry = move(geometry);
+ if (it == m_accessibility_record_cache->records.end())
+ return 0;
+ geometry.version = m_accessibility_record_cache->next_version++;
+ auto version = geometry.version;
+ it->value.text_geometry = move(geometry);
+ return version;
}
static bool is_table_structure(Element const& element)
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 996ed8b7084..dd1badb9c6a 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -41,6 +41,7 @@
#include <LibWeb/CSS/ScrollStateContainerQuery.h>
#include <LibWeb/CSS/StyleScope.h>
#include <LibWeb/CSS/StyleUpdateInputs.h>
+#include <LibWeb/DOM/AccessibilityChangeTracker.h>
#include <LibWeb/DOM/AnchorNameMap.h>
#include <LibWeb/DOM/HoverEventData.h>
#include <LibWeb/DOM/ParentNode.h>
@@ -1257,6 +1258,10 @@ public:
Utf16String dump_accessibility_tree_as_json();
Vector<WebView::AccessibilityNodeData> build_accessibility_node_data();
+ // Builds the tree and hands over what changed in it since the tracker's last build: every node whose data changed,
+ // and the IDs of the nodes that are gone. Hands over the whole tree when asked to, and for the tracker's first build
+ // of this document.
+ AccessibilityChangeTracker::Change build_accessibility_tree_change(AccessibilityChangeTracker&, bool whole_tree);
// The accessible name and description that the accessibility tree reports for an element, kept from one build of
// the tree to the next while an assistive technology consumes it, and dropped whenever a change could alter them.
@@ -1271,7 +1276,15 @@ public:
// What a build of the accessibility tree keeps of each node's data for the next build, while an assistive
// technology consumes the tree; see AccessibilityTreeNode::serialize_tree_as_node_data().
WebView::AccessibilityNodeData const* cached_accessibility_record(Node const&) const;
- void cache_accessibility_record(Node const&, WebView::AccessibilityNodeData const&) const;
+ // A kept record, with the version it got when it was kept: Every record and geometry kept gets a new version, so
+ // the same version means the same data.
+ struct KeptAccessibilityRecord {
+ WebView::AccessibilityNodeData const* data { nullptr };
+ u64 version { 0 };
+ };
+ KeptAccessibilityRecord kept_accessibility_record(Node const&) const;
+ // Returns the version the record gets, or 0 when it isn't kept.
+ u64 cache_accessibility_record(Node const&, WebView::AccessibilityNodeData const&) const;
void invalidate_cached_accessibility_records_after_style_change(Element const&);
// A text node's character geometry, kept with its record from one build to the next along with what layout
@@ -1296,12 +1309,14 @@ public:
bool operator==(Source const&) const = default;
};
Vector<Source> sources;
+ u64 version { 0 };
Vector<Gfx::IntPoint> character_offsets;
Vector<i32> line_break_character_offsets;
Vector<i32> line_heights;
};
CachedTextGeometry const* cached_text_geometry(Node const&) const;
- void cache_text_geometry(Node const&, CachedTextGeometry) const;
+ // Returns the version the geometry gets, or 0 when it isn't kept.
+ u64 cache_text_geometry(Node const&, CachedTextGeometry) const;
// For tests: each text node whose kept geometry differs from what computing it again gives.
Vector<String> cached_text_geometry_mismatches();
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 9b35f33ccd6..104ff8e83ca 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -442,6 +442,7 @@ class AbortController;
class AbortSignal;
class AbstractElement;
class AbstractRange;
+class AccessibilityChangeTracker;
class AccessibilityTreeNode;
class AnchorNameMap;
class Attr;
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index 2cd46fdf4f7..512a766413b 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -2094,6 +2094,41 @@ String Internals::accessibility_cached_record_mismatches()
return builder.to_string_without_validation();
}
+String Internals::accessibility_change_tracking_mismatches()
+{
+ auto& document = window().associated_document();
+ document.update_layout(DOM::UpdateLayoutReason::InspectAccessibilityTree);
+
+ // Apply what changed since the last call to the tree as it stood then, as the UI process does with what WebContent
+ // sends it, and compare the result with the whole tree built again.
+ auto change = document.build_accessibility_tree_change(m_accessibility_change_tracker, false);
+ m_last_accessibility_change_size = change.is_whole_tree
+ ? MUST(String::formatted("whole tree of {} nodes", change.nodes.size()))
+ : MUST(String::formatted("{} nodes changed, {} removed", change.nodes.size(), change.removed_ids.size()));
+ if (change.is_whole_tree)
+ m_accessibility_tracked_tree.clear();
+ for (auto id : change.removed_ids)
+ m_accessibility_tracked_tree.remove(id);
+ for (auto& node : change.nodes)
+ m_accessibility_tracked_tree.set(node.id, move(node));
+
+ StringBuilder builder;
+ HashTable<i64> built_ids;
+ for (auto const& node : document.build_accessibility_node_data()) {
+ built_ids.set(node.id);
+ auto tracked = m_accessibility_tracked_tree.find(node.id);
+ if (tracked == m_accessibility_tracked_tree.end())
+ builder.appendff("{} \"{}\": missing from the tree built from changes\n", node.role, node.name);
+ else if (tracked->value != node)
+ builder.appendff("{} \"{}\": differs in the tree built from changes\n", node.role, node.name);
+ }
+ for (auto const& [id, node] : m_accessibility_tracked_tree) {
+ if (!built_ids.contains(id))
+ builder.appendff("{} \"{}\": still in the tree built from changes\n", node.role, node.name);
+ }
+ return builder.to_string_without_validation();
+}
+
String Internals::accessibility_cached_geometry_mismatches()
{
StringBuilder builder;
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index 50ba035c930..031aa060d34 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -232,6 +232,8 @@ public:
String accessibility_cached_text_mismatches();
String accessibility_cached_record_mismatches();
String accessibility_cached_geometry_mismatches();
+ String accessibility_change_tracking_mismatches();
+ String accessibility_last_change_size() const { return m_last_accessibility_change_size; }
Utf16String dump_layout_tree(GC::Ref<DOM::Node>);
Utf16String dump_stacking_context_tree();
Utf16String stacking_context_structure_verification_report();
@@ -392,6 +394,10 @@ private:
Vector<GC::Ref<InternalGamepad>> m_gamepads;
Optional<HTML::DevToolsObjectTable> m_devtools_object_table;
HTML::DevToolsLoggedObjects m_devtools_logged_objects;
+ // The accessibility tree as accessibility_change_tracking_mismatches() last left it, built up from changes alone.
+ DOM::AccessibilityChangeTracker m_accessibility_change_tracker;
+ HashMap<i64, WebView::AccessibilityNodeData> m_accessibility_tracked_tree;
+ String m_last_accessibility_change_size;
};
}
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index 3fe5eb4ecc5..39e61a8a887 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -240,6 +240,8 @@ interface Internals {
// one per line. Empty when all of it is current.
DOMString accessibilityCachedRecordMismatches();
DOMString accessibilityCachedGeometryMismatches();
+ DOMString accessibilityChangeTrackingMismatches();
+ DOMString accessibilityLastChangeSize();
DOMString dumpLayoutTree(Node node);
DOMString dumpStackingContextTree();
DOMString stackingContextStructureVerificationReport();
diff --git a/Libraries/LibWebCommon/CMakeLists.txt b/Libraries/LibWebCommon/CMakeLists.txt
index b788d17b74b..749b47537a4 100644
--- a/Libraries/LibWebCommon/CMakeLists.txt
+++ b/Libraries/LibWebCommon/CMakeLists.txt
@@ -77,7 +77,6 @@ set(SOURCES
WebDriver/Response.cpp
WebDriver/TimeoutsConfiguration.cpp
WebDriver/UserPrompt.cpp
- WebView/AccessibilityTreePublisher.cpp
WebView/Attribute.cpp
WebView/BrowsingBehavior.cpp
WebView/ConsoleOutput.cpp
diff --git a/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.cpp b/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.cpp
deleted file mode 100644
index 143e0dfd45a..00000000000
--- a/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.cpp
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <AK/HashTable.h>
-#include <LibWebCommon/WebView/AccessibilityTreePublisher.h>
-
-namespace WebView {
-
-AccessibilityTreePublisher::Update AccessibilityTreePublisher::publish(i64 document_id, Vector<AccessibilityNodeData> nodes, bool whole_tree_requested)
-{
- auto remember = [&](Vector<AccessibilityNodeData> const& published) {
- m_document_id = document_id;
- m_published_nodes.clear();
- m_published_nodes.ensure_capacity(published.size());
- for (auto const& node : published)
- m_published_nodes.set(node.id, node);
- };
-
- if (whole_tree_requested || m_document_id != document_id) {
- remember(nodes);
- return WholeTree { ++m_sequence, move(nodes) };
- }
-
- // Every node that's new or whose data changed goes in the change, in the order the tree lists them. A node whose
- // children changed is among them, so a node that moved is listed by its new parent, and dropped by its old one.
- // The tree sent keeps each node's new data, moved from the tree just built rather than copied again.
- AccessibilityTreeDelta delta;
- Vector<size_t> new_node_indices;
- size_t surviving_count = 0;
- for (size_t i = 0; i < nodes.size(); ++i) {
- auto& node = nodes[i];
- auto published = m_published_nodes.find(node.id);
- if (published == m_published_nodes.end()) {
- delta.changed_nodes.append(node);
- new_node_indices.append(i);
- continue;
- }
- ++surviving_count;
- if (published->value == node)
- continue;
- delta.changed_nodes.append(node);
- published->value = move(node);
- }
-
- // Only when fewer nodes of the tree sent survive than it had are any of them gone. (A node moved from still has its
- // ID: Moving an integer copies it.)
- if (surviving_count < m_published_nodes.size()) {
- HashTable<i64> current_ids;
- current_ids.ensure_capacity(nodes.size());
- for (auto const& node : nodes)
- current_ids.set(node.id);
- for (auto const& [id, node] : m_published_nodes) {
- if (!current_ids.contains(id))
- delta.removed_ids.append(id);
- }
- for (auto id : delta.removed_ids)
- m_published_nodes.remove(id);
- }
- for (auto i : new_node_indices)
- m_published_nodes.set(nodes[i].id, move(nodes[i]));
-
- if (delta.changed_nodes.is_empty() && delta.removed_ids.is_empty())
- return NothingChanged {};
-
- auto base_sequence = m_sequence;
- return Change { base_sequence, ++m_sequence, move(delta) };
-}
-
-}
diff --git a/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.h b/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.h
deleted file mode 100644
index 49253bd00d7..00000000000
--- a/Libraries/LibWebCommon/WebView/AccessibilityTreePublisher.h
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2026-present, the Ladybird developers.
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/HashMap.h>
-#include <AK/Variant.h>
-#include <AK/Vector.h>
-#include <LibWebCommon/Export.h>
-#include <LibWebCommon/WebView/AccessibilityNodeData.h>
-
-namespace WebView {
-
-// Decides what a page sends the UI process for each accessibility tree it builds: the whole tree when the UI process
-// asks for it or the page has a new document, and otherwise only what changed since the last tree sent. Each tree sent
-// gets the next sequence number, and a change names the number of the tree it applies to, so the UI process can tell
-// when it doesn't hold that tree and has to ask for the whole one again.
-class WEBCOMMON_API AccessibilityTreePublisher {
-public:
- struct WholeTree {
- u64 sequence { 0 };
- Vector<AccessibilityNodeData> nodes;
- };
- struct Change {
- u64 base_sequence { 0 };
- u64 sequence { 0 };
- AccessibilityTreeDelta delta;
- };
- struct NothingChanged { };
- using Update = Variant<NothingChanged, WholeTree, Change>;
-
- // The nodes are a whole tree in tree order, as the document builds it.
- Update publish(i64 document_id, Vector<AccessibilityNodeData> nodes, bool whole_tree_requested);
-
-private:
- Optional<i64> m_document_id;
- HashMap<i64, AccessibilityNodeData> m_published_nodes;
- u64 m_sequence { 0 };
-};
-
-}
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 723403f19fc..d759f706701 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1202,15 +1202,16 @@ void PageClient::publish_accessibility_tree(bool whole_tree_requested)
return;
document->update_layout(Web::DOM::UpdateLayoutReason::InspectAccessibilityTree);
- auto update = m_accessibility_tree_publisher.publish(static_cast<i64>(document->unique_id().value()), document->build_accessibility_node_data(), whole_tree_requested);
- update.visit(
- [](WebView::AccessibilityTreePublisher::NothingChanged) { },
- [&](WebView::AccessibilityTreePublisher::WholeTree& tree) {
- client().async_did_get_accessibility_tree(m_id, tree.sequence, move(tree.nodes));
- },
- [&](WebView::AccessibilityTreePublisher::Change& change) {
- client().async_did_change_accessibility_tree(m_id, change.base_sequence, change.sequence, move(change.delta));
- });
+ auto change = document->build_accessibility_tree_change(m_accessibility_change_tracker, whole_tree_requested);
+ if (change.is_whole_tree) {
+ client().async_did_get_accessibility_tree(m_id, ++m_accessibility_tree_sequence, move(change.nodes));
+ return;
+ }
+ if (change.nodes.is_empty() && change.removed_ids.is_empty())
+ return;
+ auto base_sequence = m_accessibility_tree_sequence++;
+ client().async_did_change_accessibility_tree(m_id, base_sequence, m_accessibility_tree_sequence,
+ WebView::AccessibilityTreeDelta { move(change.nodes), move(change.removed_ids) });
}
void PageClient::page_did_finish_test(Utf16String const& text)
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index cedbbdb8eb6..e58f19ffeb9 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -16,6 +16,7 @@
#include <LibJS/Forward.h>
#include <LibWeb/CSS/StyleSheetIdentifier.h>
#include <LibWeb/ContentSecurityPolicy/Directives/Directive.h>
+#include <LibWeb/DOM/AccessibilityChangeTracker.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/SameDocumentNavigationEntry.h>
#include <LibWeb/HTML/Scripting/ConsoleInputCompletion.h>
@@ -33,7 +34,6 @@
#include <LibWebCommon/WebDriver/Capabilities.h>
#include <LibWebCommon/WebDriver/Response.h>
#include <LibWebCommon/WebDriver/UserPrompt.h>
-#include <LibWebCommon/WebView/AccessibilityTreePublisher.h>
#include <LibWebCommon/WebView/ConsoleOutputBuffer.h>
#include <LibWebCommon/WebView/Geolocation.h>
#include <LibWebCommon/WebView/Mutation.h>
@@ -490,7 +490,10 @@ private:
Optional<Web::HTML::CrossProcessId> m_pending_root_navigable_id;
RefPtr<Core::Timer> m_accessibility_update_timer;
- WebView::AccessibilityTreePublisher m_accessibility_tree_publisher;
+ // What the UI process was last sent of the accessibility tree, and the number of the last tree sent: Each change
+ // names the number of the tree it applies to, so the UI process can tell when it doesn't hold that tree.
+ Web::DOM::AccessibilityChangeTracker m_accessibility_change_tracker;
+ u64 m_accessibility_tree_sequence { 0 };
RefPtr<Core::Timer> m_accessibility_focus_timer;
Optional<i64> m_pending_accessibility_focus;
diff --git a/Tests/LibWeb/Text/expected/accessibility-node-data-geometry.txt b/Tests/LibWeb/Text/expected/accessibility-node-data-geometry.txt
index 6c3bbe86c1c..c77e64e367a 100644
--- a/Tests/LibWeb/Text/expected/accessibility-node-data-geometry.txt
+++ b/Tests/LibWeb/Text/expected/accessibility-node-data-geometry.txt
@@ -1,4 +1,4 @@
-== initial
+== initial (whole tree of 13 nodes)
n0 generic bounds=8,8 784x76
n1 generic bounds=8,8 784x20
n2 text leaf name="above" bounds=8,10 46x16 font="SerenitySans" 16 400 normal color=#000000 characters=0,0 9,0 18,0 29,0 37,0 lines=0@16
@@ -10,7 +10,7 @@ n0 generic bounds=8,8 784x76
n8 generic bounds=8,46 390x36
n9 text leaf name="the text that follows it on the same line, long enough to wrap" bounds=8,46 398x36 font="SerenitySans" 16 400 normal color=#000000 characters=52,0 59,0 68,0 77,0 85,0 92,0 101,0 110,0 117,0 125,0 132,0 141,0 151,0 158,0 166,0 172,0 182,0 187,0 191,0 201,0 212,0 222,0 230,0 234,0 241,0 249,0 260,0 269,0 277,0 284,0 293,0 302,0 310,0 319,0 329,0 341,0 350,0 358,0 362,0 366,0 376,0 385,0 390,0 0,20 4,20 14,20 24,20 31,20 39,20 48,20 58,20 68,20 78,20 85,20 95,20 103,20 109,20 120,20 128,20 139,20 148,20 157,20 lines=0@16 43@16
-== unchanged
+== unchanged (0 nodes changed, 0 removed)
n0 generic bounds=8,8 784x76
n1 generic bounds=8,8 784x20
n2 text leaf name="above" bounds=8,10 46x16 font="SerenitySans" 16 400 normal color=#000000 characters=0,0 9,0 18,0 29,0 37,0 lines=0@16
@@ -22,7 +22,7 @@ n0 generic bounds=8,8 784x76
n8 generic bounds=8,46 390x36
n9 text leaf name="the text that follows it on the same line, long enough to wrap" bounds=8,46 398x36 font="SerenitySans" 16 400 normal color=#000000 characters=52,0 59,0 68,0 77,0 85,0 92,0 101,0 110,0 117,0 125,0 132,0 141,0 151,0 158,0 166,0 172,0 182,0 187,0 191,0 201,0 212,0 222,0 230,0 234,0 241,0 249,0 260,0 269,0 277,0 284,0 293,0 302,0 310,0 319,0 329,0 341,0 350,0 358,0 362,0 366,0 376,0 385,0 390,0 0,20 4,20 14,20 24,20 31,20 39,20 48,20 58,20 68,20 78,20 85,20 95,20 103,20 109,20 120,20 128,20 139,20 148,20 157,20 lines=0@16 43@16
-== container narrowed
+== container narrowed (10 nodes changed, 0 removed)
n0 generic bounds=8,8 784x96
n1 generic bounds=8,8 784x20
n2 text leaf name="above" bounds=8,10 46x16 font="SerenitySans" 16 400 normal color=#000000 characters=0,0 9,0 18,0 29,0 37,0 lines=0@16
@@ -34,7 +34,7 @@ n0 generic bounds=8,8 784x96
n8 generic bounds=8,46 243x56
n9 text leaf name="the text that follows it on the same line, long enough to wrap" bounds=8,46 251x56 font="SerenitySans" 16 400 normal color=#000000 characters=52,0 59,0 68,0 77,0 85,0 92,0 101,0 110,0 117,0 125,0 132,0 141,0 151,0 158,0 166,0 172,0 182,0 187,0 191,0 201,0 212,0 222,0 230,0 234,0 241,0 0,20 10,20 19,20 27,20 34,20 44,20 52,20 60,20 70,20 79,20 91,20 100,20 108,20 112,20 117,20 126,20 135,20 140,20 148,20 152,20 163,20 172,20 180,20 188,20 197,20 206,20 217,20 226,20 234,20 243,20 0,40 6,40 17,40 25,40 36,40 45,40 54,40 lines=0@16 25@16 55@16
-== earlier text on the line grew
+== earlier text on the line grew (11 nodes changed, 0 removed)
n0 generic bounds=8,8 784x116
n1 generic bounds=8,8 784x20
n2 text leaf name="above" bounds=8,10 46x16 font="SerenitySans" 16 400 normal color=#000000 characters=0,0 9,0 18,0 29,0 37,0 lines=0@16
@@ -46,7 +46,7 @@ n0 generic bounds=8,8 784x116
n8 generic bounds=8,46 233x76
n9 text leaf name="the text that follows it on the same line, long enough to wrap" bounds=8,46 241x76 font="SerenitySans" 16 400 normal color=#000000 characters=209,0 215,0 225,0 233,0 0,20 6,20 15,20 25,20 32,20 40,20 47,20 56,20 65,20 72,20 80,20 86,20 97,20 101,20 105,20 116,20 127,20 136,20 144,20 149,20 156,20 164,20 174,20 184,20 192,20 198,20 208,20 216,20 0,40 9,40 18,40 30,40 39,40 47,40 51,40 56,40 66,40 74,40 79,40 87,40 92,40 102,40 111,40 119,40 127,40 136,40 145,40 156,40 166,40 173,40 182,40 190,40 197,40 208,40 0,60 11,60 20,60 29,60 lines=0@16 4@16 32@16 58@16
-== content above grew
+== content above grew (14 nodes changed, 0 removed)
n0 generic bounds=8,8 784x136
n1 generic bounds=8,8 784x40
n2 text leaf name="above" bounds=8,10 46x16 font="SerenitySans" 16 400 normal color=#000000 characters=0,0 9,0 18,0 29,0 37,0 lines=0@16
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-geometry.html b/Tests/LibWeb/Text/input/accessibility-node-data-geometry.html
index 93798779e03..eb9095ed45e 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-geometry.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-geometry.html
@@ -17,15 +17,20 @@
test(() => {
internals.setAccessibilityInterested();
const root = document.getElementById("root");
+ // Printing adds to the DOM, so the output waits until the last step: Otherwise, each step's change would include
+ // the previous step's output.
+ const output = [];
const step = (label, change) => {
change();
- // Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
+ const changeSize = internals.accessibilityLastChangeSize();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
- println(`== ${label}`);
- println(dump);
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
+ output.push(`== ${label} (${changeSize})`, dump);
if (stale)
- println(`stale cached data:\n${stale}`);
+ output.push(`stale cached data:\n${stale}`);
};
step("initial", () => {});
@@ -33,5 +38,7 @@
step("container narrowed", () => document.getElementById("container").style.width = "250px");
step("earlier text on the line grew", () => document.getElementById("first").firstChild.data = "much longer than before");
step("content above grew", () => document.getElementById("above").append(document.createElement("br"), "and more"));
+ for (const line of output)
+ println(line);
});
</script>
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-indirect.html b/Tests/LibWeb/Text/input/accessibility-node-data-indirect.html
index c47771ece64..66a3da073a4 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-indirect.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-indirect.html
@@ -36,8 +36,11 @@
const step = (label, change) => {
change();
// Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
println(`== ${label}`);
println(dump);
if (stale)
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-names.html b/Tests/LibWeb/Text/input/accessibility-node-data-names.html
index b4a99481b97..6f71e8eb69f 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-names.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-names.html
@@ -39,8 +39,11 @@
const step = (label, change) => {
change();
// Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
println(`== ${label}`);
println(dump);
if (stale)
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-states.html b/Tests/LibWeb/Text/input/accessibility-node-data-states.html
index 9a254a1f784..eb505db4bfc 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-states.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-states.html
@@ -23,8 +23,11 @@
const step = (label, change) => {
change();
// Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
println(`== ${label}`);
println(dump);
if (stale)
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-structure.html b/Tests/LibWeb/Text/input/accessibility-node-data-structure.html
index 7d13c10de5e..46873af4598 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-structure.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-structure.html
@@ -25,8 +25,11 @@
const step = (label, change) => {
change();
// Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
println(`== ${label}`);
println(dump);
if (stale)
diff --git a/Tests/LibWeb/Text/input/accessibility-node-data-tables-and-geometry.html b/Tests/LibWeb/Text/input/accessibility-node-data-tables-and-geometry.html
index 196271bd6b5..acce566ed84 100644
--- a/Tests/LibWeb/Text/input/accessibility-node-data-tables-and-geometry.html
+++ b/Tests/LibWeb/Text/input/accessibility-node-data-tables-and-geometry.html
@@ -27,8 +27,11 @@
const step = (label, change) => {
change();
// Printing adds to the DOM, so read everything before printing anything.
+ // The change tracker comes first, so that it builds the tree the way publishing does: before anything else
+ // keeps what changed.
+ const tracking = internals.accessibilityChangeTrackingMismatches();
const dump = internals.dumpAccessibilityNodeData(root);
- const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches();
+ const stale = internals.accessibilityCachedTextMismatches() + internals.accessibilityCachedRecordMismatches() + internals.accessibilityCachedGeometryMismatches() + tracking;
println(`== ${label}`);
println(dump);
if (stale)
diff --git a/Tests/LibWebView/TestAccessibilityTreeManager.cpp b/Tests/LibWebView/TestAccessibilityTreeManager.cpp
index 3ed5315a39d..5511d4f0d19 100644
--- a/Tests/LibWebView/TestAccessibilityTreeManager.cpp
+++ b/Tests/LibWebView/TestAccessibilityTreeManager.cpp
@@ -5,8 +5,8 @@
*/
#include <AK/HashTable.h>
+#include <AK/Variant.h>
#include <LibTest/TestCase.h>
-#include <LibWebCommon/WebView/AccessibilityTreePublisher.h>
#include <LibWebView/AccessibilityTreeManager.h>
namespace {
@@ -516,7 +516,86 @@ TEST_CASE(delta_announces_live_region_changes)
namespace {
-using Publisher = WebView::AccessibilityTreePublisher;
+// A stand-in for WebContent that sends each whole tree once, and then only what changed in it, found by comparing it
+// with the last tree sent: what the manager has to apply.
+class Publisher {
+public:
+ struct WholeTree {
+ u64 sequence { 0 };
+ Vector<WebView::AccessibilityNodeData> nodes;
+ };
+ struct Change {
+ u64 base_sequence { 0 };
+ u64 sequence { 0 };
+ WebView::AccessibilityTreeDelta delta;
+ };
+ struct NothingChanged { };
+ using Update = Variant<NothingChanged, WholeTree, Change>;
+
+ Update publish(i64 document_id, Vector<WebView::AccessibilityNodeData> nodes, bool whole_tree_requested)
+ {
+ auto remember = [&](Vector<WebView::AccessibilityNodeData> const& published) {
+ m_document_id = document_id;
+ m_published_nodes.clear();
+ m_published_nodes.ensure_capacity(published.size());
+ for (auto const& node : published)
+ m_published_nodes.set(node.id, node);
+ };
+
+ if (whole_tree_requested || m_document_id != document_id) {
+ remember(nodes);
+ return WholeTree { ++m_sequence, move(nodes) };
+ }
+
+ // Every node that's new or whose data changed goes in the change, in tree order. A node whose children changed
+ // is among them, so a node that moved is listed by its new parent, and dropped by its old one.
+ WebView::AccessibilityTreeDelta delta;
+ Vector<size_t> new_node_indices;
+ size_t surviving_count = 0;
+ for (size_t i = 0; i < nodes.size(); ++i) {
+ auto& node = nodes[i];
+ auto published = m_published_nodes.find(node.id);
+ if (published == m_published_nodes.end()) {
+ delta.changed_nodes.append(node);
+ new_node_indices.append(i);
+ continue;
+ }
+ ++surviving_count;
+ if (published->value == node)
+ continue;
+ delta.changed_nodes.append(node);
+ published->value = move(node);
+ }
+
+ // Only when fewer nodes of the tree sent survive than it had are any of them gone. (A node moved from still has
+ // its ID: Moving an integer copies it.)
+ if (surviving_count < m_published_nodes.size()) {
+ HashTable<i64> current_ids;
+ current_ids.ensure_capacity(nodes.size());
+ for (auto const& node : nodes)
+ current_ids.set(node.id);
+ for (auto const& [id, node] : m_published_nodes) {
+ if (!current_ids.contains(id))
+ delta.removed_ids.append(id);
+ }
+ for (auto id : delta.removed_ids)
+ m_published_nodes.remove(id);
+ }
+ for (auto i : new_node_indices)
+ m_published_nodes.set(nodes[i].id, move(nodes[i]));
+
+ if (delta.changed_nodes.is_empty() && delta.removed_ids.is_empty())
+ return NothingChanged {};
+
+ auto base_sequence = m_sequence;
+ return Change { base_sequence, ++m_sequence, move(delta) };
+ }
+
+private:
+ Optional<i64> m_document_id;
+ HashMap<i64, WebView::AccessibilityNodeData> m_published_nodes;
+ u64 m_sequence { 0 };
+};
Vector<WebView::AccessibilityNodeData> base_tree_nodes()
{
@@ -544,41 +623,6 @@ bool deliver(WebView::AccessibilityTreeManager& manager, Publisher::Update updat
}
-TEST_CASE(publisher_sends_the_whole_tree_only_when_it_has_to)
-{
- Publisher publisher;
-
- auto first = publisher.publish(7, base_tree_nodes(), false);
- EXPECT(first.has<Publisher::WholeTree>());
- EXPECT_EQ(first.get<Publisher::WholeTree>().sequence, 1u);
- EXPECT_EQ(first.get<Publisher::WholeTree>().nodes.size(), 4u);
-
- EXPECT(publisher.publish(7, base_tree_nodes(), false).has<Publisher::NothingChanged>());
-
- // Rename 3 and remove 2: The change holds just 3 and the root, whose children changed, and names 2 as removed.
- auto nodes = base_tree_nodes();
- nodes[0].child_ids = { 1 };
- nodes.remove(2);
- nodes[2].name = "renamed"_string;
- auto second = publisher.publish(7, nodes, false);
- EXPECT(second.has<Publisher::Change>());
- auto const& change = second.get<Publisher::Change>();
- EXPECT_EQ(change.base_sequence, 1u);
- EXPECT_EQ(change.sequence, 2u);
- EXPECT_EQ(change.delta.changed_nodes.size(), 2u);
- EXPECT_EQ(change.delta.changed_nodes[0].id, 0);
- EXPECT_EQ(change.delta.changed_nodes[1].id, 3);
- EXPECT_EQ(change.delta.removed_ids, Vector<i64> { 2 });
-
- // The whole tree goes out when the UI process asks for it, and for a new document.
- auto requested = publisher.publish(7, nodes, true);
- EXPECT(requested.has<Publisher::WholeTree>());
- EXPECT_EQ(requested.get<Publisher::WholeTree>().sequence, 3u);
- auto new_document = publisher.publish(8, nodes, false);
- EXPECT(new_document.has<Publisher::WholeTree>());
- EXPECT_EQ(new_document.get<Publisher::WholeTree>().sequence, 4u);
-}
-
TEST_CASE(change_to_a_tree_the_manager_does_not_hold_is_rejected)
{
Publisher publisher;