MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / morph

Method morph

www/js/_hyperscript-max.js:3533–3560  ·  view source on GitHub ↗

* 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 = {})

Source from the content-addressed store, hash-verified

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

Callers 4

morphMethod · 0.45
resolveMethod · 0.45
stampFunction · 0.45
_stampTemplateMethod · 0.45

Calls 3

_copyAttributesFunction · 0.70
_createIdMapsFunction · 0.70
_morphChildrenFunction · 0.70

Tested by

no test coverage detected