Substitute a replacement character for invalid UTF-16 text in a TextSpan (#84887)
This commit is contained in:
@@ -244,8 +244,20 @@ class TextSpan extends InlineSpan implements HitTestTarget, MouseTrackerAnnotati
|
||||
final bool hasStyle = style != null;
|
||||
if (hasStyle)
|
||||
builder.pushStyle(style!.getTextStyle(textScaleFactor: textScaleFactor));
|
||||
if (text != null)
|
||||
builder.addText(text!);
|
||||
if (text != null) {
|
||||
try {
|
||||
builder.addText(text!);
|
||||
} on ArgumentError catch (exception, stack) {
|
||||
FlutterError.reportError(FlutterErrorDetails(
|
||||
exception: exception,
|
||||
stack: stack,
|
||||
library: 'painting library',
|
||||
context: ErrorDescription('while building a TextSpan'),
|
||||
));
|
||||
// Use a Unicode replacement character as a substitute for invalid text.
|
||||
builder.addText('\uFFFD');
|
||||
}
|
||||
}
|
||||
if (children != null) {
|
||||
for (final InlineSpan child in children!) {
|
||||
assert(child != null);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
@@ -952,6 +953,24 @@ void main() {
|
||||
expect(glyphBox, newGlyphBox);
|
||||
});
|
||||
}, skip: isBrowser);
|
||||
|
||||
test('TextPainter handles invalid UTF-16', () {
|
||||
Object? exception;
|
||||
FlutterError.onError = (FlutterErrorDetails details) {
|
||||
exception = details.exception;
|
||||
};
|
||||
|
||||
final TextPainter painter = TextPainter()
|
||||
..textDirection = TextDirection.ltr;
|
||||
|
||||
const String text = 'Hello\uD83DWorld';
|
||||
const double fontSize = 20.0;
|
||||
painter.text = const TextSpan(text: text, style: TextStyle(fontSize: fontSize));
|
||||
painter.layout();
|
||||
// The layout should include one replacement character.
|
||||
expect(painter.width, equals(fontSize));
|
||||
expect(exception, isNotNull);
|
||||
}, skip: kIsWeb);
|
||||
}
|
||||
|
||||
class MockCanvas extends Fake implements Canvas {
|
||||
|
||||
Reference in New Issue
Block a user