1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-22 23:12:52 +02:00

Recursive safety fixed, and throw error instead

This commit is contained in:
Soreine
2016-10-27 17:45:11 +02:00
parent 4fa097ab0b
commit ddb8522584

View File

@@ -52,27 +52,39 @@ function _normalizeChildrenWith(transform, schema, node) {
*/
function _normalizeNodeWith(transform, schema, node) {
const failure = schema.__validate(node)
let recursiveCount = 0
// Auxiliary function, called recursively, with a maximum calls safety net.
function _recur(_transform, _schema, _node) {
const failure = _schema.__validate(_node)
// Node is valid?
if (!failure) {
return transform
return _transform
}
const { value, rule } = failure
// Normalize and get the new state
transform = rule.normalize(transform, node, value)
_transform = rule.normalize(_transform, _node, value)
// Search for the updated node in the new state
const newNode = _refreshNode(transform, node)
const newNode = _refreshNode(_transform, _node)
// Node no longer exist, go back to normalize parents
if (!newNode) {
return transform
return _transform
}
return _normalizeNodeWith(transform, schema, newNode)
recursiveCount++
if (recursiveCount > MAX_CALLS) {
throw new Error('Unexpected number of successive normalizations. Aborting.')
}
return _recur(_transform, _schema, _node)
}
return _recur(transform, schema, node)
}
/**
@@ -85,15 +97,6 @@ function _normalizeNodeWith(transform, schema, node) {
*/
export function normalizeNodeWith(transform, schema, node) {
let recursiveCount = 0
// Auxiliary function, called recursively, with a maximum calls safety net.
function _recur() {
recursiveCount++
if (recursiveCount > MAX_CALLS) {
warning('Unexpected number of successive normalizations. Aborting.')
return transform
}
// Iterate over its children
transform = _normalizeChildrenWith(transform, schema, node)
@@ -106,9 +109,6 @@ export function normalizeNodeWith(transform, schema, node) {
return transform
}
return _recur(transform, schema, node)
}
/**
* Normalize a node its parents using a schema.
*