MCPcopy Index your code
hub / github.com/bigskysoftware/_hyperscript / morph

Method morph

www/js/_hyperscript.js:3534–3561  ·  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

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) {

Callers 7

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

Calls 3

_copyAttributesFunction · 0.70
_createIdMapsFunction · 0.70
_morphChildrenFunction · 0.70

Tested by

no test coverage detected