From 06cdc0299f70af446776d0706403f1df2c65905d Mon Sep 17 00:00:00 2001 From: Yegor Date: Mon, 4 Nov 2019 10:16:38 -0800 Subject: [PATCH] fix getBoxesForRange for zero-length ranges (flutter/engine#13483) --- .../web_ui/lib/src/engine/text/paragraph.dart | 2 +- .../lib/web_ui/test/paragraph_test.dart | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart index 3214fb897d..6574cbddbc 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart @@ -201,7 +201,7 @@ class EngineParagraph implements ui.Paragraph { }) { assert(boxHeightStyle != null); assert(boxWidthStyle != null); - if (_plainText == null) { + if (_plainText == null || start == end) { return []; } diff --git a/engine/src/flutter/lib/web_ui/test/paragraph_test.dart b/engine/src/flutter/lib/web_ui/test/paragraph_test.dart index 99700e11ed..4d5c0a92c8 100644 --- a/engine/src/flutter/lib/web_ui/test/paragraph_test.dart +++ b/engine/src/flutter/lib/web_ui/test/paragraph_test.dart @@ -96,4 +96,40 @@ void main() async { paragraph2.layout(ParagraphConstraints(width: fontSize * 5.0)); expect(paragraph2.height, closeTo(fontSize, 0.001)); // because it wraps }); + + test('getBoxesForRange returns a box', () { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: 10, + textDirection: TextDirection.rtl, + )); + builder.addText('abcd'); + final Paragraph paragraph = builder.build(); + paragraph.layout(const ParagraphConstraints(width: 1000)); + expect( + paragraph.getBoxesForRange(1, 2).single, + const TextBox.fromLTRBD( + 10, + 0, + 20, + 10, + TextDirection.rtl, + ), + ); + }); + + test('getBoxesForRange return empty list for zero-length range', () { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle( + fontFamily: 'Ahem', + fontStyle: FontStyle.normal, + fontWeight: FontWeight.normal, + fontSize: 10, + )); + builder.addText('abcd'); + final Paragraph paragraph = builder.build(); + paragraph.layout(const ParagraphConstraints(width: 1000)); + expect(paragraph.getBoxesForRange(0, 0), isEmpty); + }); }