Truly independent web browser
UI/Qt: Have AppKit identify accessibility elements by a unique id
sideshowbarker3e6e247d5709
diff --git a/Documentation/ScreenReaderSupport.md b/Documentation/ScreenReaderSupport.mdindex 129754923d4..9ba7663b2d9 100644--- a/Documentation/ScreenReaderSupport.md+++ b/Documentation/ScreenReaderSupport.md@@ -167,6 +167,8 @@ Chromium uses `NSAccessibilityElement` as its base class. We tried that but enco`NSObject` with the “informal” protocol gives us full control. Tradeoff: we have to override certain “modern” property API methods that `NSAccessibilityElement` would provide automatically. In particular, we found that missing overrides for `accessibilityWindow` and `accessibilityTopLevelUIElement` caused VoiceOver to treat nested elements as orphaned (not part of any window) — breaking VoiceOver forward navigation into nested groups.+Likewise `accessibilityShouldUseUniqueId`, which `NSAccessibilityElement` answers `YES` and `NSObject` answers `NO`. Without a unique id, AppKit identifies an element to an assistive technology by its path of child indices from the window: Every request resolves that path again, asking each ancestor for its `AXChildren` on the way down — enough work on a big page for VoiceOver to report “Ladybird not responding”. And a handle VoiceOver holds then names a position, not an element. So `LadybirdAccessibilityElement` answers `YES`, as WebKit’s wrappers do, and gives the id up (`NSAccessibilityUnregisterUniqueIdForUIElement`) when its node leaves the tree or it’s deallocated — AppKit’s table of ids doesn’t retain the elements.+### Dual API support: “informal” and “modern”The macOS accessibility system queries objects via two code paths:diff --git a/Tests/LibWeb/AccessibilityBridge/input/sibling_insert.html b/Tests/LibWeb/AccessibilityBridge/input/sibling_insert.htmlnew file mode 100644index 00000000000..39662a1f0c4--- /dev/null+++ b/Tests/LibWeb/AccessibilityBridge/input/sibling_insert.html@@ -0,0 +1,15 @@+<!DOCTYPE html>+<html>+<body style="font: 16px sans-serif; margin: 40px;">+<button id="insert">Insert</button>+<button>Alpha</button>+<button>Beta</button>+<script>+ document.getElementById("insert").addEventListener("click", () => {+ const inserted = document.createElement("button");+ inserted.textContent = "Inserted";+ document.getElementById("insert").after(inserted);+ });+</script>+</body>+</html>diff --git a/Tests/LibWeb/AccessibilityBridgeMac/tests/test_regressions.py b/Tests/LibWeb/AccessibilityBridgeMac/tests/test_regressions.pyindex dd9ea716c90..79718056472 100644--- a/Tests/LibWeb/AccessibilityBridgeMac/tests/test_regressions.py+++ b/Tests/LibWeb/AccessibilityBridgeMac/tests/test_regressions.py@@ -15,12 +15,14 @@ sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))from ApplicationServices import AXUIElementCopyAttributeValue # noqa: E402from ApplicationServices import AXUIElementIsAttributeSettable # noqa: E402+from ApplicationServices import AXUIElementPerformAction # noqa: E402from ApplicationServices import AXUIElementSetAttributeValue # noqa: E402from ApplicationServices import kAXFocusedAttribute # noqa: E402from ApplicationServices import kAXParentAttribute # noqa: E402from ApplicationServices import kAXPositionAttribute # noqa: E402from ApplicationServices import kAXRoleAttribute # noqa: E402from ApplicationServices import kAXSizeAttribute # noqa: E402+from ApplicationServices import kAXTitleAttribute # noqa: E402from ApplicationServices import kAXValueAttribute # noqa: E402from harness import AccessibilityBridgeMacTestCase # noqa: E402from harness import NotificationCollector # noqa: E402@@ -585,5 +587,39 @@ class TreeUpdatesUnderContinuousMutationTests(AccessibilityBridgeMacTestCase):self.assertNotEqual(first, second, f"the tree stopped updating: the status text still reads {first!r}")+class ElementKeepsItsIdentityAcrossSiblingInsertionTests(AccessibilityBridgeMacTestCase):+ """Symptom: VoiceOver kept reporting "Ladybird not responding" while the user navigated a page, and its cursor could+ land on the wrong element after the page changed.++ Root cause: the elements didn't ask AppKit for unique ids, so AppKit identified each one to an AT by its path of+ child indices from the window. Every request resolved that path again, computing the AXChildren of each ancestor+ on the way down: Seconds per navigation step on a big page, since the AXWebArea's children skip through every+ unnamed div and paragraph. And a handle the AT held named a position, not an element: Once a sibling was inserted+ before it, the handle resolved to whatever element had moved into its place.++ Fix: every element uses a unique id (accessibilityShouldUseUniqueId), and gives it up when its node leaves the+ tree."""++ FIXTURE = "sibling_insert.html"++ def test_handle_still_names_its_element_after_a_sibling_is_inserted_before_it(self):+ insert = wait_for_descendant_by_role(self.web, "AXButton", title="Insert")+ beta = wait_for_descendant_by_role(self.web, "AXButton", title="Beta")+ self.assertIsNotNone(insert, "sibling_insert.html must expose its Insert button")+ self.assertIsNotNone(beta, "sibling_insert.html must expose its Beta button")++ err = AXUIElementPerformAction(insert, "AXPress")+ self.assertEqual(err, 0, "pressing the Insert button failed")+ self.assertIsNotNone(+ wait_for_descendant_by_role(self.ctx.find_web_area(), "AXButton", title="Inserted", timeout=10.0),+ "the inserted button never reached the tree",+ )+ self.assertEqual(+ _ax_attr(beta, kAXTitleAttribute),+ "Beta",+ "the handle to the Beta button now names another element: the element is identified by its position",+ )++if __name__ == "__main__":unittest.main()diff --git a/UI/Qt/LadybirdAccessibilityElement.mm b/UI/Qt/LadybirdAccessibilityElement.mmindex 71415e1267a..049a7b58d2f 100644--- a/UI/Qt/LadybirdAccessibilityElement.mm+++ b/UI/Qt/LadybirdAccessibilityElement.mm@@ -16,6 +16,8 @@#include <ApplicationServices/ApplicationServices.h>+extern "C" void NSAccessibilityUnregisterUniqueIdForUIElement(id element);+// Text marker: encodes a node ID and character offset for cursor-level text navigation.struct TextMarkerData {i64 node_id { -1 };@@ -215,12 +217,31 @@ static NSString* nsStringFromAK(String const& string)return _manager->node(_nodeID);}+- (void)dealloc+{+ // An element the tree dropped gave up its id then (invalidate); one that goes with its view gives it up here.+ if (_manager)+ NSAccessibilityUnregisterUniqueIdForUIElement(self);+}+- (void)invalidate{+ // AppKit's table of ids doesn't retain the elements, so give up the id while this element is still alive — and an+ // AT holding this element learns that it's gone.+ NSAccessibilityUnregisterUniqueIdForUIElement(self);_manager = nullptr;_view = nil;}+// Without a unique id, AppKit identifies an element to an AT by its path of child indices from the window. So every+// request resolves that path again, computing the AXChildren of each ancestor on the way down — and the AXWebArea's+// children skip through every unnamed div and paragraph in the page. And a handle the AT holds then names a position,+// not an element: Once a sibling is inserted before the element, it resolves to whatever moved into its place.+- (BOOL)accessibilityShouldUseUniqueId+{+ return YES;+}+#pragma mark - NSAccessibility informal protocol- (BOOL)shouldGroupAccessibilityChildren