diff --git a/src/models/schema.js b/src/models/schema.js index 1b7967c5e..9d7d3abef 100644 --- a/src/models/schema.js +++ b/src/models/schema.js @@ -206,39 +206,6 @@ class Schema extends new Record(DEFAULTS) { } } - /** - * Normalize the node using a schema, by pushing operations to a transform. - * "prevNode" can be used to prevent iterating over all children. - * - * @param {Transform} transform - * @param {Node} node - * @param {Node} prevNode? - * @return {Transform} - */ - - __normalize(transform, node, prevNode) { - if (prevNode === node) { - return transform - } - - // Normalize children - if (node.nodes) { - node.nodes.forEach(child => { - const prevChild = prevNode ? prevNode.getChild(child.key) : null - this.__normalize(transform, child, prevChild) - }) - } - - // Normalize the node itself - let failure - if (failure = this.__validate(node)) { - const { value, rule } = failure - rule.normalize(transform, node, value) - } - - return transform - } - } /** diff --git a/src/models/state.js b/src/models/state.js index c52dc85ae..25153226d 100644 --- a/src/models/state.js +++ b/src/models/state.js @@ -414,23 +414,6 @@ class State extends new Record(DEFAULTS) { : this.document.getTextsAtRange(this.selection) } - /** - * Normalize a state against a `schema`. - * - * @param {Schema} schema - * @return {State} - */ - - normalize(schema) { - const state = this - const { document, selection } = this - let transform = this.transform() - - transform = schema.__normalize(transform, document, null) - - return transform.apply({ save: false }) - } - /** * Return a new `Transform` with the current state as a starting point. * diff --git a/src/transforms/normalize.js b/src/transforms/normalize.js index 2bb6f786c..c8ee6322b 100644 --- a/src/transforms/normalize.js +++ b/src/transforms/normalize.js @@ -1,6 +1,40 @@ import Schema from '../models/schema' import { default as defaultSchema } from '../plugins/schema' + +/** +* Normalize a node using a schema, by pushing operations to a transform. +* "prevNode" can be used to prevent iterating over all children. +* +* @param {Transform} transform +* @param {Node} node +* @param {Node} prevNode? +* @return {Transform} + */ + +function normalizeNode(transform, schema, node, prevNode) { + if (prevNode === node) { + return transform + } + + // Normalize children + if (node.nodes) { + transform = node.nodes.reduce((t, child) => { + const prevChild = prevNode ? prevNode.getChild(child.key) : null + return normalizeNode(transform, schema, child, prevChild) + }, transform) + } + + // Normalize the node itself + let failure + if (failure = schema.__validate(node)) { + const { value, rule } = failure + transform = rule.normalize(transform, node, value) + } + + return transform +} + /** * Normalize the state with a schema. * @@ -12,7 +46,7 @@ import { default as defaultSchema } from '../plugins/schema' export function normalizeWith(transform, schema) { const { state } = transform const { document } = state - return schema.__normalize(transform, document, null) + return normalizeNode(transform, schema, document, null) } /** @@ -25,33 +59,6 @@ export function normalizeWith(transform, schema) { export function normalize(transform) { return transform.normalizeWith(defaultSchema) - /* - let { state } = transform - let { document, selection } = state - let failure - - // Normalize all of the document's nodes. - document.filterDescendantsDeep((node) => { - if (failure = node.validate(SCHEMA)) { - const { value, rule } = failure - rule.normalize(transform, node, value) - } - }) - - // Normalize the document itself. - if (failure = document.validate(SCHEMA)) { - const { value, rule } = failure - rule.normalize(transform, document, value) - } - - // Normalize the selection. - // TODO: turn this into schema rules. - state = transform.state - document = state.document - let nextSelection = selection.normalize(document) - if (!selection.equals(nextSelection)) transform.setSelection(selection) - return transform - */ } /**