diff --git a/src/models/schema.js b/src/models/schema.js index 9d7d3abef..e81e38626 100644 --- a/src/models/schema.js +++ b/src/models/schema.js @@ -1,102 +1,10 @@ import React from 'react' -import includes from 'lodash/includes' import isReactComponent from '../utils/is-react-component' import typeOf from 'type-of' import memoize from '../utils/memoize' import { Record } from 'immutable' -/** - * Checks that the schema can perform, ordered by performance. - * - * @type {Object} - */ - -const CHECKS = { - - kind(object, value) { - if (object.kind != value) return object.kind - }, - - type(object, value) { - if (object.type != value) return object.type - }, - - isVoid(object, value) { - if (object.isVoid != value) return object.isVoid - }, - - minChildren(object, value) { - if (object.nodes.size < value) return object.nodes.size - }, - - maxChildren(object, value) { - if (object.nodes.size > value) return object.nodes.size - }, - - kinds(object, value) { - if (!includes(value, object.kind)) return object.kind - }, - - types(object, value) { - if (!includes(value, object.type)) return object.type - }, - - minLength(object, value) { - const { length } = object - if (length < value) return length - }, - - maxLength(object, value) { - const { length } = object - if (length > value) return length - }, - - text(object, value) { - const { text } = object - switch (typeOf(value)) { - case 'function': if (value(text)) return text - case 'regexp': if (!text.match(value)) return text - default: if (text != value) return text - } - }, - - anyOf(object, value) { - const { nodes } = object - if (!nodes) return - const invalids = nodes.filterNot((child) => { - return value.some(match => match(child)) - }) - - if (invalids.size) return invalids - }, - - noneOf(object, value) { - const { nodes } = object - if (!nodes) return - const invalids = nodes.filterNot((child) => { - return value.every(match => !match(child)) - }) - - if (invalids.size) return invalids - }, - - exactlyOf(object, value) { - const { nodes } = object - if (!nodes) return - if (nodes.size != value.length) return nodes - - const invalids = nodes.filterNot((child, i) => { - const match = value[i] - if (!match) return false - return match(child) - }) - - if (invalids.size) return invalids - }, - -} - /** * Default properties. * @@ -137,6 +45,15 @@ class Schema extends new Record(DEFAULTS) { return 'schema' } + /** + * Return true if one rule can normalize the document + */ + + get isNormalization() { + const { rules } = this + return rules.some(rule => rule.validate) + } + /** * Return the renderer for an `object`. * diff --git a/src/plugins/core.js b/src/plugins/core.js index b2a65082c..d8fd690f4 100644 --- a/src/plugins/core.js +++ b/src/plugins/core.js @@ -742,8 +742,7 @@ function Plugin(options = {}) { const schema = { rules: [ BLOCK_RENDER_RULE, - INLINE_RENDER_RULE, - ...defaultSchema.rules + INLINE_RENDER_RULE ] } diff --git a/src/transforms/normalize.js b/src/transforms/normalize.js index 411e25d39..a8721f8db 100644 --- a/src/transforms/normalize.js +++ b/src/transforms/normalize.js @@ -137,6 +137,11 @@ export function normalizeWith(transform, schema) { const { state } = transform const { document } = state + // Schema was not rule to edit the document + if (!schema.isNormalization) { + return transform + } + return transform.normalizeNodeWith(schema, document) }