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