From d2ff824e61bcd1094169c3ae122c8b789bab3eba Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 31 Oct 2017 23:45:05 -0700 Subject: [PATCH] fix schema first/last rules when nodes is empty --- packages/slate/src/models/schema.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/slate/src/models/schema.js b/packages/slate/src/models/schema.js index 4fd0bbd94..77ef374fa 100644 --- a/packages/slate/src/models/schema.js +++ b/packages/slate/src/models/schema.js @@ -304,26 +304,28 @@ class Schema extends Record(DEFAULTS) { } if (rule.first != null) { - const first = node.nodes.first() + const { kinds, types } = rule.first + const child = node.nodes.first() - if (rule.first.kinds != null && !rule.first.kinds.includes(first.kind)) { - return this.fail(FIRST_CHILD_KIND_INVALID, { ...ctx, child: first }) + if (child && kinds && !kinds.includes(child.kind)) { + return this.fail(FIRST_CHILD_KIND_INVALID, { ...ctx, child }) } - if (rule.first.types != null && !rule.first.types.includes(first.type)) { - return this.fail(FIRST_CHILD_TYPE_INVALID, { ...ctx, child: first }) + if (child && types && !types.includes(child.type)) { + return this.fail(FIRST_CHILD_TYPE_INVALID, { ...ctx, child }) } } if (rule.last != null) { - const last = node.nodes.last() + const { kinds, types } = rule.last + const child = node.nodes.last() - if (rule.last.kinds != null && !rule.last.kinds.includes(last.kind)) { - return this.fail(LAST_CHILD_KIND_INVALID, { ...ctx, child: last }) + if (child && kinds && !kinds.includes(child.kind)) { + return this.fail(LAST_CHILD_KIND_INVALID, { ...ctx, child }) } - if (rule.last.types != null && !rule.last.types.includes(last.type)) { - return this.fail(LAST_CHILD_TYPE_INVALID, { ...ctx, child: last }) + if (child && types && !types.includes(child.type)) { + return this.fail(LAST_CHILD_TYPE_INVALID, { ...ctx, child }) } }