File size: 977 Bytes
3f22414
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
 * Idiomorph wrapper — uses the global Idiomorph loaded via CDN.
 * Prevents nested custom elements from being torn down and re-created.
 */

export function morph(component, html) {
    Idiomorph.morph(component, html, {
        morphStyle: 'innerHTML',
        ignoreActiveValue: true,
        callbacks: {
            beforeNodeMorphed(oldNode, newNode) {
                if (isNestedCustomElement(oldNode, component)) {
                    syncAttributes(oldNode, newNode);
                    return false;
                }
            }
        }
    });
}

function isNestedCustomElement(node, owner) {
    return node instanceof Element && node.tagName.includes('-') && node !== owner;
}

function syncAttributes(oldNode, newNode) {
    if (!(newNode instanceof Element)) return;
    for (const attr of newNode.attributes) {
        if (oldNode.getAttribute(attr.name) !== attr.value) {
            oldNode.setAttribute(attr.name, attr.value);
        }
    }
}