diff --git a/packages/slate/src/models/schema.js b/packages/slate/src/models/schema.js index 8091339fb..471a17a35 100644 --- a/packages/slate/src/models/schema.js +++ b/packages/slate/src/models/schema.js @@ -355,6 +355,10 @@ class Schema extends Record(DEFAULTS) { if (max != null && offset == max) nextDef() return !!child } + function rewind() { + offset -= 1 + index -= 1 + } if (rule.nodes != null) { nextDef() @@ -379,12 +383,18 @@ class Schema extends Record(DEFAULTS) { } if (def.kinds != null && !def.kinds.includes(child.kind)) { - if (offset >= min && nextDef()) continue + if (offset >= min && nextDef()) { + rewind() + continue + } return this.fail(CHILD_KIND_INVALID, { ...ctx, child, index }) } if (def.types != null && !def.types.includes(child.type)) { - if (offset >= min && nextDef()) continue + if (offset >= min && nextDef()) { + rewind() + continue + } return this.fail(CHILD_TYPE_INVALID, { ...ctx, child, index }) } } diff --git a/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js b/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js new file mode 100644 index 000000000..27d0bb5d5 --- /dev/null +++ b/packages/slate/test/schema/custom/child-kind-invalid-custom-optional-first.js @@ -0,0 +1,42 @@ +/** @jsx h */ + +import h from '../../helpers/h' + +export const schema = { + blocks: { + paragraph: {}, + quote: { + nodes: [ + { kinds: ['block'], types: ['image'], min: 0, max: 1 }, + { kinds: ['block'], types: ['paragraph'], min: 1 } + ], + normalize: (change, reason, { node, child }) => { + if (reason == 'child_kind_invalid') { + change.wrapBlockByKey(child.key, 'paragraph') + } + } + } + } +} + +export const input = ( + + + + text + + + +) + +export const output = ( + + + + + text + + + + +)