AppFlowy-IO / AppFlowy-IO/appflowy-editor

`ArgumentError` in `_findCloseNode` when tapping an empty EditorState

Đang mở
#1,218 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Dart
Star
684
Fork
329
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

### Describe the bug
When `EditorState` is initialized with a completely empty `Document` (i.e. containing 0 nodes, such as `Document.blank(withInitialText: false)`) and the user taps inside the editor, an `ArgumentError: Invalid argument(s): 0` is thrown by `_findCloseNode` in `shared.dart`.

### Steps to Reproduce
1. Initialize `EditorState` with `Document.blank(withInitialText: false)`.
2. Render `AppFlowyEditor`.
3. Tap on the editor surface.
4. The application throws the following exception:
```
ArgumentError: Invalid argument(s): 0

Stack trace:
EditorStateSelection._findCloseNode
EditorStateSelection.getNodeInOffset
_MobileSelectionServiceWidgetState.getNodeInOffset
_MobileSelectionServiceWidgetState.getPositionInOffset
_MobileSelectionServiceWidgetState._onTapUpAndroid
```

### Expected Behavior
The editor should handle the empty node list gracefully without throwing an exception, likely by returning `null` from `getNodeInOffset` and safely ignoring the tap selection.

### Root Cause Analysis
In `lib/src/editor/editor_component/service/selection/shared.dart`, the `getNodeInOffset` function attempts to guard against invalid bounds:

```dart
if (start < 0 && end >= sortedNodes.length) {
return null;
}
```

If `sortedNodes` is empty, `getNodeInOffset` receives `start = 0` and `end = -1`.
The condition `end >= sortedNodes.length` evaluates to `-1 >= 0`, which is `false`. Because of the `&&` operator, the bounds check fails to catch the empty array case.

The code then proceeds to call `_findCloseNode(sortedNodes, 0, -1)`.
Inside `_findCloseNode`, the binary search loop is skipped, and it returns:
```dart
return min.clamp(start, end);
```
Since `min = 0`, `start = 0`, and `end = -1`, `0.clamp(0, -1)` throws an `ArgumentError: Invalid argument(s): 0` because the lower bound is greater than the upper bound.

### Proposed Fix
Change the bounds check in `getNodeInOffset` to:
```dart
if (start < 0 || end >= sortedNodes.length || start > end) {
return null;
}
```
Or alternatively, add an explicit check for empty lists:
```dart
if (sortedNodes.isEmpty) return null;
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.