AppFlowy-IO / AppFlowy-IO/appflowy-editor

[Bug] replaceText has the possibility of throwing `Bad state: no element`

Đang mở
#767 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
bug p0
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ả

### Bug Description

The transaction of `replaceText` does not check the length of the Delta before calling `.first`.

### How to Reproduce

Immediately after inserting a TextDelta with no attribute, call replaceText.

Eg.
```
final transaction = editorState.transaction
..insertTextDelta(
node,
selection.end.offset,
Delta(operations: [TextInsert('insert')]),
)
..replaceText(
node,
selection.end.offset - 3,
3,
'replacement',
);
```

### Expected Behavior

Change the code in `replaceText`:
```
var newAttributes = attributes;
if (index != 0 && attributes == null) {
newAttributes = delta.slice(max(index - 1, 0), index).first.attributes;
if (newAttributes == null) {
final slicedDelta = delta.slice(index, index + length);
if (slicedDelta.isNotEmpty) {
newAttributes = slicedDelta.first.attributes;
}
}
}
```
To add a required check and make sure the logic is correct.

Specifically the call to `.first` on `delta.slice(max(index - 1, 0), index)` is what might fail.

### Operating System

*

### AppFlowy Editor Version(s)

main

### Screenshots

_No response_

### Additional Context

_No response_

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.