···2323 }
2424 }
2525}
2626-// This is where we actually morph the nodes. The `morph` function exists to set up the `idMap`.
2626+// This is where we actually morph the nodes. The `morph` function (above) exists only to set up the `idMap`.
2727function morphNode(node, ref, context) {
2828 const writableRef = ref;
2929 if (!(context.beforeNodeMorphed?.(node, writableRef) ?? true)) return;
···3535 for (const child of node.children) {
3636 const key = child.outerHTML;
3737 const refChild = refChildNodes.get(key);
3838- refChild ? refChildNodes.delete(key) : child.remove(); // TODO add callback
3838+ refChild ? refChildNodes.delete(key) : removeNode(child, context);
3939 }
4040 for (const refChild of refChildNodes.values()) appendChild(node, refChild.cloneNode(true), context);
4141 } else if (node.hasChildNodes() || ref.hasChildNodes()) morphChildNodes(node, ref, context);
···8989 if (child && refChild) morphChildNode(child, refChild, element, context);
9090 else if (refChild) {
9191 appendChild(element, refChild.cloneNode(true), context);
9292- } else if (child && (context.beforeNodeRemoved?.(child) ?? true)) {
9393- child.remove();
9494- context.afterNodeRemoved?.(child);
9595- }
9292+ } else if (child) removeNode(child, context);
9693 }
9794 // Remove any excess child nodes from the main element. This is separate because
9895 // the loop above might modify the length of the main element’s child nodes.
9999- while (element.childNodes.length > ref.childNodes.length) element.lastChild?.remove();
9696+ while (element.childNodes.length > ref.childNodes.length) {
9797+ const child = element.lastChild;
9898+ if (child) removeNode(child, context);
9999+ }
100100}
101101function updateProperty(element, propertyName, newValue, context) {
102102 const previousValue = element[propertyName];
···151151 if (context.beforeNodeAdded?.(newNode, node) ?? true) {
152152 node.appendChild(newNode);
153153 context.afterNodeAdded?.(newNode);
154154+ }
155155+}
156156+function removeNode(node, context) {
157157+ if (context.beforeNodeRemoved?.(node) ?? true) {
158158+ node.remove();
159159+ context.afterNodeRemoved?.(node);
154160 }
155161}
156162function isText(node) {
+14-7
src/morphlex.ts
···8383 }
8484}
85858686-// This is where we actually morph the nodes. The `morph` function exists to set up the `idMap`.
8686+// This is where we actually morph the nodes. The `morph` function (above) exists only to set up the `idMap`.
8787function morphNode(node: ChildNode, ref: ReadonlyNode<ChildNode>, context: Context): void {
8888 const writableRef = ref as ChildNode;
8989 if (!(context.beforeNodeMorphed?.(node, writableRef) ?? true)) return;
···9696 for (const child of node.children) {
9797 const key = child.outerHTML;
9898 const refChild = refChildNodes.get(key);
9999- refChild ? refChildNodes.delete(key) : child.remove(); // TODO add callback
9999+ refChild ? refChildNodes.delete(key) : removeNode(child, context);
100100 }
101101 for (const refChild of refChildNodes.values()) appendChild(node, refChild.cloneNode(true), context);
102102 } else if (node.hasChildNodes() || ref.hasChildNodes()) morphChildNodes(node, ref, context);
···158158 if (child && refChild) morphChildNode(child, refChild, element, context);
159159 else if (refChild) {
160160 appendChild(element, refChild.cloneNode(true), context);
161161- } else if (child && (context.beforeNodeRemoved?.(child) ?? true)) {
162162- child.remove();
163163- context.afterNodeRemoved?.(child);
164164- }
161161+ } else if (child) removeNode(child, context);
165162 }
166163167164 // Remove any excess child nodes from the main element. This is separate because
168165 // the loop above might modify the length of the main element’s child nodes.
169169- while (element.childNodes.length > ref.childNodes.length) element.lastChild?.remove();
166166+ while (element.childNodes.length > ref.childNodes.length) {
167167+ const child = element.lastChild;
168168+ if (child) removeNode(child, context);
169169+ }
170170}
171171172172function updateProperty<N extends Node, P extends keyof N>(element: N, propertyName: P, newValue: N[P], context: Context): void {
···232232 if (context.beforeNodeAdded?.(newNode, node) ?? true) {
233233 node.appendChild(newNode);
234234 context.afterNodeAdded?.(newNode);
235235+ }
236236+}
237237+238238+function removeNode(node: ChildNode, context: Context): void {
239239+ if (context.beforeNodeRemoved?.(node) ?? true) {
240240+ node.remove();
241241+ context.afterNodeRemoved?.(node);
235242 }
236243}
237244