flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.15k stars 27.47k forks source link

Paragraph.getWordBoundary doesn't return empty TextRange when offset out of bounds #75494

Open suragch opened 3 years ago

suragch commented 3 years ago

If you supply an out-of-range offset to Paragraph.getLineBoundary, then the result is TextRange.empty as expected. This is demonstrated with the following test:

test('Line boundary for out-of-range offset returns empty range', () {
  const text = 'Hello world';
  const offset = 1000;
  final paragraphStyle = ParagraphStyle(
    textDirection: TextDirection.ltr,
  );
  final paragraphBuilder = ParagraphBuilder(paragraphStyle)
    ..addText(text);
  final constraints = ParagraphConstraints(width: 300);
  final paragraph = paragraphBuilder.build();
  paragraph.layout(constraints);
  final range = paragraph.getLineBoundary(TextPosition(offset: offset));
  expect(range, TextRange.empty);
});

However if you do the same thing with Paragraph.getWordBoundary, the test fails. It returns a range from the end of the text to the offset. This isn't the behavior I expect, but is this intentional?

Here is a test to demonstrate it:

test('word boundary for out-of-range offset returns empty range', () {
  const text = 'Hello world';
  const offset = 1000;
  final paragraphStyle = ParagraphStyle(
    textDirection: TextDirection.ltr,
  );
  final paragraphBuilder = ParagraphBuilder(paragraphStyle)
    ..addText(text);
  final constraints = ParagraphConstraints(width: 300);
  final paragraph = paragraphBuilder.build();
  paragraph.layout(constraints);
  final range = paragraph.getWordBoundary(TextPosition(offset: offset));
  expect(range, TextRange.empty);
});

This test fails:

Expected: TextRange:<TextRange(start: -1, end: -1)>
  Actual: TextRange:<TextRange(start: 11, end: 1000)>

Here is the relevant part of flutter doctor -v:

[✓] Flutter (Channel beta, 1.25.0-8.3.pre, on Mac OS X 10.15.4 19E266 darwin-x64, locale en)
    • Flutter version 1.25.0-8.3.pre at /Users/suragch/fvm/versions/beta
    • Framework revision 5d36f2e7f5 (3 weeks ago), 2021-01-14 15:57:49 -0800
    • Engine revision 7a8f8ca02c
    • Dart version 2.12.0 (build 2.12.0-133.7.beta)
darshankawar commented 3 years ago

Verified this on latest stable and issue persists.

Screenshot 2021-02-05 at 4 04 45 PM
flutter doctor -v ``` [✓] Flutter (Channel stable, 1.22.6, on Mac OS X 10.15.4 19E2269 darwin-x64, locale en-IN) • Flutter version 1.22.6 at /Users/dhs/documents/Fluttersdk/flutter • Framework revision 9b2d32b605 (13 days ago), 2021-01-22 14:36:39 -0800 • Engine revision 2f0af37152 • Dart version 2.10.5 [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0) • Android SDK at /Users/dhs/Library/Android/sdk • Platform android-30, build-tools 30.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Xcode 12.3, Build version 12C33 • CocoaPods version 1.9.3 [!] Android Studio (version 4.1) • Android Studio at /Applications/Android Studio.app/Contents ✗ Flutter plugin not installed; this adds Flutter specific functionality. ✗ Dart plugin not installed; this adds Dart specific functionality. • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495) [✓] VS Code (version 1.52.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.18.1 [✓] Connected device (1 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.2 ! Doctor found issues in 1 category. ```
chinmaygarde commented 3 years ago

cc @jason-simmons for followup on whether SkParagraph specifies consistency on this.

danagbemava-nc commented 1 year ago

The test still fails on latest stable and master.

