mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-23 07:22:55 +02:00
Recursive safety fixed, and throw error instead
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
// Auxiliary function, called recursively, with a maximum calls safety net.
|
||||||
|
function _recur(_transform, _schema, _node) {
|
||||||
|
const failure = _schema.__validate(_node)
|
||||||
|
|
||||||
// Node is valid?
|
// Node is valid?
|
||||||
if (!failure) {
|
if (!failure) {
|
||||||
return transform
|
return _transform
|
||||||
}
|
}
|
||||||
|
|
||||||
const { value, rule } = failure
|
const { value, rule } = failure
|
||||||
|
|
||||||
// Normalize and get the new state
|
// 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
|
// 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
|
// Node no longer exist, go back to normalize parents
|
||||||
if (!newNode) {
|
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) {
|
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
|
// Iterate over its children
|
||||||
transform = _normalizeChildrenWith(transform, schema, node)
|
transform = _normalizeChildrenWith(transform, schema, node)
|
||||||
|
|
||||||
@@ -104,9 +107,6 @@ export function normalizeNodeWith(transform, schema, node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return transform
|
return transform
|
||||||
}
|
|
||||||
|
|
||||||
return _recur(transform, schema, node)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user