Unverified Commit 40f3a794 authored by flutteractionsbot's avatar flutteractionsbot Committed by GitHub

[CP-beta]Fix `getOffsetForCaret` crash (#146813)

This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/wiki/Flutter-Cherrypick-Process#automatically-creates-a-cherry-pick-request)
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

### Issue Link:
What is the link to the issue this cherry-pick is addressing?

b/333560406. Couldn't find a github issue for it

### Changelog Description:
Explain this cherry pick in one line that is accessible to most Flutter developers. See [best practices](https://github.com/flutter/flutter/wiki/Hotfix-Documentation-Best-Practices) for examples

Fixes a crash that may happen when the user enters certain characters in a text field.

### Impact Description:
What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping production app (the app crashes on launch)

Production apps crash when the user enters certain characters in certain conditions.

b/333560406 has some related stats.

### Workaround:
Is there a workaround for this issue?

No

### Risk:
What is the risk level of this cherry-pick?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

### Validation Steps:
What are the steps to validate that this fix works?

Run the test in the PR. The issue didn't come with a real world repro.
parent bc38ac07
......@@ -1463,10 +1463,15 @@ class TextPainter {
final _LineCaretMetrics metrics;
final List<TextBox> boxes = cachedLayout.paragraph
.getBoxesForRange(graphemeRange.start, graphemeRange.end, boxHeightStyle: ui.BoxHeightStyle.strut);
if (boxes.isNotEmpty) {
final TextBox box = boxes.single;
final bool ahchorToLeft = switch (glyphInfo.writingDirection) {
TextDirection.ltr => anchorToLeadingEdge,
TextDirection.rtl => !anchorToLeadingEdge,
};
final TextBox box = ahchorToLeft ? boxes.first : boxes.last;
metrics = _LineCaretMetrics(
offset: Offset(anchorToLeadingEdge ? box.start : box.end, box.top),
offset: Offset(ahchorToLeft ? box.left : box.right, box.top),
writingDirection: box.direction,
);
} else {
......
......@@ -1682,6 +1682,20 @@ void main() {
});
}, skip: kIsWeb && !isCanvasKit); // [intended] strut spport for HTML renderer https://github.com/flutter/flutter/issues/32243.
test('getOffsetForCaret does not crash on decomposed characters', () {
final TextPainter painter = TextPainter(
textDirection: TextDirection.ltr,
text: const TextSpan(
text: '각',
style: TextStyle(fontSize: 10),
),
)..layout(maxWidth: 1); // Force the jamo characters to soft wrap.
expect(
() => painter.getOffsetForCaret(const TextPosition(offset: 0), Rect.zero),
returnsNormally,
);
});
test('TextPainter dispatches memory events', () async {
await expectLater(
await memoryEvents(() => TextPainter().dispose(), TextPainter),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment