1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-19 21:51:51 +02:00

feat(normalize-node): Adding children field to prevent erronous nodes from breaking notebooks. (#5620)

* FEAT: Adding children array to erronous node to prevent various `children undefined` errors

* CHORE: Adding changeset
This commit is contained in:
John Costa
2024-04-02 00:19:54 +01:00
committed by GitHub
parent e71e6ce247
commit 4470f37057
2 changed files with 18 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': patch
---
Making `normalizeNode` capable of normalizing erronous nodes, making slate more resilient.

View File

@@ -75,6 +75,19 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
}
}
} else {
// If the child is not a text node, and doesn't have a `children` field,
// then we have an invalid node that will upset slate.
//
// eg: `{ type: 'some_node' }`.
//
// To prevent slate from breaking, we can add the `children` field,
// and now that it is valid, we can to many more operations easily,
// such as extend normalizers to fix erronous structure.
if (!Text.isText(child) && !('children' in child)) {
const elementChild = child as Element
elementChild.children = []
}
// Merge adjacent text nodes that are empty or match.
if (prev != null && Text.isText(prev)) {
if (Text.equals(child, prev, { loose: true })) {