facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
19.98k stars 1.7k forks source link

Bug: spliceText throws error on parameter modification in LexicalSelection #6804

Closed mdmahendri closed 2 weeks ago

mdmahendri commented 2 weeks ago

Lexical version: v0.19.0+git

Steps To Reproduce

  1. Edit line 1155 LexicalSelection.ts from firstNode.spliceText(firstPoint.offset, del, '', true); to firstNode.spliceText(firstPoint.offset, del, '', false); //changing moveSelection=false
  2. select any plain text and click insert code block
  3. delete character from the end of the code block
  4. i suspect the same behavior will happen to all elements with OuterTag

Link to code example: this bug is easy to reproduce using clone of the git

...
    if (firstNode === lastNode && $isTextNode(firstNode)) {
      const del = Math.abs(focus.offset - anchor.offset);
      firstNode.spliceText(firstPoint.offset, del, '', false);
      fixText(firstNode, del);
      return;
    }
...

The current behavior

changing the value of moveSelection throws error

IndexSizeError: Failed to execute 'setBaseAndExtent' on 'Selection': The offset 11 is larger than the node's length (10).
    at updateDOMSelection (LexicalSelection.ts:2813:18)
    at $commitPendingUpdates (LexicalUpdates.ts:627:9)
    at LexicalUpdates.ts:1016:9

The expected behavior

I am new to lexical, i do not have any information when or where moveSelection is important

Impact of fix

It will be important to fix if someone decided to make their custom LexicalSelection.ts or LexicalTextNode.ts

etrepum commented 2 weeks ago

I think it's expected behavior to get errors when you make modifications to the internals of a library without really understanding what's going on. If you can demonstrate some unexpected behavior when using the unmodified library in a normal way then you should open a new issue.

mdmahendri commented 2 weeks ago

ah yes! thank you for swift response, i forgot that spliceText is not exclusively for deleting text/char