Skip to content

[lexical][lexical-mark] Bug Fix: $wrapSelectionInMarkNode with element points - #7132

Merged
etrepum merged 1 commit into
meta:mainfrom
etrepum:fix-marknode-tests
Feb 5, 2025
Merged

etrepum merged 1 commit into
meta:mainfrom
etrepum:fix-marknode-tests

Conversation

@etrepum

@etrepum etrepum commented Feb 4, 2025

Copy link
Copy Markdown
Collaborator

Description

$wrapSelectionInMarkNode had incorrect logic for dealing with point offsets, and the tests were written to work with this incorrect logic.

This PR fixes that logic and adds a __DEV__ invariant to PointType to check that we are not setting text or element points with the wrong type of node (AFAIK this test suite was the only place doing this incorrectly). The logic was much simpler once extract() was used instead of getNodes().

There is some risk if there is code that is specifically creating invalid points to work around this issue as the tests were written, instead of the correct workaround which would've been to normalize the endpoints of the selection to text nodes.

There is some other risk with the new PointType.set invariant, but only in __DEV__, and this is only identifying potential problems that would've been hard to debug elsewhere.

Test plan

Before

After

Everything should work as before, but the way it was supposed to.

Follow-up considerations

I think as a follow up this API could change to deprecate isBackwards. It doesn't do anything useful, and it can be inferred from the RangeSelection.

I noticed that the original code would almost certainly behave incorrectly with a backwards selection in most multi-node cases, which I fixed by flipping the selection. The existing tests did not cover this case and I did not write additional tests to cover that case (but it should be fixed).

… with element points; add dev invariant for PointType type correctness
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 4, 2025
@vercel

vercel Bot commented Feb 4, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 4, 2025 7:03pm
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 4, 2025 7:03pm

@etrepum
etrepum added this pull request to the merge queue Feb 5, 2025
Merged via the queue into meta:main with commit dde1d3f Feb 5, 2025
@etrepum
etrepum deleted the fix-marknode-tests branch February 5, 2025 17:18

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 515051ea Deployed Feb 4, 2025 by vercel[bot]
Preview – lexical — 515051ea Deployed Feb 4, 2025 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants