-
Notifications
You must be signed in to change notification settings - Fork 6k
Add support for setting the heading level for web semantics (#97894) #41435
Changes from all commits
a341b26
eff0206
7f8ef2c
9029d63
260d4bd
baea4c1
bf0fe7f
b773f81
90de9fc
c9ac350
c8ccd31
0241f07
6298a5f
36c5fd6
30153e0
5460388
a526070
dceef3c
3a6e45d
b9bfb88
664c1e4
e6a8c6a
470f67f
a56c398
b31bd9f
8305e4b
d860f21
a16a8f7
70efe89
ddeaefc
bcd3205
9d25b32
268e31c
a7bb630
0f72806
79e4728
4b69d9f
c83039b
b351c1e
5705912
89ef536
e60c0a7
bb787e0
7199da1
76b65f6
ee38434
41f77aa
e38e3f3
1e570fb
ac1ec5e
0b9efa6
4a54812
6d8d511
2586dbc
fc0a3a5
ca2d289
5e05f24
9231840
c52b545
33a246c
1b0e241
e918e20
4383464
5186055
af5e70a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import '../dom.dart'; | ||
import 'semantics.dart'; | ||
|
||
/// Renders semantics objects as headings with the corresponding | ||
/// level (h1 ... h6). | ||
class Heading extends PrimaryRoleManager { | ||
Heading(SemanticsObject semanticsObject) | ||
: super.blank(PrimaryRole.heading, semanticsObject) { | ||
addHeadingRole(); | ||
} | ||
|
||
@override | ||
void update() { | ||
super.update(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can return early if the heading level field is not dirty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I added it. |
||
|
||
if (!semanticsObject.isHeadingLevelDirty) { | ||
return; | ||
} | ||
|
||
addHeadingLevel(semanticsObject.headingLevel); | ||
} | ||
|
||
@override | ||
bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; | ||
|
||
void addHeadingRole() { | ||
setAriaRole('heading'); | ||
} | ||
|
||
void addHeadingLevel(int headingLevel) { | ||
semanticsObject.element.setAttribute('aria-level', headingLevel); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -721,6 +721,27 @@ void _testHeader() { | |
owner().updateSemantics(builder.build()); | ||
expectSemanticsTree(owner(), ''' | ||
<sem role="group" aria-label="Header of the page"><sem-c><sem></sem></sem-c></sem> | ||
'''); | ||
|
||
semantics().semanticsEnabled = false; | ||
}); | ||
|
||
test('renders aria-level tag for headings with heading level', () { | ||
semantics() | ||
..debugOverrideTimestampFunction(() => _testTime) | ||
..semanticsEnabled = true; | ||
|
||
final ui.SemanticsUpdateBuilder builder = ui.SemanticsUpdateBuilder(); | ||
updateNode( | ||
builder, | ||
headingLevel: 2, | ||
transform: Matrix4.identity().toFloat64(), | ||
rect: const ui.Rect.fromLTRB(0, 0, 100, 50), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also test that we can update it and clear There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey @yjbanov, I'd say i's not a meaningful state, aria-level is required for "heading" role. Do you think I still should add it?. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, if it's not an expected state, we don't need to test it. |
||
|
||
owner().updateSemantics(builder.build()); | ||
expectSemanticsTree(owner(), ''' | ||
<sem aria-level="2" role="heading" style="filter: opacity(0%); color: rgba(0, 0, 0, 0)"></sem> | ||
'''); | ||
|
||
semantics().semanticsEnabled = false; | ||
|
@@ -3557,6 +3578,7 @@ void updateNode( | |
Int32List? childrenInTraversalOrder, | ||
Int32List? childrenInHitTestOrder, | ||
Int32List? additionalActions, | ||
int headingLevel = 0, | ||
}) { | ||
transform ??= Float64List.fromList(Matrix4.identity().storage); | ||
childrenInTraversalOrder ??= Int32List(0); | ||
|
@@ -3596,6 +3618,7 @@ void updateNode( | |
childrenInTraversalOrder: childrenInTraversalOrder, | ||
childrenInHitTestOrder: childrenInHitTestOrder, | ||
additionalActions: additionalActions, | ||
headingLevel: headingLevel, | ||
); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we also want to
assert(headingLevel >= -1)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right!, done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably can't be 0 as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done