AppFlowy-IO / AppFlowy-IO/appflowy-editor

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

Abierto
#1,218 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Dart
Estrellas
684
Forks
329
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.