Fix normalizeNode
to keep text/inline nodes when removing blocks
#5768
+12
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)