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

Fix normalizeNode to keep text/inline nodes when removing blocks #5768

Merged
merged 2 commits into from
Nov 23, 2024

Conversation

ahoisl
Copy link
Contributor

@ahoisl ahoisl commented Nov 20, 2024

Description
normalizeNode ensures that all children of a node are either all blocks or inline/text nodes.
When blocks need to be removed, we can at least unwrap the nodes instead of removing them altogether and thus prevent data loss.
(As normalization is called again on changes, this recursively ensures that the inline/text nodes contained in a block eventually remain.
The other way round, i.e. when inline/text nodes need to be removed, is not fixable, since we do not know which block element to use to wrap the inline/text nodes.)

Issue
Fixes: no issue created yet

Example
See the changed unit tests. Instead of throwing away text nodes contained in a block, the inner text nodes are added to the parent node (and then merged).

Context
This change improves resiliency on invalid documents, e.g. if documents can be changed manually, uploaded by an API, another normalization function ill-behaves, or some other way exists to create invalid documents.
Without the change, on the first edit action in the editor, the document would be normalized and data is lost without an easy way for a user to cope with the behavior (you cannot copy & paste part of the editor because this would by default result in the same problem, the pasted part would be normalized and thrown away).

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

Use `unwrapNodes` instead of `removeNodes` to
convert block to inline/text nodes.
Copy link

changeset-bot bot commented Nov 20, 2024

🦋 Changeset detected

Latest commit: 9615b6e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ahoisl ahoisl changed the title Fix normalizeNode to at least keep text data when removing blocks Fix normalizeNode to keep text/inline nodes when removing blocks Nov 20, 2024
@dylans dylans merged commit 4bc552f into ianstormtaylor:main Nov 23, 2024
7 of 11 checks passed
@github-actions github-actions bot mentioned this pull request Nov 19, 2024
@ahoisl ahoisl deleted the fix-normalize-node-data-loss branch November 23, 2024 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants