mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-02-13 11:44:55 +01:00
fix nodes validation with an optional first child (#1444)
When moving to the next definition because of a fulfilled min a child could slip through without being valid.
This commit is contained in:
parent
199c32a2b5
commit
a4028cac6b
@ -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 })
|
||||
}
|
||||
}
|
||||
|
@ -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 = (
|
||||
<value>
|
||||
<document>
|
||||
<quote>
|
||||
text
|
||||
</quote>
|
||||
</document>
|
||||
</value>
|
||||
)
|
||||
|
||||
export const output = (
|
||||
<value>
|
||||
<document>
|
||||
<quote>
|
||||
<paragraph>
|
||||
text
|
||||
</paragraph>
|
||||
</quote>
|
||||
</document>
|
||||
</value>
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user