Skip to content

Commit 8e091b1

Browse files
committed
Make replaceNode atomic
1 parent 087548b commit 8e091b1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/morphlex.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,10 +670,15 @@ class Morph {
670670
#replaceNode(node: ChildNode, newNode: ChildNode): void {
671671
const parent = node.parentNode || document
672672
const insertionPoint = node
673-
if (this.#options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true) {
673+
// Check if both removal and addition are allowed before starting the replacement
674+
if (
675+
(this.#options.beforeNodeRemoved?.(node) ?? true) &&
676+
(this.#options.beforeNodeAdded?.(parent, newNode, insertionPoint) ?? true)
677+
) {
674678
moveBefore(parent, newNode, insertionPoint)
675679
this.#options.afterNodeAdded?.(newNode)
676-
this.#removeNode(node)
680+
node.remove()
681+
this.#options.afterNodeRemoved?.(node)
677682
}
678683
}
679684

0 commit comments

Comments
 (0)