* Morph oldNode to match content. * @param {Element} oldNode - The existing DOM element to morph * @param {string|Element|DocumentFragment} content - The new content * @param {MorphCallbacks} [callbacks] - Optional lifecycle callbacks
(oldNode, content, callbacks = {})
| 3532 | * @param {MorphCallbacks} [callbacks] - Optional lifecycle callbacks |
| 3533 | */ |
| 3534 | morph(oldNode, content, callbacks = {}) { |
| 3535 | var fragment; |
| 3536 | if (typeof content === "string") { |
| 3537 | var temp = document.createElement("template"); |
| 3538 | temp.innerHTML = content; |
| 3539 | fragment = temp.content; |
| 3540 | } else if (content instanceof DocumentFragment) { |
| 3541 | fragment = content; |
| 3542 | } else if (content instanceof Element) { |
| 3543 | fragment = document.createDocumentFragment(); |
| 3544 | fragment.append(content.cloneNode(true)); |
| 3545 | } else { |
| 3546 | throw new Error("morph requires an HTML string, element, or document fragment"); |
| 3547 | } |
| 3548 | var newRoot = fragment.firstElementChild; |
| 3549 | if (newRoot && !newRoot.nextElementSibling && newRoot.tagName === oldNode.tagName) { |
| 3550 | _copyAttributes(oldNode, newRoot); |
| 3551 | fragment = newRoot; |
| 3552 | } |
| 3553 | var { persistentIds, idMap } = _createIdMaps(oldNode, fragment); |
| 3554 | var pantry = document.createElement("div"); |
| 3555 | pantry.hidden = true; |
| 3556 | (document.body || oldNode.parentElement).after(pantry); |
| 3557 | var ctx = { target: oldNode, idMap, persistentIds, pantry, futureMatches: /* @__PURE__ */ new WeakSet(), callbacks }; |
| 3558 | _morphChildren(ctx, oldNode, fragment); |
| 3559 | callbacks.beforeNodeRemoved?.(pantry); |
| 3560 | pantry.remove(); |
| 3561 | } |
| 3562 | }; |
| 3563 | function _morphChildren(ctx, oldParent, newParent, insertionPoint = null, endPoint = null) { |
| 3564 | if (oldParent instanceof HTMLTemplateElement && newParent instanceof HTMLTemplateElement) { |
no test coverage detected