1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-13 10:44:02 +02:00

fix schema defaultNormalize to use normalize:false (#2083)

#### Is this adding or improving a _feature_ or fixing a _bug_?

Bug.

#### What's the new behavior?

All normalizing change methods should use `normalize: false` otherwise the normalization can recurse and cause strange errors.

#### Have you checked that...?

<!-- 
Please run through this checklist for your pull request: 
-->

* [x] The new code matches the existing patterns and styles.
* [x] The tests pass with `yarn test`.
* [x] The linter passes with `yarn lint`. (Fix errors with `yarn prettier`.)
* [x] The relevant examples still work. (Run examples with `yarn watch`.)
This commit is contained in:
Ian Storm Taylor
2018-08-15 12:34:50 -07:00
committed by GitHub
parent 5c7211e0fe
commit aa0ef4d24f

View File

@@ -391,7 +391,9 @@ class Schema extends Record(DEFAULTS) {
*/ */
function defaultNormalize(change, error) { function defaultNormalize(change, error) {
switch (error.code) { const { code, node, child, key, mark } = error
switch (code) {
case CHILD_OBJECT_INVALID: case CHILD_OBJECT_INVALID:
case CHILD_TYPE_INVALID: case CHILD_TYPE_INVALID:
case CHILD_UNKNOWN: case CHILD_UNKNOWN:
@@ -399,46 +401,52 @@ function defaultNormalize(change, error) {
case FIRST_CHILD_TYPE_INVALID: case FIRST_CHILD_TYPE_INVALID:
case LAST_CHILD_OBJECT_INVALID: case LAST_CHILD_OBJECT_INVALID:
case LAST_CHILD_TYPE_INVALID: { case LAST_CHILD_TYPE_INVALID: {
const { child, node } = error return child.object === 'text' &&
return child.object == 'text' && node.object === 'block' &&
node.object == 'block' && node.nodes.size === 1
node.nodes.size == 1 ? change.removeNodeByKey(node.key, { normalize: false })
? change.removeNodeByKey(node.key) : change.removeNodeByKey(child.key, { normalize: false })
: change.removeNodeByKey(child.key)
} }
case CHILD_REQUIRED: case CHILD_REQUIRED:
case NODE_TEXT_INVALID: case NODE_TEXT_INVALID:
case PARENT_OBJECT_INVALID: case PARENT_OBJECT_INVALID:
case PARENT_TYPE_INVALID: { case PARENT_TYPE_INVALID: {
const { node } = error return node.object === 'document'
return node.object == 'document' ? node.nodes.forEach(n =>
? node.nodes.forEach(child => change.removeNodeByKey(child.key)) change.removeNodeByKey(n.key, { normalize: false })
: change.removeNodeByKey(node.key) )
: change.removeNodeByKey(node.key, { normalize: false })
} }
case NODE_DATA_INVALID: { case NODE_DATA_INVALID: {
const { node, key } = error return node.data.get(key) === undefined && node.object !== 'document'
return node.data.get(key) === undefined && node.object != 'document' ? change.removeNodeByKey(node.key, { normalize: false })
? change.removeNodeByKey(node.key) : change.setNodeByKey(
: change.setNodeByKey(node.key, { data: node.data.delete(key) }) node.key,
{ data: node.data.delete(key) },
{ normalize: false }
)
} }
case NODE_IS_VOID_INVALID: { case NODE_IS_VOID_INVALID: {
const { node } = error return change.setNodeByKey(
return change.setNodeByKey(node.key, { isVoid: !node.isVoid }) node.key,
{ isVoid: !node.isVoid },
{ normalize: false }
)
} }
case NODE_MARK_INVALID: { case NODE_MARK_INVALID: {
const { node, mark } = error return node.getTexts().forEach(t =>
return node change.removeMarkByKey(t.key, 0, t.text.length, mark, {
.getTexts() normalize: false,
.forEach(t => change.removeMarkByKey(t.key, 0, t.text.length, mark)) })
)
} }
default: { default: {
const { node } = error return change.removeNodeByKey(node.key, { normalize: false })
return change.removeNodeByKey(node.key)
} }
} }
} }