1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 04:20:26 +02:00

Fix child_min_invalid checks (#3219)

This commit is contained in:
Dylan Markow
2019-12-03 20:22:05 -06:00
committed by Ian Storm Taylor
parent b5fe096844
commit f355f8a6f0
3 changed files with 37 additions and 5 deletions

View File

@@ -130,7 +130,6 @@ export const checkAncestor = (
let g = 0
while (true) {
count++
const group = groups[g] as ChildValidation | undefined
const child = parent.children[index] as Descendant | undefined
const childPath = parentPath.concat(index)
@@ -172,6 +171,12 @@ export const checkAncestor = (
continue
}
if (
child &&
Editor.isMatch(editor, [child, childPath], group.match || {})
) {
count++
}
// Since we want to report overflow on last matching child we don't
// immediately v for count > max, but instead do so once we find
// a child that doesn't match.
@@ -197,7 +202,7 @@ export const checkAncestor = (
// If there's no child, we're either done, we're in an optional group, or
// we're missing a child in a group with a mininmum set.
if (!child) {
if (group.min != null && count <= group.min) {
if (group.min != null && count < group.min) {
return [
rule,
{

View File

@@ -0,0 +1,26 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'
export const schema = [
{
for: 'node',
match: { a: true },
validate: {
children: [
{ match: { b: true }, min: 1 },
{ match: { c: true }, min: 0 },
],
},
},
]
export const input = (
<editor>
<element a>
<element c>one</element>
</element>
</editor>
)
export const output = <editor />

View File

@@ -15,7 +15,8 @@ const schema = [
],
},
normalize: (editor, error) => {
const { code, path, index } = error
const { code, path } = error
const [index] = path
const type = index === 0 ? 'title' : 'paragraph'
switch (code) {
@@ -25,11 +26,11 @@ const schema = [
}
case 'child_min_invalid': {
const block = { type, children: [{ text: '', marks: [] }] }
Editor.insertNodes(editor, block, { at: path.concat(index) })
Editor.insertNodes(editor, block, { at: path })
break
}
case 'child_max_invalid': {
Editor.setNodes(editor, { type }, { at: path.concat(index) })
Editor.setNodes(editor, { type }, { at: path })
break
}
}