···2323 }
2424}
2525// This is where we actually morph the nodes. The `morph` function exists to set up the `idMap`.
2626-function morphNodes(node, guide, idMap, insertBefore, parent) {
2727- // TODO: We should extract this into a separate function.
2828- if (parent && insertBefore && insertBefore !== node) parent.insertBefore(guide, insertBefore);
2626+function morphNodes(node, guide, idMap) {
2927 if (isElement(node) && isElement(guide) && node.tagName === guide.tagName) {
3028 // We need to check if the element is an input, option, or textarea here, because they have
3129 // special attributes not covered by the isEqualNode check.
···9189 while (currentNode) {
9290 if (isElement(currentNode)) {
9391 if (currentNode.id === guide.id) {
9494- return morphNodes(currentNode, guide, idMap, child, parent);
9292+ parent.insertBefore(currentNode, child);
9393+ return morphNodes(currentNode, guide, idMap);
9594 } else if (currentNode.id !== "") {
9695 const currentIdSet = idMap.get(currentNode);
9796 if (currentIdSet && guideSetArray.some((it) => currentIdSet.has(it))) {
9898- return morphNodes(currentNode, guide, idMap, child, parent);
9797+ parent.insertBefore(currentNode, child);
9898+ return morphNodes(currentNode, guide, idMap);
9999 }
100100 } else if (!nextMatchByTagName && currentNode.tagName === guide.tagName) {
101101 nextMatchByTagName = currentNode;
···103103 }
104104 currentNode = currentNode.nextSibling;
105105 }
106106- if (nextMatchByTagName) morphNodes(nextMatchByTagName, guide, idMap, child, parent);
107107- else child.replaceWith(guide.cloneNode(true));
106106+ if (nextMatchByTagName) {
107107+ parent.insertBefore(nextMatchByTagName, child);
108108+ morphNodes(nextMatchByTagName, guide, idMap);
109109+ } else child.replaceWith(guide.cloneNode(true));
108110}
109111// We cannot use `instanceof` when nodes might be from different documents,
110112// so we use type guards instead. This keeps TypeScript happy, while doing
+9-8
src/morphlex.ts
···3434}
35353636// This is where we actually morph the nodes. The `morph` function exists to set up the `idMap`.
3737-function morphNodes(node: ChildNode, guide: ChildNode, idMap: IdMap, insertBefore?: Node, parent?: Node): void {
3838- // TODO: We should extract this into a separate function.
3939- if (parent && insertBefore && insertBefore !== node) parent.insertBefore(guide, insertBefore);
4040-3737+function morphNodes(node: ChildNode, guide: ChildNode, idMap: IdMap): void {
4138 if (isElement(node) && isElement(guide) && node.tagName === guide.tagName) {
4239 // We need to check if the element is an input, option, or textarea here, because they have
4340 // special attributes not covered by the isEqualNode check.
···117114 while (currentNode) {
118115 if (isElement(currentNode)) {
119116 if (currentNode.id === guide.id) {
120120- return morphNodes(currentNode, guide, idMap, child, parent);
117117+ parent.insertBefore(currentNode, child);
118118+ return morphNodes(currentNode, guide, idMap);
121119 } else if (currentNode.id !== "") {
122120 const currentIdSet = idMap.get(currentNode);
123121124122 if (currentIdSet && guideSetArray.some((it) => currentIdSet.has(it))) {
125125- return morphNodes(currentNode, guide, idMap, child, parent);
123123+ parent.insertBefore(currentNode, child);
124124+ return morphNodes(currentNode, guide, idMap);
126125 }
127126 } else if (!nextMatchByTagName && currentNode.tagName === guide.tagName) {
128127 nextMatchByTagName = currentNode;
···132131 currentNode = currentNode.nextSibling;
133132 }
134133135135- if (nextMatchByTagName) morphNodes(nextMatchByTagName, guide, idMap, child, parent);
136136- else child.replaceWith(guide.cloneNode(true));
134134+ if (nextMatchByTagName) {
135135+ parent.insertBefore(nextMatchByTagName, child);
136136+ morphNodes(nextMatchByTagName, guide, idMap);
137137+ } else child.replaceWith(guide.cloneNode(true));
137138}
138139139140// We cannot use `instanceof` when nodes might be from different documents,