Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: document getting first or last node exception #885

Merged
merged 1 commit into from
Sep 11, 2024
Merged
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
10 changes: 5 additions & 5 deletions lib/src/core/document/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ class Document {
final Node root;

/// First node of the document.
Node? get first => root.children.first;
Node? get first => root.children.firstOrNull;

/// Last node of the document.
Node? get last {
var last = root.children.last;
while (last.children.isNotEmpty) {
last = last.children.last;
Node? current = root.children.lastOrNull;
while (current != null && current.children.isNotEmpty) {
current = current.children.last;
}
return last;
return current;
}

/// Must call this method when the [Document] is no longer needed.
Expand Down
10 changes: 10 additions & 0 deletions test/core/document/document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,14 @@ void main() async {
final last = document.last!;
expect(last.delta!.toPlainText(), secondChild);
});

test('Document.blank().first is null', () {
final document = Document.blank();
expect(document.first, isNull);
});

test('Document.blank().last is null', () {
final document = Document.blank();
expect(document.last, isNull);
});
}
Loading