Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 745ef77

Browse files
committed
Reapply "[web:a11y] make header a proper <header> (#55747)" (#55993)
This reverts commit d302cc9.
1 parent d302cc9 commit 745ef77

File tree

6 files changed

+77
-20
lines changed

6 files changed

+77
-20
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43886,6 +43886,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics.dart + ../../../flu
4388643886
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/accessibility.dart + ../../../flutter/LICENSE
4388743887
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart + ../../../flutter/LICENSE
4388843888
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/focusable.dart + ../../../flutter/LICENSE
43889+
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/header.dart + ../../../flutter/LICENSE
4388943890
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/heading.dart + ../../../flutter/LICENSE
4389043891
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/image.dart + ../../../flutter/LICENSE
4389143892
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/semantics/incrementable.dart + ../../../flutter/LICENSE
@@ -46764,6 +46765,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics.dart
4676446765
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/accessibility.dart
4676546766
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/checkable.dart
4676646767
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/focusable.dart
46768+
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/header.dart
4676746769
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/heading.dart
4676846770
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/image.dart
4676946771
FILE: ../../../flutter/lib/web_ui/lib/src/engine/semantics/incrementable.dart

lib/web_ui/lib/src/engine.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export 'engine/scene_view.dart';
147147
export 'engine/semantics/accessibility.dart';
148148
export 'engine/semantics/checkable.dart';
149149
export 'engine/semantics/focusable.dart';
150+
export 'engine/semantics/header.dart';
150151
export 'engine/semantics/heading.dart';
151152
export 'engine/semantics/image.dart';
152153
export 'engine/semantics/incrementable.dart';

lib/web_ui/lib/src/engine/semantics.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
export 'semantics/accessibility.dart';
66
export 'semantics/checkable.dart';
77
export 'semantics/focusable.dart';
8+
export 'semantics/header.dart';
89
export 'semantics/heading.dart';
910
export 'semantics/image.dart';
1011
export 'semantics/incrementable.dart';
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import '../dom.dart';
6+
import 'label_and_value.dart';
7+
import 'semantics.dart';
8+
9+
/// Renders a semantic header.
10+
///
11+
/// A header is a group of nodes that together introduce the content of the
12+
/// current screen or page.
13+
///
14+
/// Uses the `<header>` element, which implies ARIA role "banner".
15+
///
16+
/// See also:
17+
/// * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/header
18+
/// * https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/banner_role
19+
class SemanticHeader extends SemanticRole {
20+
SemanticHeader(SemanticsObject semanticsObject) : super.withBasics(
21+
SemanticRoleKind.header,
22+
semanticsObject,
23+
24+
// Why use sizedSpan?
25+
//
26+
// On an empty <header> aria-label alone will read the label but also add
27+
// "empty banner". Additionally, if the label contains information that's
28+
// meant to be crawlable, it will be lost by moving into aria-label, because
29+
// most crawlers ignore ARIA labels.
30+
//
31+
// Using DOM text, such as <header>DOM text</header> causes the browser to
32+
// generate two a11y nodes, one for the <header> element, and one for the
33+
// "DOM text" text node. The text node is sized according to the text size,
34+
// and does not match the size of the <header> element, which is the same
35+
// issue as https://github.com/flutter/flutter/issues/146774.
36+
preferredLabelRepresentation: LabelRepresentation.sizedSpan,
37+
);
38+
39+
@override
40+
DomElement createElement() => createDomElement('header');
41+
42+
@override
43+
bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false;
44+
}

lib/web_ui/lib/src/engine/semantics/semantics.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import '../window.dart';
2121
import 'accessibility.dart';
2222
import 'checkable.dart';
2323
import 'focusable.dart';
24+
import 'header.dart';
2425
import 'heading.dart';
2526
import 'image.dart';
2627
import 'incrementable.dart';
@@ -396,14 +397,17 @@ enum SemanticRoleKind {
396397
/// The node's role is to host a platform view.
397398
platformView,
398399

400+
/// Contains a link.
401+
link,
402+
403+
/// Denotes a header.
404+
header,
405+
399406
/// A role used when a more specific role cannot be assigend to
400407
/// a [SemanticsObject].
401408
///
402409
/// Provides a label or a value.
403410
generic,
404-
405-
/// Contains a link.
406-
link,
407411
}
408412

409413
/// Responsible for setting the `role` ARIA attribute, for attaching
@@ -688,23 +692,18 @@ final class GenericRole extends SemanticRole {
688692
return;
689693
}
690694

691-
// Assign one of three roles to the element: group, heading, text.
695+
// Assign one of two roles to the element: group or text.
692696
//
693697
// - "group" is used when the node has children, irrespective of whether the
694698
// node is marked as a header or not. This is because marking a group
695699
// as a "heading" will prevent the AT from reaching its children.
696-
// - "heading" is used when the framework explicitly marks the node as a
697-
// heading and the node does not have children.
698700
// - If a node has a label and no children, assume is a paragraph of text.
699701
// In HTML text has no ARIA role. It's just a DOM node with text inside
700702
// it. Previously, role="text" was used, but it was only supported by
701703
// Safari, and it was removed starting Safari 17.
702704
if (semanticsObject.hasChildren) {
703705
labelAndValue!.preferredRepresentation = LabelRepresentation.ariaLabel;
704706
setAriaRole('group');
705-
} else if (semanticsObject.hasFlag(ui.SemanticsFlag.isHeader)) {
706-
labelAndValue!.preferredRepresentation = LabelRepresentation.domText;
707-
setAriaRole('heading');
708707
} else {
709708
labelAndValue!.preferredRepresentation = LabelRepresentation.sizedSpan;
710709
removeAttribute('role');
@@ -1272,11 +1271,24 @@ class SemanticsObject {
12721271
bool get isTextField => hasFlag(ui.SemanticsFlag.isTextField);
12731272

12741273
/// Whether this object represents a heading element.
1274+
///
1275+
/// Typically, a heading is a prominent piece of text that describes what the
1276+
/// rest of the screen or page is about.
1277+
///
1278+
/// Not to be confused with [isHeader].
12751279
bool get isHeading => headingLevel != 0;
12761280

1277-
/// Whether this object represents an editable text field.
1281+
/// Whether this object represents an interactive link.
12781282
bool get isLink => hasFlag(ui.SemanticsFlag.isLink);
12791283

1284+
/// Whether this object represents a header.
1285+
///
1286+
/// A header is a group of widgets that introduce the content of the screen
1287+
/// or a page.
1288+
///
1289+
/// Not to be confused with [isHeading].
1290+
bool get isHeader => hasFlag(ui.SemanticsFlag.isHeader);
1291+
12801292
/// Whether this object needs screen readers attention right away.
12811293
bool get isLiveRegion =>
12821294
hasFlag(ui.SemanticsFlag.isLiveRegion) &&
@@ -1690,6 +1702,8 @@ class SemanticsObject {
16901702
return SemanticRoleKind.route;
16911703
} else if (isLink) {
16921704
return SemanticRoleKind.link;
1705+
} else if (isHeader) {
1706+
return SemanticRoleKind.header;
16931707
} else {
16941708
return SemanticRoleKind.generic;
16951709
}
@@ -1707,6 +1721,7 @@ class SemanticsObject {
17071721
SemanticRoleKind.platformView => SemanticPlatformView(this),
17081722
SemanticRoleKind.link => SemanticLink(this),
17091723
SemanticRoleKind.heading => SemanticHeading(this),
1724+
SemanticRoleKind.header => SemanticHeader(this),
17101725
SemanticRoleKind.generic => GenericRole(this),
17111726
};
17121727
}

lib/web_ui/test/engine/semantics/semantics_test.dart

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ class MockSemanticsEnabler implements SemanticsEnabler {
739739
}
740740

741741
void _testHeader() {
742-
test('renders heading role for headers', () {
742+
test('renders a header with a label and uses a sized span for label', () {
743743
semantics()
744744
..debugOverrideTimestampFunction(() => _testTime)
745745
..semanticsEnabled = true;
@@ -755,19 +755,13 @@ void _testHeader() {
755755

756756
owner().updateSemantics(builder.build());
757757
expectSemanticsTree(owner(), '''
758-
<sem role="heading">Header of the page</sem>
758+
<header><span>Header of the page</span></header>
759759
''');
760760

761761
semantics().semanticsEnabled = false;
762762
});
763763

764-
// When a header has child elements, role="heading" prevents AT from reaching
765-
// child elements. To fix that role="group" is used, even though that causes
766-
// the heading to not be announced as a heading. If the app really needs the
767-
// heading to be announced as a heading, the developer can restructure the UI
768-
// such that the heading is not a parent node, but a side-note, e.g. preceding
769-
// the child list.
770-
test('uses group role for headers when children are present', () {
764+
test('renders a header with children and uses aria-label', () {
771765
semantics()
772766
..debugOverrideTimestampFunction(() => _testTime)
773767
..semanticsEnabled = true;
@@ -791,7 +785,7 @@ void _testHeader() {
791785

792786
owner().updateSemantics(builder.build());
793787
expectSemanticsTree(owner(), '''
794-
<sem role="group" aria-label="Header of the page"><sem-c><sem></sem></sem-c></sem>
788+
<header aria-label="Header of the page"><sem-c><sem></sem></sem-c></header>
795789
''');
796790

797791
semantics().semanticsEnabled = false;

0 commit comments

Comments
 (0)