Unverified Commit 3a51eb12 authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

Substitute a replacement character for invalid UTF-16 text in a TextSpan (#84887)

parent e1660e4d
......@@ -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 {
......
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