AppFlowy-IO / AppFlowy-IO/appflowy-editor

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

Aperta
#1,218 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Dart
Stelle
684
Fork
329
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

### 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;
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.