AppFlowy-IO / AppFlowy-IO/appflowy-editor

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

未关闭
#767 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug p0
主要语言
Dart
星标
684
派生
329
PR 合并指标
30 天内没有已合并 PR

描述

### 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_

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。