From 74ac7c476c73c6cc076fcb1919e55981a82ed8c7 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Wed, 25 Aug 2021 23:01:03 -0700 Subject: [PATCH] Fix empty textspan with spell out crashes (#88738) --- packages/flutter/lib/src/painting/text_span.dart | 9 +++++---- packages/flutter/test/rendering/paragraph_test.dart | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_span.dart b/packages/flutter/lib/src/painting/text_span.dart index a80ce78bd2..0887794738 100644 --- a/packages/flutter/lib/src/painting/text_span.dart +++ b/packages/flutter/lib/src/painting/text_span.dart @@ -371,13 +371,14 @@ class TextSpan extends InlineSpan implements HitTestTarget, MouseTrackerAnnotati final bool effectiveSpellOut = spellOut ?? inheritedSpellOut; if (text != null) { + final int textLength = semanticsLabel?.length ?? text!.length; collector.add(InlineSpanSemanticsInformation( text!, stringAttributes: [ - if (effectiveSpellOut) - ui.SpellOutStringAttribute(range: TextRange(start: 0, end: semanticsLabel?.length ?? text!.length)), - if (effectiveLocale != null) - ui.LocaleStringAttribute(locale: effectiveLocale, range: TextRange(start: 0, end: semanticsLabel?.length ?? text!.length)), + if (effectiveSpellOut && textLength > 0) + ui.SpellOutStringAttribute(range: TextRange(start: 0, end: textLength)), + if (effectiveLocale != null && textLength > 0) + ui.LocaleStringAttribute(locale: effectiveLocale, range: TextRange(start: 0, end: textLength)), ], semanticsLabel: semanticsLabel, recognizer: recognizer, diff --git a/packages/flutter/test/rendering/paragraph_test.dart b/packages/flutter/test/rendering/paragraph_test.dart index 3f85e37119..b8e2cb08e2 100644 --- a/packages/flutter/test/rendering/paragraph_test.dart +++ b/packages/flutter/test/rendering/paragraph_test.dart @@ -749,6 +749,18 @@ void main() { paragraph.assembleSemanticsNode(SemanticsNode(), SemanticsConfiguration(), []); }); + test('Supports empty text span with spell out', () { + final RenderParagraph paragraph = RenderParagraph( + const TextSpan(text: '', spellOut: true), + textDirection: TextDirection.rtl, + ); + layout(paragraph); + final SemanticsNode node = SemanticsNode(); + paragraph.assembleSemanticsNode(node, SemanticsConfiguration(), []); + expect(node.attributedLabel.string, ''); + expect(node.attributedLabel.attributes.length, 0); + }); + test('Asserts on unsupported gesture recognizer', () { final RenderParagraph paragraph = RenderParagraph( TextSpan(text: _kText, children: [