Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit d132ac5

Browse files
authored
[web] Fix exception when getting boxes for rich text range (#17933)
1 parent cade0e9 commit d132ac5

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/web_ui/lib/src/engine/text/paragraph.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,13 +359,23 @@ class EngineParagraph implements ui.Paragraph {
359359
}) {
360360
assert(boxHeightStyle != null);
361361
assert(boxWidthStyle != null);
362-
if (_plainText == null || start == end) {
362+
// Zero-length ranges and invalid ranges return an empty list.
363+
if (start == end || start < 0 || end < 0) {
363364
return <ui.TextBox>[];
364365
}
365366

367+
// For rich text, we can't measure the boxes. So for now, we'll just return
368+
// a placeholder box to stop exceptions from being thrown in the framework.
369+
// https://github.com/flutter/flutter/issues/55587
370+
if (_plainText == null) {
371+
return <ui.TextBox>[
372+
ui.TextBox.fromLTRBD(0, 0, 0, _lineHeight, _textDirection)
373+
];
374+
}
375+
366376
final int length = _plainText.length;
367377
// Ranges that are out of bounds should return an empty list.
368-
if (start < 0 || end < 0 || start > length || end > length) {
378+
if (start > length || end > length) {
369379
return <ui.TextBox>[];
370380
}
371381

lib/web_ui/test/paragraph_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,25 @@ void main() async {
415415
);
416416
});
417417

418+
testEachMeasurement('getBoxesForRange returns a box for rich text', () {
419+
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(
420+
fontFamily: 'Ahem',
421+
fontStyle: FontStyle.normal,
422+
fontWeight: FontWeight.normal,
423+
fontSize: 10,
424+
textDirection: TextDirection.ltr,
425+
));
426+
builder.addText('abcd');
427+
builder.pushStyle(TextStyle(fontWeight: FontWeight.bold));
428+
builder.addText('xyz');
429+
final Paragraph paragraph = builder.build();
430+
paragraph.layout(const ParagraphConstraints(width: 1000));
431+
expect(
432+
paragraph.getBoxesForRange(1, 2).single,
433+
const TextBox.fromLTRBD(0, 0, 0, 10, TextDirection.ltr),
434+
);
435+
});
436+
418437
testEachMeasurement(
419438
'getBoxesForRange return empty list for zero-length range', () {
420439
final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle(

0 commit comments

Comments
 (0)