From b9908f6b86f8e86e8bff34f0b751038fdfbd94a1 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 23 Aug 2021 16:03:49 -0700 Subject: [PATCH] Fixes renderparagraph crashes due to truncated semantics node (#88190) * Fixes renderparagraph crashes due to truncated semantics node * add period --- .../flutter/lib/src/rendering/editable.dart | 1 + .../flutter/lib/src/rendering/paragraph.dart | 18 ++++++---- .../test/rendering/paragraph_test.dart | 35 ++++++++++++++++++- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart index b55400da27..eb590a0834 100644 --- a/packages/flutter/lib/src/rendering/editable.dart +++ b/packages/flutter/lib/src/rendering/editable.dart @@ -2814,6 +2814,7 @@ class RenderEditable extends RenderBox with RelayoutWhenSystemFontsChangeMixin, children.elementAt(childIndex).isTagged(PlaceholderSpanIndexSemanticsTag(placeholderIndex))) { final SemanticsNode childNode = children.elementAt(childIndex); final TextParentData parentData = child!.parentData! as TextParentData; + assert(parentData.scale != null); childNode.rect = Rect.fromLTWH( childNode.rect.left, childNode.rect.top, diff --git a/packages/flutter/lib/src/rendering/paragraph.dart b/packages/flutter/lib/src/rendering/paragraph.dart index 1bd12f59d3..9e14d5ae5a 100644 --- a/packages/flutter/lib/src/rendering/paragraph.dart +++ b/packages/flutter/lib/src/rendering/paragraph.dart @@ -933,13 +933,17 @@ class RenderParagraph extends RenderBox children.elementAt(childIndex).isTagged(PlaceholderSpanIndexSemanticsTag(placeholderIndex))) { final SemanticsNode childNode = children.elementAt(childIndex); final TextParentData parentData = child!.parentData! as TextParentData; - childNode.rect = Rect.fromLTWH( - childNode.rect.left, - childNode.rect.top, - childNode.rect.width * parentData.scale!, - childNode.rect.height * parentData.scale!, - ); - newChildren.add(childNode); + assert(parentData.scale != null || parentData.offset == Offset.zero); + // parentData.scale may be null if the render object is truncated. + if (parentData.scale != null) { + childNode.rect = Rect.fromLTWH( + childNode.rect.left, + childNode.rect.top, + childNode.rect.width * parentData.scale!, + childNode.rect.height * parentData.scale!, + ); + newChildren.add(childNode); + } childIndex += 1; } child = childAfter(child!); diff --git a/packages/flutter/test/rendering/paragraph_test.dart b/packages/flutter/test/rendering/paragraph_test.dart index 4bc6ba5937..3f85e37119 100644 --- a/packages/flutter/test/rendering/paragraph_test.dart +++ b/packages/flutter/test/rendering/paragraph_test.dart @@ -702,7 +702,40 @@ void main() { expect(boxes[8], const TextBox.fromLTRBD(14.0, 28.0, 28.0, 42.0 , TextDirection.ltr)); }, skip: isBrowser); // https://github.com/flutter/flutter/issues/61020 - test('Supports gesture recognizer semantics', () { + test('Does not include the semantics node of truncated rendering children', () { + // Regression test for https://github.com/flutter/flutter/issues/88180 + const double screenWidth = 100; + const String sentence = 'truncated'; + final List renderBoxes = [ + RenderParagraph( + const TextSpan(text: sentence), textDirection: TextDirection.ltr), + ]; + final RenderParagraph paragraph = RenderParagraph( + const TextSpan( + text: 'a long line to be truncated.', + children: [ + WidgetSpan(child: Text(sentence)), + ], + ), + overflow: TextOverflow.ellipsis, + textScaleFactor: 1.0, + children: renderBoxes, + textDirection: TextDirection.ltr, + ); + layout(paragraph, constraints: const BoxConstraints(maxWidth: screenWidth)); + final SemanticsNode result = SemanticsNode(); + final SemanticsNode truncatedChild = SemanticsNode(); + truncatedChild.tags = {const PlaceholderSpanIndexSemanticsTag(0)}; + paragraph.assembleSemanticsNode(result, SemanticsConfiguration(), [truncatedChild]); + // It should only contain the semantics node of the TextSpan. + expect(result.childrenCount, 1); + result.visitChildren((SemanticsNode node) { + expect(node != truncatedChild, isTrue); + return true; + }); + }); + + test('Supports gesture recognizer semantics', () { final RenderParagraph paragraph = RenderParagraph( TextSpan(text: _kText, children: [ TextSpan(text: 'one', recognizer: TapGestureRecognizer()..onTap = () {}),