Skip to content

Commit 13c7b36

Browse files
authored
Harden split_node operation (#6043)
1 parent 20a1a93 commit 13c7b36

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

  • packages/slate/src/interfaces/transforms

packages/slate/src/interfaces/transforms/general.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ export const GeneralTransforms: GeneralTransforms = {
256256
throw new Error(`Cannot set the "${key}" property of nodes!`)
257257
}
258258

259-
const value = Object.hasOwn(newProperties, key)
260-
? newProperties[<keyof Node>key]
261-
: undefined
259+
const value = newProperties[<keyof Node>key]
262260

263261
// Make sure we're not setting `then` to a function, since this will
264262
// cause the node to be treated as a Promise-like object, which can
@@ -374,7 +372,6 @@ export const GeneralTransforms: GeneralTransforms = {
374372
text: before,
375373
}
376374
nextNode = {
377-
...(properties as Partial<Text>),
378375
text: after,
379376
}
380377
} else {
@@ -385,11 +382,32 @@ export const GeneralTransforms: GeneralTransforms = {
385382
children: before,
386383
}
387384
nextNode = {
388-
...(properties as Partial<Element>),
389385
children: after,
390386
}
391387
}
392388

389+
for (const key in properties) {
390+
if (NON_SETTABLE_NODE_PROPERTIES.includes(key)) {
391+
throw new Error(`Cannot set the "${key}" property of nodes!`)
392+
}
393+
394+
const value = properties[<keyof Node>key]
395+
396+
// Make sure we're not setting `then` to a function, since this will
397+
// cause the node to be treated as a Promise-like object, which can
398+
// cause unexpected behaviour when returning the node from async
399+
// functions.
400+
if (key === 'then' && typeof value === 'function') {
401+
throw new Error(
402+
'Cannot set the "then" property of a node to a function'
403+
)
404+
}
405+
406+
if (value != null) {
407+
nextNode[<keyof Node>key] = value
408+
}
409+
}
410+
393411
return replaceChildren(children, index, 1, newNode, nextNode)
394412
})
395413

0 commit comments

Comments
 (0)