All files / src/renderer render.ts

84.75% Statements 50/59
76.32% Branches 29/38
100% Functions 2/2
83.93% Lines 47/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 1771x 1x                 1x   12x   8x       4x   2x   2x     2x   1x         2x               8x   8x     8x   8x   8x   8x   8x   4x       8x   8x   12x   12x   9x   9x         9x             3x         3x   3x     3x   3x           2x   2x   2x   2x   2x                 1x   1x           1x   1x   1x   1x   1x                 1x   1x                                                       8x   4x      
import { createNode } from "./createNode";
import { patchNode } from "./patchNode";
import { NodePatchingData } from "./NodePatcher";
 
/**
 * Renders a node
 * @param container The container where the node will be inserted if created 
 * @param node The node to render
 * @param patchingData The changes to apply to the node
 */
export function render(container: DocumentFragment | HTMLElement, node: Node = null, patchingData: NodePatchingData = null): void {
 
    if (Array.isArray(patchingData)) { // Collection of children
 
        patchChildren(container, node, patchingData);
    }
    else { // One child
 
        if (node === null) {
 
            const node = createNode(patchingData);
 
            container.appendChild(node);
 
            // If the node is a document fragment then transfer its patching data to the container
            if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) {
 
                (container as any)._$patchingData = (node as any)._$patchingData;
            }     
        }
        else {
 
            patchNode(/*container,*/ node, patchingData);
        }
 
    }
}
 
function patchChildren(container: DocumentFragment | HTMLElement, node: Node, patchingData: NodePatchingData[]) {
 
    const oldChildren = container.childNodes;
 
    let { length: oldChildrenCount } = oldChildren;
 
    // Map the keyed nodes from the old children nodes
    const keyedNodes = new Map<any, Node>();
 
    for (let i = 0; i < oldChildrenCount; ++i) {
 
        const oldChild = oldChildren[i] as HTMLElement;
 
        let key = oldChild.getAttribute?.('key') || null;
 
        if (key !== null) {
 
            keyedNodes.set(key, oldChild);
        }
    }
 
    const { length: patchingDataCount } = patchingData;
 
    for (let i = 0; i < patchingDataCount; ++i) {
 
        const oldChild = oldChildren[i] as HTMLElement;
 
        if (oldChild === undefined) {
 
            const node = createNode(patchingData[i]);
 
            Iif (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { // Nested children of a child
 
                throw new Error('Child cannot have nested children');
            }
 
            container.appendChild(node);
        }
        else { // oldChild !== undefined
 
            const {
                patcher,
                values
            } = patchingData[i];
 
            // Check for any keyed patching data
            const {
                keyIndex
            } = patcher;
 
            const valueKey = keyIndex !== undefined ? values[keyIndex].toString() : null;
 
            // Compare against a keyed node
            const oldChildKey = oldChild.getAttribute?.('key') || null;
 
            if (oldChildKey === valueKey) { // If the keys are the same patch the node with that patching data    
 
                const {
                    patcher: oldPatcher,
                    values: oldValues,
                    rules
                } = (oldChild as any)._$patchingData;
 
                Eif (oldPatcher === patcher) {
 
                    oldPatcher.patchNode(oldChild, rules, oldValues, values);
 
                    patchingData[i].rules = rules; // Transfer the compiled rules
 
                    (oldChild as any)._$patchingData = patchingData[i]; // Replace the old patching data with the new one
                }
                else {
 
                    throw new Error('Not implemented');
                }
            }
            else { // oldChildKey !== valueKey - Find the node that corresponds with the keyed patching data
 
                Eif (keyedNodes.has(valueKey)) { // Find an existing keyed node
 
                    const keyedNode = keyedNodes.get(valueKey);
 
                    const {
                        patcher: oldPatcher,
                        values: oldValues,
                        rules
                    } = (keyedNode as any)._$patchingData;
 
                    Eif (oldPatcher === patcher) {
 
                        oldPatcher.patchNode(keyedNode, rules, oldValues, values);
 
                        patchingData[i].rules = rules; // Transfer the compiled rules
 
                        (keyedNode as any)._$patchingData = patchingData[i]; // Replace the old patching data with the new one
                    }
                    else {
 
                        throw new Error('Not implemented');
                    }
 
                    const {
                        parentNode
                    } = keyedNode;
 
                    parentNode.replaceChild(keyedNode, oldChildren[i]);
                }
                else { // No keyed node found, set the new child
 
                    const {
                        patcher: oldPatcher,
                        values: oldValues,
                        rules
                    } = (oldChild as any)._$patchingData;
 
                    if (oldPatcher === patcher) {
 
                        oldPatcher.patchNode(oldChild, rules, oldValues, values);
 
                        patchingData[i].rules = rules; // Transfer the compiled rules
 
                        (oldChild as any)._$patchingData = patchingData[i]; // Replace the old patching data with the new one
                    }
                    else {
 
                        throw new Error('Not implemented');
                    }
                }
            }
        }
    }
 
    // Remove the extra nodes
    for (let i = oldChildrenCount - 1; i >= patchingDataCount; --i) {
 
        oldChildren[i].remove();
    }
 
}