Skip to content

Truly independent web browser

UI/Qt: Report AXFocused on the web area while it has the focus

sideshowbarker99c48b3c8b2d
diff --git a/Documentation/ScreenReaderSupport.md b/Documentation/ScreenReaderSupport.md
index 39a72e9c6b2..297c22f8223 100644
--- a/Documentation/ScreenReaderSupport.md
+++ b/Documentation/ScreenReaderSupport.md
@@ -253,7 +253,7 @@ On the `AXWebArea` itself, three text-marker-related attributes work together to
Beyond text markers, the document `AXWebArea` element exposes a small set of attributes that VoiceOver uses to recognize it as the active reading target after page load:
-- `AXFocused`: returns `YES` when the containing view is window first responder. VoiceOver uses this to identify which web area is currently the active reading target.
+- `AXFocused`: returns `YES` exactly when the `AXWebArea` is the page’s focused UI element — that is, when nothing in the page has the focus. VoiceOver expects the element the application reports as focused to say so itself. When it doesn’t, VoiceOver sets `AXFocused` on it, and then again after every tree update — announcing “web content” each time.
- `AXURL`: the page URL. VoiceOver compares this against its own page-identity tracking — to decide whether the load is a new page worth re-reading.
- `AXVisibleCharacterRange`: a non-empty range — so that VoiceOver doesn’t treat the `AXWebArea` as skippable empty content.
diff --git a/Tests/LibWeb/AccessibilityBridgeMac/tests/test_states.py b/Tests/LibWeb/AccessibilityBridgeMac/tests/test_states.py
index d614d74ebb3..1432bbc1419 100644
--- a/Tests/LibWeb/AccessibilityBridgeMac/tests/test_states.py
+++ b/Tests/LibWeb/AccessibilityBridgeMac/tests/test_states.py
@@ -4,7 +4,7 @@ The macOS surface is narrower than AT-SPI2 here: no platform-level "focusable" b
"sensitive" state. The wrapper exposes:
* AXEnabled ← *not* data->is_disabled
-* AXFocused ← data->is_focused (or view-is-first-responder, for the document)
+* AXFocused ← data->is_focused (or, for the document, being the focused UI element)
* AXPress action ← for button/link/checkbox/radio/menuitem/tab roles only"""
from __future__ import annotations
@@ -16,10 +16,14 @@ import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from ApplicationServices import AXUIElementCopyActionNames # noqa: E402
+from ApplicationServices import AXUIElementSetAttributeValue # noqa: E402
from ApplicationServices import kAXEnabledAttribute # noqa: E402
from ApplicationServices import kAXFocusedAttribute # noqa: E402
+from ApplicationServices import kAXFocusedUIElementAttribute # noqa: E402
+from ApplicationServices import kAXRoleAttribute # noqa: E402
from ApplicationServices import kAXValueAttribute # noqa: E402
from harness import AccessibilityBridgeMacTestCase # noqa: E402
+from harness import wait_for # noqa: E402
from harness import wait_for_descendant_by_role # noqa: E402
from harness.ladybird import _ax_attr # noqa: E402
@@ -73,8 +77,8 @@ class EnabledStateTests(AccessibilityBridgeMacTestCase):
class FocusedStateTests(AccessibilityBridgeMacTestCase):
- """AXFocused on individual elements tracks data->is_focused; the document is special-cased to mirror
- view-is-first-responder."""
+ """AXFocused on individual elements tracks data->is_focused; the document reports it exactly when it's the page's
+ focused UI element."""
FIXTURE = "states.html"
@@ -86,6 +90,34 @@ class FocusedStateTests(AccessibilityBridgeMacTestCase):
# Could be either False or NO (False); the wrapper returns @YES/@NO which PyObjC marshals to True/False.
self.assertEqual(_ax_attr(btn, kAXFocusedAttribute), False)
+ def test_web_area_is_focused_while_nothing_in_the_page_is(self):
+ """With nothing in the page focused, the application's focused UI element is the AXWebArea — and the web area
+ reports AXFocused=YES itself. Defends the regression where it reported NO: VoiceOver then set AXFocused on it
+ after every tree update, announcing the web area over and over and never reading the page."""
+ focused = wait_for(
+ lambda: _ax_attr(self.app, kAXFocusedUIElementAttribute), description="the application's focused element"
+ )
+ self.assertIsNotNone(focused, "the application reports no focused UI element")
+ self.assertEqual(_ax_attr(focused, kAXRoleAttribute), "AXWebArea")
+ self.assertEqual(_ax_attr(focused, kAXFocusedAttribute), True)
+ self.assertEqual(_ax_attr(self.web, kAXFocusedAttribute), True)
+
+
+class FocusedElementStateTests(AccessibilityBridgeMacTestCase):
+ """Once an element in the page has the focus, it alone reports AXFocused=YES — not the AXWebArea too."""
+
+ FIXTURE = "states.html"
+
+ def test_web_area_is_not_focused_while_an_element_is(self):
+ btn = wait_for_descendant_by_role(self.web, "AXButton", title="Normal button")
+ self.assertIsNotNone(btn)
+ self.assertEqual(AXUIElementSetAttributeValue(btn, kAXFocusedAttribute, True), 0, "setting AXFocused failed")
+ self.assertTrue(
+ wait_for(lambda: _ax_attr(btn, kAXFocusedAttribute) is True, description="the button to take focus"),
+ "the button never reported AXFocused",
+ )
+ self.assertEqual(_ax_attr(self.web, kAXFocusedAttribute), False)
+
class ActionNameTests(AccessibilityBridgeMacTestCase):
"""accessibilityActionNames — read externally via AXUIElementCopyActionNames.
diff --git a/UI/Qt/LadybirdAccessibilityElement.mm b/UI/Qt/LadybirdAccessibilityElement.mm
index 807771a79f2..71415e1267a 100644
--- a/UI/Qt/LadybirdAccessibilityElement.mm
+++ b/UI/Qt/LadybirdAccessibilityElement.mm
@@ -498,11 +498,11 @@ static NSString* nsStringFromAK(String const& string)
if ([attribute isEqualToString:NSAccessibilityFocusedAttribute]) {
if (!data)
return @NO;
- // The document AXWebArea reports focused when its containing view is first responder. VoiceOver uses this to
- // know which web area is the active reading target on the page. Without it, AXLoadComplete fires — but
- // VoiceOver doesn't move its cursor into the web content.
+ // The document AXWebArea is focused exactly when it's the page's focused UI element: when nothing in the page
+ // has the focus. VoiceOver expects the element reported as focused to say so itself — and when it doesn't,
+ // VoiceOver sets AXFocused on it, and again after every tree update, announcing the web area each time.
if (data->role.bytes_as_string_view() == "document"sv)
- return [_view accessibilityViewIsFirstResponder] ? @YES : @NO;
+ return [_view accessibilityFocusedUIElement] == self ? @YES : @NO;
return data->is_focused ? @YES : @NO;
}
diff --git a/UI/Qt/LadybirdAccessibilityViewProtocol.h b/UI/Qt/LadybirdAccessibilityViewProtocol.h
index e430f88b290..e3891325b7b 100644
--- a/UI/Qt/LadybirdAccessibilityViewProtocol.h
+++ b/UI/Qt/LadybirdAccessibilityViewProtocol.h
@@ -16,7 +16,7 @@
- (NSRect)accessibilityViewRectForScreenPoint:(NSPoint)screenPoint;
- (void)performAccessibilityAction:(NSString*)action forNodeID:(int64_t)nodeID;
- (NSURL*)accessibilityPageURL;
-- (BOOL)accessibilityViewIsFirstResponder;
+- (id)accessibilityFocusedUIElement;
- (NSWindow*)window;
@end
diff --git a/UI/Qt/WebContentViewAccessibility.mm b/UI/Qt/WebContentViewAccessibility.mm
index 450e38a613c..d2c4586faac 100644
--- a/UI/Qt/WebContentViewAccessibility.mm
+++ b/UI/Qt/WebContentViewAccessibility.mm
@@ -130,11 +130,6 @@ extern "C" void NSAccessibilityHandleFocusChanged();
return [NSURL URLWithString:ns_string];
}
-- (BOOL)accessibilityViewIsFirstResponder
-{
- return [[self window] firstResponder] == self;
-}
-
// NSAccessibility: scroll area containing the web content root
- (BOOL)isAccessibilityElement