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

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

Open
Xazin opened this issue Apr 12, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@Xazin
Copy link
Collaborator

Xazin commented Apr 12, 2024

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

@Xazin Xazin added the bug Something isn't working label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant