Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ class _MobileSelectionServiceWidgetState
context.read<EditorScrollController>(),
);

if (sortedNodes.isEmpty) {
return null;
}

return editorState.getNodeInOffset(
sortedNodes,
offset,
Expand Down
14 changes: 13 additions & 1 deletion lib/src/editor/editor_component/service/selection/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,19 @@ extension EditorStateSelection on EditorState {
// Create rect cache for this operation if not provided
rectCache ??= {};

if (start < 0 && end >= sortedNodes.length) {
// Guard against empty node lists and out-of-range indices.
//
// The mobile selection service calls this method with
// `sortedNodes.length - 1` as `end`, which becomes -1 when the
// document is empty. The previous guard used `&&` which only
// returned null when BOTH bounds were invalid, allowing an empty
// list to slip through and crash in [_findCloseNode].
if (sortedNodes.isEmpty ||
start < 0 ||
end < 0 ||
start > end ||
start >= sortedNodes.length ||
end >= sortedNodes.length) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/mobile_selection_service.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/selection/shared.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

import '../../../../test_helper.dart';

/// Regression test for AppFlowy Editor issue #1218.
///
/// The mobile selection service calls `editorState.getNodeInOffset(...)` with
/// `sortedNodes.length - 1` as the `end` argument. When the document is empty
/// (Document.blank(withInitialText: false)), `sortedNodes` is empty and `end`
/// becomes -1. The guard in `shared.dart` used `&&` instead of `||`, so it
/// did not return null for this case, and the code proceeded to index into an
/// empty list, raising a RangeError.
void main() {
group('EditorStateSelection.getNodeInOffset with empty document', () {
test('returns null for empty node list (start=0, end=-1)', () {
final editorState = EditorState.blank(withInitialText: false);
addTearDown(editorState.dispose);

// This mirrors the call the mobile selection service makes when the
// document has no children: getNodeInOffset([], offset, 0, length - 1)
// where length is 0, so end is -1.
final result = editorState.getNodeInOffset(
<Node>[],
const Offset(100, 100),
0,
-1,
);

expect(result, isNull);
});

test('returns null for empty node list with explicit negative end', () {
final editorState = EditorState.blank(withInitialText: false);
addTearDown(editorState.dispose);

final result = editorState.getNodeInOffset(
<Node>[],
const Offset(50, 50),
-1,
-1,
);

expect(result, isNull);
});

test('returns null for empty node list with out-of-bounds end', () {
final editorState = EditorState.blank(withInitialText: false);
addTearDown(editorState.dispose);

final result = editorState.getNodeInOffset(
<Node>[],
const Offset(50, 50),
0,
100,
);

expect(result, isNull);
});
});

group('MobileSelectionServiceWidget with empty document', () {
testWidgets('getNodeInOffset returns null without crashing', (tester) async {
final editorState = EditorState.blank(withInitialText: false);
addTearDown(editorState.dispose);

final scrollController = EditorScrollController(
editorState: editorState,
shrinkWrap: true,
);
addTearDown(scrollController.dispose);

final key = GlobalKey();

await tester.buildAndPump(
MultiProvider(
providers: [
Provider<EditorState>(create: (_) => editorState),
Provider<EditorScrollController>(create: (_) => scrollController),
],
child: MobileSelectionServiceWidget(
key: key,
child: const SizedBox.shrink(),
),
),
);
await tester.pumpAndSettle();

// Access the selection service state and call getNodeInOffset.
// This is the exact code path triggered when a user taps the editor
// surface on a mobile device with an empty document.
final state = key.currentState as AppFlowySelectionService;
final node = state.getNodeInOffset(const Offset(50, 50));

expect(node, isNull);
expect(tester.takeException(), isNull);
});
});
}