full test ```dart import 'dart:ui'; import 'package:flutter_test/flutter_test.dart'; void main() { test('Line boundary for out-of-range offset returns empty range', () { const text = 'Hello world'; const offset = 1000; final paragraphStyle = ParagraphStyle( textDirection: TextDirection.ltr, ); final paragraphBuilder = ParagraphBuilder(paragraphStyle)..addText(text); const constraints = ParagraphConstraints(width: 300); final paragraph = paragraphBuilder.build(); paragraph.layout(constraints); final range = paragraph.getLineBoundary(const TextPosition(offset: offset)); expect(range, TextRange.empty); }); test('word boundary for out-of-range offset returns empty range', () { const text = 'Hello world'; const offset = 1000; final paragraphStyle = ParagraphStyle( textDirection: TextDirection.ltr, ); final paragraphBuilder = ParagraphBuilder(paragraphStyle)..addText(text); const constraints = ParagraphConstraints(width: 300); final paragraph = paragraphBuilder.build(); paragraph.layout(constraints); final range = paragraph.getWordBoundary(const TextPosition(offset: offset)); expect(range, TextRange.empty); }); } ```
Expected: TextRange:<TextRange(start: -1, end: -1)>
  Actual: TextRange:<TextRange(start: 11, end: 11)>
package:test_api                                    expect
package:flutter_test/src/widget_tester.dart 460:16  expect
test/widget_test.dart 31:5                          main.<fn>
/test/widget_test.dart:31
✖ word boundary for out-of-range offset returns empty range
flutter doctor -v ``` [✓] Flutter (Channel stable, 3.3.8, on macOS 13.0.1 22A400 darwin-arm, locale en-GB) • Flutter version 3.3.8 on channel stable at /Users/nexus/dev/sdks/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 52b3dc25f6 (8 days ago), 2022-11-09 12:09:26 +0800 • Engine revision 857bd6b74c • Dart version 2.18.4 • DevTools version 2.15.0 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-33, build-tools 33.0.0 • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14B47b • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio • Android Studio at /Applications/Android Studio Preview.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [!] Android Studio • Android Studio at /Applications/Android Studio Preview 2.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] Android Studio (version 2021.3) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] Android Studio (version 2021.3) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] VS Code (version 1.73.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (4 available) • M2007J20CG (mobile) • 5dd3be00 • android-arm64 • Android 12 (API 31) • iPhone 14 Pro (mobile) • 4F72110C-F38B-4CF9-93C4-4D6042148D28 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-1 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.0.1 22A400 darwin-arm • Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 2 categories. ``` ``` [!] Flutter (Channel master, 3.6.0-4.0.pre.28, on macOS 13.0.1 22A400 darwin-arm64, locale en-GB) • Flutter version 3.6.0-4.0.pre.28 on channel master at /Users/nexus/dev/sdks/flutters ! Warning: `flutter` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/nexus/dev/sdks/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nexus/dev/sdks/flutters. Consider adding /Users/nexus/dev/sdks/flutters/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision c0719509a1 (8 hours ago), 2022-11-16 23:55:08 -0500 • Engine revision d422023f39 • Dart version 2.19.0 (build 2.19.0-406.0.dev) • DevTools version 2.19.0 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) • Android SDK at /Users/nexus/Library/Android/sdk • Platform android-33, build-tools 33.0.0 • Java binary at: /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14B47b • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [!] Android Studio • Android Studio at /Applications/Android Studio Preview.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [!] Android Studio • Android Studio at /Applications/Android Studio Preview 2.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart ✗ Unable to find bundled Java version. • Try updating or re-installing Android Studio. [✓] Android Studio (version 2021.3) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9123335/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] Android Studio (version 2021.3) • Android Studio at /Users/nexus/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/213.7172.25.2113.9014738/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) [✓] VS Code (version 1.73.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.52.0 [✓] Connected device (4 available) • M2007J20CG (mobile) • 5dd3be00 • android-arm64 • Android 12 (API 31) • iPhone 14 Pro (mobile) • 4F72110C-F38B-4CF9-93C4-4D6042148D28 • ios • com.apple.CoreSimulator.SimRuntime.iOS-16-1 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 13.0.1 22A400 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.110 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 3 categories. ```