1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 06:01:24 +02:00

Fixed regression in #4208 where normalization on empty block nodes could not be overridden (#4431)

This commit is contained in:
Andrew Herron
2021-08-12 06:01:44 +10:00
committed by GitHub
parent 6f47cbbe0d
commit 55ff8f00e4
3 changed files with 55 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': minor
---
Fixed regression in #4208 where normalization on empty block nodes could not be overridden

View File

@@ -997,16 +997,18 @@ export const Editor: EditorInterface = {
*/ */
for (const dirtyPath of getDirtyPaths(editor)) { for (const dirtyPath of getDirtyPaths(editor)) {
if (Node.has(editor, dirtyPath)) { if (Node.has(editor, dirtyPath)) {
const [node, _] = Editor.node(editor, dirtyPath) const entry = Editor.node(editor, dirtyPath)
const [node, _] = entry
// Add a text child to elements with no children. /*
// This is safe to do in any order, by definition it can't cause other paths to change. The default normalizer inserts an empty text node in this scenario, but it can be customised.
So there is some risk here.
As long as the normalizer only inserts child nodes for this case it is safe to do in any order;
by definition adding children to an empty node can't cause other paths to change.
*/
if (Element.isElement(node) && node.children.length === 0) { if (Element.isElement(node) && node.children.length === 0) {
const child = { text: '' } editor.normalizeNode(entry)
Transforms.insertNodes(editor, child, {
at: dirtyPath.concat(0),
voids: true,
})
} }
} }
} }

View File

@@ -0,0 +1,40 @@
/** @jsx jsx */
import { jsx } from '../..'
import { Editor, Element, Transforms } from 'slate'
export const input = (
<editor>
<element type="body" />
</editor>
)
// patch in a custom normalizer that inserts empty paragraphs in the body instead of text nodes
// this test also verifies the new node itself is also normalized, because it's inserting a non-normalized node
const editor = (input as unknown) as Editor
const defaultNormalize = editor.normalizeNode
editor.normalizeNode = entry => {
const [node, path] = entry
if (
Element.isElement(node) &&
node.children.length === 0 &&
(node as any).type === 'body'
) {
const child = { type: 'paragraph', children: [] }
Transforms.insertNodes(editor, child, {
at: path.concat(0),
voids: true,
})
} else {
defaultNormalize(entry)
}
}
export const output = (
<editor>
<element type="body">
<element type="paragraph">
<text />
</element>
</element>
</editor>
)