Skip to content

Commit f1a0549

Browse files
kevmooflutter-zl
andauthored
[web] Propagate aria-label to inner slider input (#192964)
On Flutter Web, `SemanticIncrementable` (`role="slider"`) renders an outer `<flt-semantics>` container alongside an inner `<input type="range" role="slider">`. Previously, `AriaLabelRepresentation` set `aria-label` exclusively on the outer `<flt-semantics>` wrapper (`role="none"` when active), leaving the interactive `<input role="slider">` element without an accessible name in the browser's accessibility tree (`name: ""` in Chrome `Accessibility.getFullAXTree` and `"50%, slider"` in VoiceOver). This change: 1. Propagates `semanticsObject.label` to the inner `<input role="slider">` element's `aria-label` attribute in `SemanticIncrementable.update()`, removing it when the label becomes empty. 2. Tracks `_previousLabel` in `AriaLabelRepresentation` so stale `aria-label` attributes are cleaned up when a label is cleared. 3. Adds unit tests in `semantics_test.dart` (`_testIncrementables()`) verifying `aria-label` propagation and cleanup on `<input role="slider">`. Fixes http://b/463248173 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: zhongliugo <[email protected]>
1 parent b959896 commit f1a0549

4 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/incrementable.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ class SemanticIncrementable extends SemanticRole {
122122
void update() {
123123
super.update();
124124

125+
final String? ariaLabel = element.getAttribute('aria-label');
126+
if (ariaLabel != null && ariaLabel.trim().isNotEmpty) {
127+
_element.setAttribute('aria-label', ariaLabel);
128+
} else {
129+
_element.removeAttribute('aria-label');
130+
}
131+
125132
switch (EngineSemantics.instance.gestureMode) {
126133
case GestureMode.browserGestures:
127134
_enableBrowserGestureHandling();

‎engine/src/flutter/lib/web_ui/lib/src/engine/semantics/label_and_value.dart‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ final class AriaLabelRepresentation extends LabelRepresentationBehavior {
120120
if (label == _previousLabel) {
121121
return;
122122
}
123+
_previousLabel = label;
123124
owner.setAttribute('aria-label', label);
124125
}
125126

126127
@override
127128
void cleanUp() {
129+
_previousLabel = null;
128130
owner.removeAttribute('aria-label');
129131
}
130132

‎engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_multi_view_test.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Future<void> testMain() async {
107107
</sem>''');
108108
expectSemanticsTree(view2.semantics, '''
109109
<sem style="filter: opacity(0%); color: rgba(0, 0, 0, 0)">
110-
<sem aria-label="d"><input aria-valuemax="1" aria-valuemin="1" aria-valuenow="1" aria-valuetext="" role="slider"></sem>
110+
<sem aria-label="d"><input aria-label="d" aria-valuemax="1" aria-valuemin="1" aria-valuenow="1" aria-valuetext="" role="slider"></sem>
111111
</sem>
112112
''');
113113

‎engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,6 +2932,39 @@ void _testIncrementables() {
29322932

29332933
semantics().semanticsEnabled = false;
29342934
});
2935+
2936+
test('propagates aria-label to inner slider input and cleans up when cleared', () async {
2937+
semantics()
2938+
..debugOverrideTimestampFunction(() => _testTime)
2939+
..semanticsEnabled = true;
2940+
addTearDown(() {
2941+
semantics().semanticsEnabled = false;
2942+
});
2943+
2944+
void pumpSlider({required String label}) {
2945+
final tester = SemanticsTester(owner());
2946+
tester.updateNode(
2947+
id: 0,
2948+
label: label,
2949+
hasIncrease: true,
2950+
hasDecrease: true,
2951+
flags: const ui.SemanticsFlags(isEnabled: ui.Tristate.isTrue),
2952+
value: '50%',
2953+
increasedValue: '60%',
2954+
decreasedValue: '40%',
2955+
transform: Matrix4.identity().toFloat64(),
2956+
rect: const ui.Rect.fromLTRB(0, 0, 100, 50),
2957+
);
2958+
tester.apply();
2959+
}
2960+
2961+
pumpSlider(label: 'Volume');
2962+
final DomElement input = owner().debugSemanticsTree![0]!.element.querySelector('input')!;
2963+
expect(input.getAttribute('aria-label'), 'Volume');
2964+
2965+
pumpSlider(label: '');
2966+
expect(input.getAttribute('aria-label'), isNull);
2967+
});
29352968
}
29362969

29372970
void _testTextField() {

0 commit comments

Comments
 (0)