Skip to content

Commit 235b1ce

Browse files
flutter-zlsuryaKommana2662
authored andcommitted
[web] Respect text affinity in getLineBoundary at a soft wrap (flutter#192664)
Fixes flutter#188874 **Problem** When text wraps onto a second line, one offset sits in two places at once: the end of line 1 and the start of line 2. `TextPosition.affinity` is what says which of the two you mean. Web ignored it. For `'Test Text'` wrapping after `'Test '`, asking about the start of line 2: ``` getLineBoundary(TextPosition(offset: 5)) native: TextRange(5, 9) line 2 correct web: TextRange(0, 5) line 1 wrong ``` Skia reports line bounds inclusively, so line 1's `end` and line 2's `start` are both 5 and the lookup always matched line 1 first. Line 2 was unreachable from any caret position, which broke line navigation keys and triple-tap line selection on web. Both shipping web renderers, CanvasKit and skwasm, had the bug independently. **Fix** Ported the lookahead native has used since flutter-team-archive/engine#28008. If the position is downstream and sits exactly where one line's `end` meets the next line's `start`, return the next line instead. Hard line breaks are unaffected, because a newline takes an offset of its own so the two ranges never touch and the rule cannot fire. A test pins that. **Demo** Before: https://flutter-demo-60-before.web.app After: https://flutter-demo-60-after.web.app Repro steps: 1. Click in the text box, then drag the caret so it sits just before the `A` on the second line. The readout confirms offset 15. 2. The panel underneath follows the caret and shows the range `getLineBoundary` returns for that position. 3. Before: the caret is drawn on line 2, but the boundary comes back as `0..15`, which is line 1, so the panel reads MISMATCH. 4. After: the boundary comes back as `15..16` and the panel reads AGREE. Every other offset behaves identically in both builds. Offset 15 with downstream affinity is the only input whose answer changes.
1 parent 97d7f64 commit 235b1ce

6 files changed

Lines changed: 201 additions & 8 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,26 @@ class CkParagraph implements ui.Paragraph {
974974
ui.TextRange getLineBoundary(ui.TextPosition position) {
975975
assert(!_disposed, 'Paragraph has been disposed.');
976976
final List<SkLineMetrics> metrics = skiaObject.getLineMetrics();
977-
final int offset = position.offset;
977+
final ui.TextRange line = _lineBoundaryAtOffset(metrics, position.offset);
978+
979+
// A line's endIndex equals the next line's startIndex at a soft wrap, so
980+
// the lookup above cannot tell the two apart on its own and always answers
981+
// with the earlier line, as if the affinity were upstream. A downstream
982+
// position sitting exactly on that seam belongs to the next line instead.
983+
// This mirrors the native implementation in `lib/ui/text.dart`.
984+
final ui.TextRange nextLine = _lineBoundaryAtOffset(metrics, position.offset + 1);
985+
if (nextLine.isValid &&
986+
position.affinity == ui.TextAffinity.downstream &&
987+
line != nextLine &&
988+
position.offset == line.end &&
989+
line.end == nextLine.start) {
990+
return nextLine;
991+
}
992+
return line;
993+
}
994+
995+
// The line containing [offset], treating both line bounds as inclusive.
996+
static ui.TextRange _lineBoundaryAtOffset(List<SkLineMetrics> metrics, int offset) {
978997
for (final metric in metrics) {
979998
if (offset >= metric.startIndex && offset <= metric.endIndex) {
980999
return ui.TextRange(start: metric.startIndex.toInt(), end: metric.endIndex.toInt());

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,30 @@ class SkwasmParagraph extends SkwasmObjectWrapper<RawParagraph> implements ui.Pa
247247

248248
@override
249249
ui.TextRange getLineBoundary(ui.TextPosition position) {
250-
final int offset = position.offset;
251-
for (final SkwasmLineMetrics metrics in computeLineMetrics()) {
252-
if (offset >= metrics.startIndex && offset <= metrics.endIndex) {
253-
return ui.TextRange(start: metrics.startIndex, end: metrics.endIndex);
250+
final List<SkwasmLineMetrics> metrics = computeLineMetrics();
251+
final ui.TextRange line = _lineBoundaryAtOffset(metrics, position.offset);
252+
253+
// A line's endIndex equals the next line's startIndex at a soft wrap, so
254+
// the lookup above cannot tell the two apart on its own and always answers
255+
// with the earlier line, as if the affinity were upstream. A downstream
256+
// position sitting exactly on that seam belongs to the next line instead.
257+
// This mirrors the native implementation in `lib/ui/text.dart`.
258+
final ui.TextRange nextLine = _lineBoundaryAtOffset(metrics, position.offset + 1);
259+
if (nextLine.isValid &&
260+
position.affinity == ui.TextAffinity.downstream &&
261+
line != nextLine &&
262+
position.offset == line.end &&
263+
line.end == nextLine.start) {
264+
return nextLine;
265+
}
266+
return line;
267+
}
268+
269+
// The line containing [offset], treating both line bounds as inclusive.
270+
static ui.TextRange _lineBoundaryAtOffset(List<SkwasmLineMetrics> metrics, int offset) {
271+
for (final line in metrics) {
272+
if (offset >= line.startIndex && offset <= line.endIndex) {
273+
return ui.TextRange(start: line.startIndex, end: line.endIndex);
254274
}
255275
}
256276
return ui.TextRange.empty;

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/layout.dart‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,26 @@ class TextLayout {
944944
return ui.TextRange(start: start, end: end);
945945
}
946946

947-
ui.TextRange getLineBoundary(int codepointPosition) {
947+
ui.TextRange getLineBoundary(ui.TextPosition position) {
948+
final ui.TextRange line = _lineBoundaryAtOffset(position.offset);
949+
950+
// A line's end equals the next line's start at a soft wrap, so the lookup
951+
// above cannot tell the two apart on its own and always answers with the
952+
// earlier line, as if the affinity were upstream. A downstream position
953+
// sitting exactly on that seam belongs to the next line instead. This
954+
// mirrors the native implementation in `lib/ui/text.dart`.
955+
final ui.TextRange nextLine = _lineBoundaryAtOffset(position.offset + 1);
956+
if (nextLine.isValid &&
957+
position.affinity == ui.TextAffinity.downstream &&
958+
line != nextLine &&
959+
position.offset == line.end &&
960+
line.end == nextLine.start) {
961+
return nextLine;
962+
}
963+
return line;
964+
}
965+
966+
ui.TextRange _lineBoundaryAtOffset(int codepointPosition) {
948967
for (final TextLine line in lines) {
949968
// This is the condition that SkParagraph is using
950969
if (line.allLineTextRange.start <= codepointPosition &&

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/paragraph.dart‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,7 @@ class WebParagraph implements ui.Paragraph {
10101010

10111011
@override
10121012
ui.TextRange getLineBoundary(ui.TextPosition position) {
1013-
final int codepointPosition = position.offset;
1014-
final ui.TextRange result = _layout.getLineBoundary(codepointPosition);
1013+
final ui.TextRange result = _layout.getLineBoundary(position);
10151014
WebParagraphDebug.apiTrace('getLineBoundary($position): $result');
10161015
return result;
10171016
}

‎engine/src/flutter/lib/web_ui/test/ui/line_metrics_test.dart‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,81 @@ Future<void> testMain() async {
140140
}
141141
});
142142

143+
test('getLineBoundary respects position affinity at a soft wrap', () {
144+
const fontSize = 10.0;
145+
final builder = ui.ParagraphBuilder(
146+
ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'),
147+
)..addText('Test Text');
148+
final ui.Paragraph paragraph = builder.build();
149+
// The test font is square, so five characters fit and the text wraps to
150+
// 'Test ' and 'Text'.
151+
paragraph.layout(const ui.ParagraphConstraints(width: fontSize * 5.0));
152+
expect(paragraph.computeLineMetrics(), hasLength(2));
153+
154+
// Offset 5 is the seam: it is both the end of the first line and the start
155+
// of the second, so only the affinity distinguishes the two. Downstream
156+
// means the start of the second line.
157+
expect(
158+
paragraph.getLineBoundary(const ui.TextPosition(offset: 5)),
159+
const ui.TextRange(start: 5, end: 9),
160+
);
161+
162+
// Upstream means the end of the first line.
163+
expect(
164+
paragraph.getLineBoundary(
165+
const ui.TextPosition(offset: 5, affinity: ui.TextAffinity.upstream),
166+
),
167+
const ui.TextRange(start: 0, end: 5),
168+
);
169+
170+
// Away from the seam the affinity makes no difference. Downstream at 0 and
171+
// upstream at 2 both resolve to the first line.
172+
expect(
173+
paragraph.getLineBoundary(const ui.TextPosition(offset: 0)),
174+
const ui.TextRange(start: 0, end: 5),
175+
);
176+
expect(
177+
paragraph.getLineBoundary(
178+
const ui.TextPosition(offset: 2, affinity: ui.TextAffinity.upstream),
179+
),
180+
const ui.TextRange(start: 0, end: 5),
181+
);
182+
183+
// The end of the last line has no following line to move to.
184+
expect(
185+
paragraph.getLineBoundary(const ui.TextPosition(offset: 9)),
186+
const ui.TextRange(start: 5, end: 9),
187+
);
188+
});
189+
190+
test('getLineBoundary is unaffected by affinity at a hard line break', () {
191+
const fontSize = 10.0;
192+
final builder = ui.ParagraphBuilder(
193+
ui.ParagraphStyle(fontSize: fontSize, fontFamily: 'FlutterTest'),
194+
)..addText('Test\nText');
195+
final ui.Paragraph paragraph = builder.build();
196+
paragraph.layout(const ui.ParagraphConstraints(width: double.infinity));
197+
expect(paragraph.computeLineMetrics(), hasLength(2));
198+
199+
// Unlike a soft wrap, the newline occupies an offset of its own, so the
200+
// first line's end and the second line's start are not the same position
201+
// and there is no seam for the affinity to disambiguate.
202+
expect(
203+
paragraph.getLineBoundary(const ui.TextPosition(offset: 4)),
204+
const ui.TextRange(start: 0, end: 4),
205+
);
206+
expect(
207+
paragraph.getLineBoundary(
208+
const ui.TextPosition(offset: 4, affinity: ui.TextAffinity.upstream),
209+
),
210+
const ui.TextRange(start: 0, end: 4),
211+
);
212+
expect(
213+
paragraph.getLineBoundary(const ui.TextPosition(offset: 5)),
214+
const ui.TextRange(start: 5, end: 9),
215+
);
216+
});
217+
143218
group('when flutter tester emulation is enabled', () {
144219
setUp(() {
145220
ui_web.TestEnvironment.setUp(const ui_web.TestEnvironment.flutterTester());

‎engine/src/flutter/lib/web_ui/test/webparagraph/paragraph_queries_test.dart‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,67 @@ Future<void> testMain() async {
196196
);
197197
});
198198

199+
test('Paragraph getLineBoundary respects affinity at a soft wrap', () {
200+
final paragraphStyle = ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 50);
201+
202+
final builder = ui.ParagraphBuilder(paragraphStyle);
203+
builder.addText('Test Text');
204+
final ui.Paragraph paragraph = builder.build();
205+
paragraph.layout(const ui.ParagraphConstraints(width: 150));
206+
// Wraps to 'Test ' and 'Text'.
207+
expect(paragraph.computeLineMetrics(), hasLength(2));
208+
expect(
209+
paragraph.getLineBoundary(const ui.TextPosition(offset: 0)),
210+
const ui.TextRange(start: 0, end: 5),
211+
);
212+
213+
// Offset 5 is both the end of the first line and the start of the second,
214+
// so only the affinity distinguishes the two.
215+
expect(
216+
paragraph.getLineBoundary(const ui.TextPosition(offset: 5)),
217+
const ui.TextRange(start: 5, end: 9),
218+
);
219+
expect(
220+
paragraph.getLineBoundary(
221+
const ui.TextPosition(offset: 5, affinity: ui.TextAffinity.upstream),
222+
),
223+
const ui.TextRange(start: 0, end: 5),
224+
);
225+
226+
// The end of the last line has no following line to move to.
227+
expect(
228+
paragraph.getLineBoundary(const ui.TextPosition(offset: 9)),
229+
const ui.TextRange(start: 5, end: 9),
230+
);
231+
});
232+
233+
test('Paragraph getLineBoundary is unaffected by affinity at a hard line break', () {
234+
final paragraphStyle = ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 50);
235+
236+
final builder = ui.ParagraphBuilder(paragraphStyle);
237+
builder.addText('Test\nText');
238+
final ui.Paragraph paragraph = builder.build();
239+
paragraph.layout(const ui.ParagraphConstraints(width: double.infinity));
240+
241+
// Unlike a soft wrap, the newline occupies an offset of its own, so the
242+
// first line's end and the second line's start are not the same position
243+
// and there is no seam for the affinity to disambiguate.
244+
expect(
245+
paragraph.getLineBoundary(const ui.TextPosition(offset: 4)),
246+
const ui.TextRange(start: 0, end: 4),
247+
);
248+
expect(
249+
paragraph.getLineBoundary(
250+
const ui.TextPosition(offset: 4, affinity: ui.TextAffinity.upstream),
251+
),
252+
const ui.TextRange(start: 0, end: 4),
253+
);
254+
expect(
255+
paragraph.getLineBoundary(const ui.TextPosition(offset: 5)),
256+
const ui.TextRange(start: 5, end: 9),
257+
);
258+
});
259+
199260
test('Paragraph computeLineMetrics/getLineMetricsAt', () {
200261
final paragraphStyle = ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20);
201262

0 commit comments

Comments
 (0)