From 5fb88851d51f5d4732539adb6592783e775c516a Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Wed, 3 Jul 2024 17:12:26 -0400 Subject: [PATCH] Add Semantics Property `linkUrl` (flutter/engine#53507) The new property allows the user to specify a URI for their semantics link node. It's plumbed through for both web and non-web engines, but it's only used in the web engine currently. It sets the `href` of the anchor element associated with semantics node. This is going to unlock better semantics support in the Link widget on web ([PR](https://github.com/flutter/packages/pull/6711)). Framework counterpart: https://github.com/flutter/flutter/pull/150639 Part of https://github.com/flutter/flutter/issues/150263 --- .../src/flutter/lib/ui/fixtures/ui_test.dart | 1 + engine/src/flutter/lib/ui/semantics.dart | 12 +++++++-- .../flutter/lib/ui/semantics/semantics_node.h | 2 ++ .../ui/semantics/semantics_update_builder.cc | 4 ++- .../ui/semantics/semantics_update_builder.h | 3 ++- .../src/flutter/lib/web_ui/lib/semantics.dart | 2 ++ .../web_ui/lib/src/engine/semantics/link.dart | 15 +++++++++-- .../lib/src/engine/semantics/semantics.dart | 25 +++++++++++++++++++ .../test/engine/semantics/semantics_test.dart | 24 ++++++++++++++++++ .../engine/semantics/semantics_tester.dart | 2 ++ .../platform/embedder/fixtures/main.dart | 13 +++++++--- 11 files changed, 93 insertions(+), 10 deletions(-) diff --git a/engine/src/flutter/lib/ui/fixtures/ui_test.dart b/engine/src/flutter/lib/ui/fixtures/ui_test.dart index 492a234b85..a02e0ce294 100644 --- a/engine/src/flutter/lib/ui/fixtures/ui_test.dart +++ b/engine/src/flutter/lib/ui/fixtures/ui_test.dart @@ -233,6 +233,7 @@ void sendSemanticsUpdate() { childrenInHitTestOrder: childrenInHitTestOrder, additionalActions: additionalActions, headingLevel: 0, + linkUrl: '', ); _semanticsUpdate(builder.build()); } diff --git a/engine/src/flutter/lib/ui/semantics.dart b/engine/src/flutter/lib/ui/semantics.dart index 57bc1fa307..4500c08629 100644 --- a/engine/src/flutter/lib/ui/semantics.dart +++ b/engine/src/flutter/lib/ui/semantics.dart @@ -849,6 +849,9 @@ abstract class SemanticsUpdateBuilder { /// inclusive. This attribute is only used for Web platform, and it will have /// no effect on other platforms. /// + /// The `linkUrl` describes the URI that this node links to. If the node is + /// not a link, this should be an empty string. + /// /// See also: /// /// * https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/heading_role @@ -888,6 +891,7 @@ abstract class SemanticsUpdateBuilder { required Int32List childrenInHitTestOrder, required Int32List additionalActions, int headingLevel = 0, + String linkUrl = '', }); /// Update the custom semantics action associated with the given `id`. @@ -959,6 +963,7 @@ base class _NativeSemanticsUpdateBuilder extends NativeFieldWrapperClass1 implem required Int32List childrenInHitTestOrder, required Int32List additionalActions, int headingLevel = 0, + String linkUrl = '', }) { assert(_matrix4IsValid(transform)); assert ( @@ -1003,6 +1008,7 @@ base class _NativeSemanticsUpdateBuilder extends NativeFieldWrapperClass1 implem childrenInHitTestOrder, additionalActions, headingLevel, + linkUrl, ); } @Native< @@ -1044,7 +1050,8 @@ base class _NativeSemanticsUpdateBuilder extends NativeFieldWrapperClass1 implem Handle, Handle, Handle, - Int32)>(symbol: 'SemanticsUpdateBuilder::updateNode') + Int32, + Handle)>(symbol: 'SemanticsUpdateBuilder::updateNode') external void _updateNode( int id, int flags, @@ -1082,7 +1089,8 @@ base class _NativeSemanticsUpdateBuilder extends NativeFieldWrapperClass1 implem Int32List childrenInTraversalOrder, Int32List childrenInHitTestOrder, Int32List additionalActions, - int headingLevel); + int headingLevel, + String linkUrl); @override void updateCustomAction({required int id, String? label, String? hint, int overrideId = -1}) { diff --git a/engine/src/flutter/lib/ui/semantics/semantics_node.h b/engine/src/flutter/lib/ui/semantics/semantics_node.h index c27ac35f00..c83990d7ed 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_node.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_node.h @@ -144,6 +144,8 @@ struct SemanticsNode { std::vector childrenInHitTestOrder; std::vector customAccessibilityActions; int32_t headingLevel = 0; + + std::string linkUrl; }; // Contains semantic nodes that need to be updated. diff --git a/engine/src/flutter/lib/ui/semantics/semantics_update_builder.cc b/engine/src/flutter/lib/ui/semantics/semantics_update_builder.cc index 96eb0ce2e1..b853d3ae52 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_update_builder.cc +++ b/engine/src/flutter/lib/ui/semantics/semantics_update_builder.cc @@ -67,7 +67,8 @@ void SemanticsUpdateBuilder::updateNode( const tonic::Int32List& childrenInTraversalOrder, const tonic::Int32List& childrenInHitTestOrder, const tonic::Int32List& localContextActions, - int headingLevel) { + int headingLevel, + std::string linkUrl) { FML_CHECK(scrollChildren == 0 || (scrollChildren > 0 && childrenInHitTestOrder.data())) << "Semantics update contained scrollChildren but did not have " @@ -121,6 +122,7 @@ void SemanticsUpdateBuilder::updateNode( nodes_[id] = node; node.headingLevel = headingLevel; + node.linkUrl = std::move(linkUrl); } void SemanticsUpdateBuilder::updateCustomAction(int id, diff --git a/engine/src/flutter/lib/ui/semantics/semantics_update_builder.h b/engine/src/flutter/lib/ui/semantics/semantics_update_builder.h index cab294b904..e944c25109 100644 --- a/engine/src/flutter/lib/ui/semantics/semantics_update_builder.h +++ b/engine/src/flutter/lib/ui/semantics/semantics_update_builder.h @@ -66,7 +66,8 @@ class SemanticsUpdateBuilder const tonic::Int32List& childrenInTraversalOrder, const tonic::Int32List& childrenInHitTestOrder, const tonic::Int32List& customAccessibilityActions, - int headingLevel); + int headingLevel, + std::string linkUrl); void updateCustomAction(int id, std::string label, diff --git a/engine/src/flutter/lib/web_ui/lib/semantics.dart b/engine/src/flutter/lib/web_ui/lib/semantics.dart index e4698e40f2..34242b9830 100644 --- a/engine/src/flutter/lib/web_ui/lib/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/semantics.dart @@ -287,6 +287,7 @@ class SemanticsUpdateBuilder { required Int32List childrenInHitTestOrder, required Int32List additionalActions, int headingLevel = 0, + String? linkUrl, }) { if (transform.length != 16) { throw ArgumentError('transform argument must have 16 entries.'); @@ -326,6 +327,7 @@ class SemanticsUpdateBuilder { additionalActions: additionalActions, platformViewId: platformViewId, headingLevel: headingLevel, + linkUrl: linkUrl, )); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/link.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/link.dart index 3108e8cea8..d957e90d29 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/link.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/link.dart @@ -18,12 +18,23 @@ class Link extends PrimaryRoleManager { @override DomElement createElement() { final DomElement element = domDocument.createElement('a'); - // TODO(mdebbar): Fill in the real link once the framework sends entire uri. - // https://github.com/flutter/flutter/issues/150263. element.style.display = 'block'; return element; } + @override + void update() { + super.update(); + + if (semanticsObject.isLinkUrlDirty) { + if (semanticsObject.hasLinkUrl) { + element.setAttribute('href', semanticsObject.linkUrl!); + } else { + element.removeAttribute('href'); + } + } + } + @override bool focusAsRouteDefault() => focusable?.focusAsRouteDefault() ?? false; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart index c48851d983..28d5de60a9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/semantics/semantics.dart @@ -234,6 +234,7 @@ class SemanticsNodeUpdate { required this.childrenInHitTestOrder, required this.additionalActions, required this.headingLevel, + this.linkUrl, }); /// See [ui.SemanticsUpdateBuilder.updateNode]. @@ -337,6 +338,9 @@ class SemanticsNodeUpdate { /// See [ui.SemanticsUpdateBuilder.updateNode]. final int headingLevel; + + /// See [ui.SemanticsUpdateBuilder.updateNode]. + final String? linkUrl; } /// Identifies [PrimaryRoleManager] implementations. @@ -1146,6 +1150,22 @@ class SemanticsObject { _dirtyFields |= _identifierIndex; } + /// See [ui.SemanticsUpdateBuilder.updateNode]. + String? get linkUrl => _linkUrl; + String? _linkUrl; + + /// Whether this object contains a non-empty link URL. + bool get hasLinkUrl => _linkUrl != null && _linkUrl!.isNotEmpty; + + static const int _linkUrlIndex = 1 << 26; + + /// Whether the [linkUrl] field has been updated but has not been + /// applied to the DOM yet. + bool get isLinkUrlDirty => _isDirty(_linkUrlIndex); + void _markLinkUrlDirty() { + _dirtyFields |= _linkUrlIndex; + } + /// A unique permanent identifier of the semantics node in the tree. final int id; @@ -1445,6 +1465,11 @@ class SemanticsObject { _markPlatformViewIdDirty(); } + if (_linkUrl != update.linkUrl) { + _linkUrl = update.linkUrl; + _markLinkUrlDirty(); + } + // Apply updates to the DOM. _updateRoles(); diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart index 64fa0a54fe..5dfa87ad1d 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_test.dart @@ -3603,6 +3603,28 @@ void _testLink() { expect(object.element.tagName.toLowerCase(), 'a'); expect(object.element.hasAttribute('href'), isFalse); }); + + test('link nodes with linkUrl set the href attribute', () { + semantics() + ..debugOverrideTimestampFunction(() => _testTime) + ..semanticsEnabled = true; + + SemanticsObject pumpSemantics() { + final SemanticsTester tester = SemanticsTester(owner()); + tester.updateNode( + id: 0, + isLink: true, + linkUrl: 'https://flutter.dev', + rect: const ui.Rect.fromLTRB(0, 0, 100, 50), + ); + tester.apply(); + return tester.getSemanticsObject(0); + } + + final SemanticsObject object = pumpSemantics(); + expect(object.element.tagName.toLowerCase(), 'a'); + expect(object.element.getAttribute('href'), 'https://flutter.dev'); + }); } /// A facade in front of [ui.SemanticsUpdateBuilder.updateNode] that @@ -3645,6 +3667,7 @@ void updateNode( Int32List? childrenInHitTestOrder, Int32List? additionalActions, int headingLevel = 0, + String? linkUrl, }) { transform ??= Float64List.fromList(Matrix4.identity().storage); childrenInTraversalOrder ??= Int32List(0); @@ -3685,6 +3708,7 @@ void updateNode( childrenInHitTestOrder: childrenInHitTestOrder, additionalActions: additionalActions, headingLevel: headingLevel, + linkUrl: linkUrl, ); } diff --git a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_tester.dart b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_tester.dart index f9a626c486..a2d432b430 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_tester.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/semantics/semantics_tester.dart @@ -112,6 +112,7 @@ class SemanticsTester { Int32List? additionalActions, List? children, int? headingLevel, + String? linkUrl, }) { // Flags if (hasCheckedState ?? false) { @@ -313,6 +314,7 @@ class SemanticsTester { childrenInHitTestOrder: childIds, additionalActions: additionalActions ?? Int32List(0), headingLevel: headingLevel ?? 0, + linkUrl: linkUrl, ); _nodeUpdates.add(update); return update; diff --git a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart index 0336036f00..96cee3274a 100644 --- a/engine/src/flutter/shell/platform/embedder/fixtures/main.dart +++ b/engine/src/flutter/shell/platform/embedder/fixtures/main.dart @@ -178,7 +178,8 @@ Future a11y_main() async { tooltip: 'tooltip', textDirection: TextDirection.ltr, additionalActions: Int32List(0), - headingLevel: 0 + headingLevel: 0, + linkUrl: '', ) ..updateNode( id: 84, @@ -214,7 +215,8 @@ Future a11y_main() async { additionalActions: Int32List(0), childrenInHitTestOrder: Int32List(0), childrenInTraversalOrder: Int32List(0), - headingLevel: 0 + headingLevel: 0, + linkUrl: '', ) ..updateNode( id: 96, @@ -250,7 +252,8 @@ Future a11y_main() async { tooltip: 'tooltip', textDirection: TextDirection.ltr, additionalActions: Int32List(0), - headingLevel: 0 + headingLevel: 0, + linkUrl: '', ) ..updateNode( id: 128, @@ -286,7 +289,8 @@ Future a11y_main() async { textDirection: TextDirection.ltr, childrenInHitTestOrder: Int32List(0), childrenInTraversalOrder: Int32List(0), - headingLevel: 0 + headingLevel: 0, + linkUrl: '', ) ..updateCustomAction( id: 21, @@ -384,6 +388,7 @@ Future a11y_string_attributes() async { textDirection: TextDirection.ltr, additionalActions: Int32List(0), headingLevel: 0, + linkUrl: '', ); PlatformDispatcher.instance.views.first.updateSemantics(builder.build());