Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
1444e772
Unverified
Commit
1444e772
authored
Mar 18, 2020
by
Michael Goderbauer
Committed by
GitHub
Mar 18, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use stable IDs for TextSpan SemanticsNodes (#52769)
parent
542feb47
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
5 deletions
+156
-5
paragraph.dart
packages/flutter/lib/src/rendering/paragraph.dart
+23
-5
text_semantics_test.dart
packages/flutter/test/widgets/text_semantics_test.dart
+133
-0
No files found.
packages/flutter/lib/src/rendering/paragraph.dart
View file @
1444e772
...
...
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'dart:collection'
;
import
'dart:math'
as
math
;
import
'dart:ui'
as
ui
show
Gradient
,
Shader
,
TextBox
,
PlaceholderAlignment
,
TextHeightBehavior
;
...
...
@@ -844,6 +845,12 @@ class RenderParagraph extends RenderBox
}
}
// Caches [SemanticsNode]s created during [assembleSemanticsNode] so they
// can be re-used when [assembleSemanticsNode] is called again. This ensures
// stable ids for the [SemanticsNode]s of [TextSpan]s across
// [assembleSemanticsNode] invocations.
Queue
<
SemanticsNode
>
_cachedChildNodes
;
@override
void
assembleSemanticsNode
(
SemanticsNode
node
,
SemanticsConfiguration
config
,
Iterable
<
SemanticsNode
>
children
)
{
assert
(
_semanticsInfo
!=
null
&&
_semanticsInfo
.
isNotEmpty
);
...
...
@@ -854,6 +861,7 @@ class RenderParagraph extends RenderBox
int
start
=
0
;
int
placeholderIndex
=
0
;
RenderBox
child
=
firstChild
;
final
Queue
<
SemanticsNode
>
newChildCache
=
Queue
<
SemanticsNode
>();
for
(
final
InlineSpanSemanticsInformation
info
in
_combineSemanticsInfo
())
{
final
TextDirection
initialDirection
=
currentDirection
;
final
TextSelection
selection
=
TextSelection
(
...
...
@@ -914,17 +922,27 @@ class RenderParagraph extends RenderBox
assert
(
false
);
}
}
newChildren
.
add
(
SemanticsNode
()
final
SemanticsNode
newChild
=
(
_cachedChildNodes
?.
isNotEmpty
==
true
)
?
_cachedChildNodes
.
removeFirst
()
:
SemanticsNode
();
newChild
..
updateWith
(
config:
configuration
)
..
rect
=
currentRect
,
);
..
rect
=
currentRect
;
newChildCache
.
addLast
(
newChild
);
newChildren
.
add
(
newChild
);
}
start
+=
info
.
text
.
length
;
}
_cachedChildNodes
=
newChildCache
;
node
.
updateWith
(
config:
config
,
childrenInInversePaintOrder:
newChildren
);
}
@override
void
clearSemantics
()
{
super
.
clearSemantics
();
_cachedChildNodes
=
null
;
}
@override
List
<
DiagnosticsNode
>
debugDescribeChildren
()
{
return
<
DiagnosticsNode
>[
...
...
packages/flutter/test/widgets/text_semantics_test.dart
0 → 100644
View file @
1444e772
// Copyright 2014 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
'package:flutter/rendering.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/gestures.dart'
;
import
'package:flutter/material.dart'
;
import
'semantics_tester.dart'
;
void
main
(
)
{
testWidgets
(
'SemanticsNode ids are stable'
,
(
WidgetTester
tester
)
async
{
// Regression test for b/151732341.
final
SemanticsTester
semantics
=
SemanticsTester
(
tester
);
await
tester
.
pumpWidget
(
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Text
.
rich
(
TextSpan
(
text:
'Hallo '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
children:
<
TextSpan
>[
TextSpan
(
text:
'Welt '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
),
TextSpan
(
text:
'!!!'
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
),
],
),
),
));
expect
(
find
.
text
(
'Hallo Welt !!!'
),
findsOneWidget
);
final
SemanticsNode
node
=
tester
.
getSemantics
(
find
.
text
(
'Hallo Welt !!!'
));
final
Map
<
String
,
int
>
labelToNodeId
=
<
String
,
int
>{};
node
.
visitChildren
((
SemanticsNode
node
)
{
labelToNodeId
[
node
.
label
]
=
node
.
id
;
return
true
;
});
expect
(
node
.
id
,
1
);
expect
(
labelToNodeId
[
'Hallo '
],
2
);
expect
(
labelToNodeId
[
'Welt '
],
3
);
expect
(
labelToNodeId
[
'!!!'
],
4
);
expect
(
labelToNodeId
.
length
,
3
);
// Rebuild semantics.
tester
.
renderObject
(
find
.
text
(
'Hallo Welt !!!'
)).
markNeedsSemanticsUpdate
();
await
tester
.
pump
();
final
SemanticsNode
nodeAfterRebuild
=
tester
.
getSemantics
(
find
.
text
(
'Hallo Welt !!!'
));
final
Map
<
String
,
int
>
labelToNodeIdAfterRebuild
=
<
String
,
int
>{};
nodeAfterRebuild
.
visitChildren
((
SemanticsNode
node
)
{
labelToNodeIdAfterRebuild
[
node
.
label
]
=
node
.
id
;
return
true
;
});
// Node IDs are stable.
expect
(
nodeAfterRebuild
.
id
,
node
.
id
);
expect
(
labelToNodeIdAfterRebuild
[
'Hallo '
],
labelToNodeId
[
'Hallo '
]);
expect
(
labelToNodeIdAfterRebuild
[
'Welt '
],
labelToNodeId
[
'Welt '
]);
expect
(
labelToNodeIdAfterRebuild
[
'!!!'
],
labelToNodeId
[
'!!!'
]);
expect
(
labelToNodeIdAfterRebuild
.
length
,
3
);
// Remove one node.
await
tester
.
pumpWidget
(
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Text
.
rich
(
TextSpan
(
text:
'Hallo '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
children:
<
TextSpan
>[
TextSpan
(
text:
'Welt '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
),
],
),
),
));
final
SemanticsNode
nodeAfterRemoval
=
tester
.
getSemantics
(
find
.
text
(
'Hallo Welt '
));
final
Map
<
String
,
int
>
labelToNodeIdAfterRemoval
=
<
String
,
int
>{};
nodeAfterRemoval
.
visitChildren
((
SemanticsNode
node
)
{
labelToNodeIdAfterRemoval
[
node
.
label
]
=
node
.
id
;
return
true
;
});
// Node IDs are stable.
expect
(
nodeAfterRemoval
.
id
,
node
.
id
);
expect
(
labelToNodeIdAfterRemoval
[
'Hallo '
],
labelToNodeId
[
'Hallo '
]);
expect
(
labelToNodeIdAfterRemoval
[
'Welt '
],
labelToNodeId
[
'Welt '
]);
expect
(
labelToNodeIdAfterRemoval
.
length
,
2
);
await
tester
.
pumpWidget
(
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Text
.
rich
(
TextSpan
(
text:
'Hallo '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
children:
<
TextSpan
>[
TextSpan
(
text:
'Welt '
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
),
TextSpan
(
text:
'!!!'
,
recognizer:
TapGestureRecognizer
()..
onTap
=
()
{},
),
],
),
),
));
expect
(
find
.
text
(
'Hallo Welt !!!'
),
findsOneWidget
);
final
SemanticsNode
nodeAfterAddition
=
tester
.
getSemantics
(
find
.
text
(
'Hallo Welt !!!'
));
final
Map
<
String
,
int
>
labelToNodeIdAfterAddition
=
<
String
,
int
>{};
nodeAfterAddition
.
visitChildren
((
SemanticsNode
node
)
{
labelToNodeIdAfterAddition
[
node
.
label
]
=
node
.
id
;
return
true
;
});
// New node gets a new ID.
expect
(
nodeAfterAddition
.
id
,
node
.
id
);
expect
(
labelToNodeIdAfterAddition
[
'Hallo '
],
labelToNodeId
[
'Hallo '
]);
expect
(
labelToNodeIdAfterAddition
[
'Welt '
],
labelToNodeId
[
'Welt '
]);
expect
(
labelToNodeIdAfterAddition
[
'!!!'
],
isNot
(
labelToNodeId
[
'!!!'
]));
expect
(
labelToNodeIdAfterAddition
[
'!!!'
],
isNotNull
);
expect
(
labelToNodeIdAfterAddition
.
length
,
3
);
semantics
.
dispose
();
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment