From c20580f90e3e478eb59041929fc269698789ba4a Mon Sep 17 00:00:00 2001 From: Jim Fisher Date: Wed, 5 Jan 2022 19:05:21 +0000 Subject: [PATCH] Warn when normalization removes node Slate requires the invariant that children are all blocks or all inlines. It enforces this in default normalization by removing children. When such a node is removed, it's almost certainly due to a programming error: the developer needs to fix their application to ensure it maintains this invariant. But currently Slate does not tell the developer this. I made such a programming error, and spent a long time debugging nodes that mysteriously went missing. I would have fixed it within 30 seconds if Slate had warned me when it detected this error. (Note I have used console.warn despite the eslint rule. As far as I can see, Slate has no other facility for runtime warnings.) --- packages/slate/src/create-editor.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/slate/src/create-editor.ts b/packages/slate/src/create-editor.ts index b1a903b5eb6..f6f2a36de18 100644 --- a/packages/slate/src/create-editor.ts +++ b/packages/slate/src/create-editor.ts @@ -227,7 +227,16 @@ export const createEditor = (): Editor => { // other inline nodes, or parent blocks that only contain inlines and // text. if (isInlineOrText !== shouldHaveInlines) { - Transforms.removeNodes(editor, { at: path.concat(n), voids: true }) + // eslint-disable-next-line no-console + console.warn( + `Removing ${ + isInlineOrText ? 'inline' : 'block' + } node at path ${path.concat(n)} because parent expects ${ + shouldHaveInlines ? 'inline' : 'block' + } children` + ) + const removePath = path.concat(n) + Transforms.removeNodes(editor, { at: removePath, voids: true }) n-- } else if (Element.isElement(child)) { // Ensure that inline nodes are surrounded by text nodes.