mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-12 10:14:02 +02:00
fix schema normalization bug for child_max_invalid
This commit is contained in:
@@ -394,8 +394,8 @@ function validateNodes(node, rule, rules = []) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function rewind(pastZero = false) {
|
function rewind() {
|
||||||
if (index > 0 || pastZero) {
|
if (index > 0) {
|
||||||
index -= 1
|
index -= 1
|
||||||
count = lastCount
|
count = lastCount
|
||||||
}
|
}
|
||||||
@@ -422,10 +422,10 @@ function validateNodes(node, rule, rules = []) {
|
|||||||
const error = validateRules(child, def.match)
|
const error = validateRules(child, def.match)
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
// Since we want to report overflow on last matching child we don't
|
||||||
|
// immediately check for count > max, but instead do so once we find
|
||||||
|
// a child that doesn't match.
|
||||||
if (max != null && count - 1 > max) {
|
if (max != null && count - 1 > max) {
|
||||||
// Since we want to report overflow on last matching child we don't
|
|
||||||
// immediately check for count > max, but instead do so once we find
|
|
||||||
// a child that doesn't match.
|
|
||||||
rewind()
|
rewind()
|
||||||
return fail('child_max_invalid', {
|
return fail('child_max_invalid', {
|
||||||
rule,
|
rule,
|
||||||
@@ -441,22 +441,21 @@ function validateNodes(node, rule, rules = []) {
|
|||||||
// If there are more groups after this one then child might actually
|
// If there are more groups after this one then child might actually
|
||||||
// be valid.
|
// be valid.
|
||||||
if (nextDef()) {
|
if (nextDef()) {
|
||||||
// We already have all children required for current group, so this
|
// If we've already satisfied the minimum for the current group,
|
||||||
// error can safely be ignored.
|
// then we can rewind and proceed to the next group.
|
||||||
if (lastCount - 1 >= lastMin) {
|
if (lastCount - 1 >= lastMin) {
|
||||||
rewind(true)
|
index -= 1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise we know that current value is underflowing. There are
|
// Otherwise we know that current value is underflowing. There are
|
||||||
// three possible causes: there might just not be enough elements
|
// three possible causes for this...
|
||||||
// for current group, and current child is in fact the first of
|
|
||||||
// the next group; current group is underflowing, but there is also
|
// 1. There might just not be enough elements for current group, and
|
||||||
// an invalid child before the next group; or current group is not
|
// current child is in fact the first of the next group. If so, the
|
||||||
// underflowing but it appears so because there's an invalid child
|
// next def will not report errors, in which case we can rewind and
|
||||||
// between its members.
|
// report an minimum error.
|
||||||
if (validateRules(child, def.match) == null) {
|
if (validateRules(child, def.match) == null) {
|
||||||
// It's the first case, so we just report an underflow.
|
|
||||||
rewind()
|
rewind()
|
||||||
return fail('child_min_invalid', {
|
return fail('child_min_invalid', {
|
||||||
rule,
|
rule,
|
||||||
@@ -466,11 +465,15 @@ function validateNodes(node, rule, rules = []) {
|
|||||||
limit: lastMin,
|
limit: lastMin,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. The current group is underflowing, but there is also an
|
||||||
|
// invalid child before the next group.
|
||||||
|
// 3. Or the current group is not underflowing but it appears so
|
||||||
|
// because there's an invalid child between its members.
|
||||||
// It's either the second or third case. If it's the second then
|
// It's either the second or third case. If it's the second then
|
||||||
// we could report an underflow, but presence of an invalid child
|
// we could report an underflow, but presence of an invalid child
|
||||||
// is arguably more important, so we report it first. It also lets
|
// is arguably more important, so we report it first. It also lets
|
||||||
// us avoid checking for which case exactly is it.
|
// us avoid checking for which case exactly is it.
|
||||||
|
|
||||||
error.rule = rule
|
error.rule = rule
|
||||||
error.node = node
|
error.node = node
|
||||||
error.child = child
|
error.child = child
|
||||||
@@ -497,9 +500,9 @@ function validateNodes(node, rule, rules = []) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since we want to report overflow on last matching child we don't
|
||||||
|
// immediately check for count > max, but do so after processing all nodes.
|
||||||
if (max != null && count > max) {
|
if (max != null && count > max) {
|
||||||
// Since we want to report overflow on last matching child we don't
|
|
||||||
// immediately check for count > max, but do so after processing all nodes.
|
|
||||||
return fail('child_max_invalid', {
|
return fail('child_max_invalid', {
|
||||||
rule,
|
rule,
|
||||||
node,
|
node,
|
||||||
|
@@ -0,0 +1,55 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
blocks: {
|
||||||
|
paragraph: {},
|
||||||
|
title: {},
|
||||||
|
quote: {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
match: [{ type: 'title' }],
|
||||||
|
min: 1,
|
||||||
|
max: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
match: [{ type: 'paragraph' }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<quote>
|
||||||
|
<block type="title">
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<block type="title">
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<paragraph>
|
||||||
|
<text />
|
||||||
|
</paragraph>
|
||||||
|
</quote>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<quote>
|
||||||
|
<block type="title">
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<paragraph>
|
||||||
|
<text />
|
||||||
|
</paragraph>
|
||||||
|
</quote>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
@@ -0,0 +1,63 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
blocks: {
|
||||||
|
heading: {},
|
||||||
|
quote: {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
match: [
|
||||||
|
{
|
||||||
|
type: 'heading',
|
||||||
|
data: { level: v => v === 1 },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
min: 1,
|
||||||
|
max: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
match: [
|
||||||
|
{
|
||||||
|
type: 'heading',
|
||||||
|
data: { level: v => v === 2 },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
min: 1,
|
||||||
|
max: 1,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<quote>
|
||||||
|
<block type="heading" data={{ level: 1 }}>
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<block type="heading" data={{ level: 2 }}>
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
</quote>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<quote>
|
||||||
|
<block type="heading" data={{ level: 1 }}>
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<block type="heading" data={{ level: 2 }}>
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
</quote>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
@@ -0,0 +1,39 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import h from '../../helpers/h'
|
||||||
|
|
||||||
|
export const schema = {
|
||||||
|
blocks: {
|
||||||
|
paragraph: {},
|
||||||
|
title: {},
|
||||||
|
quote: {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
match: [{ type: 'title' }],
|
||||||
|
min: 1,
|
||||||
|
max: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
match: [{ type: 'paragraph' }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<quote>
|
||||||
|
<block type="title">
|
||||||
|
<text />
|
||||||
|
</block>
|
||||||
|
<paragraph>
|
||||||
|
<text />
|
||||||
|
</paragraph>
|
||||||
|
</quote>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = input
|
Reference in New Issue
Block a user