All files / renderer renderer.ts

93.55% Statements 29/31
70% Branches 14/20
100% Functions 4/4
93.33% Lines 28/30

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 13616x 16x 16x                                                                                           16x       78x           78x   12x       66x                   12x   12x   12x   36x               102x   85x   85x   85x       17x               17x   17x   17x       16x   16x   155x   155x   155x   38x   38x       155x          
import { createNode } from "./createNode";
import { NodePatcher, NodePatchingData } from "./NodePatcher";
import { patchNode } from "./patchNode";
 
 
 
// interface NodePatchingDataHolder {
 
//     _$patchingData: NodePatchingData
// }
 
 
 
 
 
 
 
 
 
 
// function appendChildren(markerNode: Node, newChildren: any) {
 
//     const { parentNode } = markerNode;
 
//     if (isPrimitive(newChildren)) {
 
//         const newChild = document.createTextNode(newChildren.toString());
 
//         parentNode.insertBefore(newChild, markerNode);
//     }
//     else {
 
//         const { length } = newChildren;
 
//         for (let i = 0; i < length; ++i) {
 
//             let newChild = newChildren[i];
 
//             if (isPrimitive(newChild)) {
 
//                 newChild = document.createTextNode(newChild.toString());
//             }
 
//             parentNode.insertBefore(newChild, markerNode);
//         }
//     }
// }
 
export function render(container: DocumentFragment | HTMLElement,
    oldPatchingData: NodePatchingData | NodePatchingData[] = null,
    newPatchingData: NodePatchingData | NodePatchingData[] = null) {
 
    Iif (oldPatchingData === null
        && newPatchingData === null) {
 
        return; // Nothing to patch
    }
 
    if (Array.isArray(newPatchingData)) {
 
        renderChildren(container, oldPatchingData as NodePatchingData[], newPatchingData);
    }
    else {
 
        renderNode(container, oldPatchingData as NodePatchingData, newPatchingData);
    }
}
 
function renderChildren(container: DocumentFragment | HTMLElement,
    oldPatchingData: NodePatchingData[],
    newPatchingData: NodePatchingData[]) {
 
    const {
        length
    } = newPatchingData;
 
    oldPatchingData = oldPatchingData || [];
 
    for (let i = 0; i < length; ++i) {
 
        renderNode(container, oldPatchingData[i], newPatchingData[i]);
    }
}
 
function renderNode(container: DocumentFragment | HTMLElement,
    oldPatchingData: NodePatchingData = null,
    newPatchingData: NodePatchingData = null) {
 
    if (oldPatchingData === null) { // Create a node from the new patching data and add the children to the container
 
        const node = createNode(newPatchingData);
 
        container.appendChild(node);
 
        newPatchingData.node = node;
 
        //newPatchingData.rules = rules;
    }
    else Iif (newPatchingData === null) { // Remove the children from the container
 
        throw new Error('render newPatchingData === null is not implemented')
    }
    else { // oldPatchingData !== null && newPatchingData !== null - Patch the old node/nodes
 
        let {
            node
        } = oldPatchingData;
 
        node = node !== undefined ? node : container;
 
        patchNode(node, newPatchingData);
    }
}
 
const cache = new Map<string, NodePatcher>();
 
export function html(strings: TemplateStringsArray, ...values: any): NodePatchingData {
 
    const key = strings.toString();
 
    let patcher = cache.get(key);
 
    if (patcher === undefined) {
 
        patcher = new NodePatcher(strings);
 
        cache.set(key, patcher);
    }
 
    // Return a new patching data with the shared patcher
    return {
        patcher,
        rules: null,
        values
    };
}