@@ -4,7 +4,7 @@ import 'package:super_editor/super_editor.dart';
4
4
5
5
void main () {
6
6
group ('SuperEditor > computeInlineSpan >' , () {
7
- testWidgets ('does not modify text with attributions and a placeholder at the beginning' , (tester) async {
7
+ testWidgets ('computes inlineSpan for text with attributions and a placeholder at the beginning' , (tester) async {
8
8
// Pump a widget because we need a BuildContext to compute the InlineSpan.
9
9
await tester.pumpWidget (
10
10
const MaterialApp (),
@@ -15,10 +15,10 @@ void main() {
15
15
'Welcome to SuperEditor' ,
16
16
AttributedSpans (
17
17
attributions: [
18
- const SpanMarker (attribution: boldAttribution, offset: 0 , markerType: SpanMarkerType .start),
19
- const SpanMarker (attribution: boldAttribution, offset: 6 , markerType: SpanMarkerType .end),
20
- const SpanMarker (attribution: boldAttribution, offset: 11 , markerType: SpanMarkerType .start),
21
- const SpanMarker (attribution: boldAttribution, offset: 21 , markerType: SpanMarkerType .end),
18
+ const SpanMarker (attribution: boldAttribution, offset: 1 , markerType: SpanMarkerType .start),
19
+ const SpanMarker (attribution: boldAttribution, offset: 7 , markerType: SpanMarkerType .end),
20
+ const SpanMarker (attribution: boldAttribution, offset: 12 , markerType: SpanMarkerType .start),
21
+ const SpanMarker (attribution: boldAttribution, offset: 22 , markerType: SpanMarkerType .end),
22
22
],
23
23
),
24
24
{0 : const _ExamplePlaceholder ()},
@@ -30,14 +30,34 @@ void main() {
30
30
[_inlineWidgetBuilder],
31
31
);
32
32
33
- // Ensure the text was not modified.
34
- expect (
35
- inlineSpan.toPlainText (includePlaceholders: false ),
36
- 'Welcome to SuperEditor' ,
37
- );
33
+ final spanList = _flattenInlineSpan (inlineSpan);
34
+ expect (spanList.length, equals (5 ));
35
+
36
+ // Ensure that the first span is an empty TextSpan with the default fontWeight.
37
+ expect (spanList[0 ], isA <TextSpan >());
38
+ expect ((spanList[0 ] as TextSpan ).text, equals ('' ));
39
+ expect ((spanList[0 ] as TextSpan ).style! .fontWeight, isNull);
40
+
41
+ // Expect that the second span is the widget rendered using the placeholder.
42
+ expect (spanList[1 ], isA <WidgetSpan >());
43
+
44
+ // Ensure that the third span is a TextSpan with the text "Welcome" in bold.
45
+ expect (spanList[2 ], isA <TextSpan >());
46
+ expect ((spanList[2 ] as TextSpan ).text, equals ('Welcome' ));
47
+ expect ((spanList[2 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
48
+
49
+ // Ensure that the fourth span is a TextSpan with the text " to " with the default fontWeight.
50
+ expect (spanList[3 ], isA <TextSpan >());
51
+ expect ((spanList[3 ] as TextSpan ).text, equals (' to ' ));
52
+ expect ((spanList[3 ] as TextSpan ).style! .fontWeight, isNull);
53
+
54
+ // Ensure that the fifth span is a TextSpan with the text "SuperEditor" in bold.
55
+ expect (spanList[4 ], isA <TextSpan >());
56
+ expect ((spanList[4 ] as TextSpan ).text, equals ('SuperEditor' ));
57
+ expect ((spanList[4 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
38
58
});
39
59
40
- testWidgets ('does not modify text with attributions and a placeholder at the middle' , (tester) async {
60
+ testWidgets ('computes inlineSpan for text with attributions and a placeholder at the middle' , (tester) async {
41
61
// Pump a widget because we need a BuildContext to compute the InlineSpan.
42
62
await tester.pumpWidget (
43
63
const MaterialApp (),
@@ -51,8 +71,8 @@ void main() {
51
71
attributions: [
52
72
const SpanMarker (attribution: boldAttribution, offset: 0 , markerType: SpanMarkerType .start),
53
73
const SpanMarker (attribution: boldAttribution, offset: 6 , markerType: SpanMarkerType .end),
54
- const SpanMarker (attribution: boldAttribution, offset: 11 , markerType: SpanMarkerType .start),
55
- const SpanMarker (attribution: boldAttribution, offset: 21 , markerType: SpanMarkerType .end),
74
+ const SpanMarker (attribution: boldAttribution, offset: 12 , markerType: SpanMarkerType .start),
75
+ const SpanMarker (attribution: boldAttribution, offset: 22 , markerType: SpanMarkerType .end),
56
76
],
57
77
),
58
78
{10 : const _ExamplePlaceholder ()},
@@ -64,14 +84,39 @@ void main() {
64
84
[_inlineWidgetBuilder],
65
85
);
66
86
67
- // Ensure the text was not modified.
68
- expect (
69
- inlineSpan.toPlainText (includePlaceholders: false ),
70
- 'Welcome to SuperEditor' ,
71
- );
87
+ final spanList = _flattenInlineSpan (inlineSpan);
88
+ expect (spanList.length, equals (6 ));
89
+
90
+ // Ensure that the first span is an empty TextSpan with the default fontWeight.
91
+ expect (spanList[0 ], isA <TextSpan >());
92
+ expect ((spanList[0 ] as TextSpan ).text, equals ('' ));
93
+ expect ((spanList[0 ] as TextSpan ).style! .fontWeight, isNull);
94
+
95
+ // Expect that the second span is a TextSpan with the text "Welcome" in bold.
96
+ expect (spanList[1 ], isA <TextSpan >());
97
+ expect ((spanList[1 ] as TextSpan ).text, equals ('Welcome' ));
98
+ expect ((spanList[1 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
99
+
100
+ // Ensure that the third span is a TextSpan with the text " to" with the default fontWeight.
101
+ expect (spanList[2 ], isA <TextSpan >());
102
+ expect ((spanList[2 ] as TextSpan ).text, equals (' to' ));
103
+ expect ((spanList[2 ] as TextSpan ).style! .fontWeight, isNull);
104
+
105
+ // Expect that the fourth span is the widget rendered using the placeholder.
106
+ expect (spanList[3 ], isA <WidgetSpan >());
107
+
108
+ // Ensure that the fifth span is a TextSpan with the text " " with the default fontWeight.
109
+ expect (spanList[4 ], isA <TextSpan >());
110
+ expect ((spanList[4 ] as TextSpan ).text, equals (' ' ));
111
+ expect ((spanList[4 ] as TextSpan ).style! .fontWeight, isNull);
112
+
113
+ // Ensure that the sixth span is a TextSpan with the text "SuperEditor" in bold.
114
+ expect (spanList[5 ], isA <TextSpan >());
115
+ expect ((spanList[5 ] as TextSpan ).text, equals ('SuperEditor' ));
116
+ expect ((spanList[5 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
72
117
});
73
118
74
- testWidgets ('does not modify text with attributions and a placeholder at the end' , (tester) async {
119
+ testWidgets ('computes inlineSpan for text with attributions and a placeholder at the end' , (tester) async {
75
120
// Pump a widget because we need a BuildContext to compute the InlineSpan.
76
121
await tester.pumpWidget (
77
122
const MaterialApp (),
@@ -97,15 +142,46 @@ void main() {
97
142
[_inlineWidgetBuilder],
98
143
);
99
144
100
- // Ensure the text was not modified.
101
- expect (
102
- inlineSpan.toPlainText (includePlaceholders: false ),
103
- 'Welcome to SuperEditor' ,
104
- );
145
+ final spanList = _flattenInlineSpan (inlineSpan);
146
+ expect (spanList.length, equals (5 ));
147
+
148
+ // Ensure that the first span is an empty TextSpan with the default fontWeight.
149
+ expect (spanList[0 ], isA <TextSpan >());
150
+ expect ((spanList[0 ] as TextSpan ).text, equals ('' ));
151
+ expect ((spanList[0 ] as TextSpan ).style! .fontWeight, isNull);
152
+
153
+ // Ensure that the second span is a TextSpan with the text "Welcome" in bold.
154
+ expect (spanList[1 ], isA <TextSpan >());
155
+ expect ((spanList[1 ] as TextSpan ).text, equals ('Welcome' ));
156
+ expect ((spanList[1 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
157
+
158
+ // Ensure that the third span is a TextSpan with the text " to " with the default fontWeight.
159
+ expect (spanList[2 ], isA <TextSpan >());
160
+ expect ((spanList[2 ] as TextSpan ).text, equals (' to ' ));
161
+ expect ((spanList[2 ] as TextSpan ).style! .fontWeight, isNull);
162
+
163
+ // Ensure that the fourth span is a TextSpan with the text "SuperEditor" in bold.
164
+ expect (spanList[3 ], isA <TextSpan >());
165
+ expect ((spanList[3 ] as TextSpan ).text, equals ('SuperEditor' ));
166
+ expect ((spanList[3 ] as TextSpan ).style! .fontWeight, equals (FontWeight .bold));
167
+
168
+ // Expect that the fifth span is the widget rendered using the placeholder.
169
+ expect (spanList[4 ], isA <WidgetSpan >());
105
170
});
106
171
});
107
172
}
108
173
174
+ List <InlineSpan > _flattenInlineSpan (InlineSpan inlineSpan) {
175
+ final flatList = < InlineSpan > [];
176
+
177
+ inlineSpan.visitChildren ((child) {
178
+ flatList.add (child);
179
+ return true ;
180
+ });
181
+
182
+ return flatList;
183
+ }
184
+
109
185
class _ExamplePlaceholder {
110
186
const _ExamplePlaceholder ();
111
187
}
0 commit comments