All files / src/renderer createNode.ts

100% Statements 23/23
100% Branches 7/7
100% Functions 4/4
100% Lines 22/22

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                19x         36x   36x       36x   36x   36x   32x     32x     32x       4x     4x   4x         3x       36x     36x   36x   36x                     36x   75x                               75x   119x     75x  
import { NodePatcherRule } from "./createNodePatcherRules";
import { NodePatchingData, CompiledNodePatcherRule } from "./NodePatcher";
 
/**
 * Creates a node according to the patching data of the node
 * @param patchingData 
 * @returns 
 */
export function createNode(parentNode: Node, patchingData: NodePatchingData): Node {
 
    const {
        patcher,
        values
    } = patchingData;
 
    const doc = patcher.template.content.cloneNode(/*deep*/true); // The content of the template is a document fragment
 
    const {
        childNodes
    } = doc;
 
    let node: Node = undefined;
 
    if (patcher.isSingleElement) { // Node is single HTMLElement
 
        node = childNodes[0];
 
        // Set the node of the patching data
        patchingData.node = node;
 
        // Attach the patching data to the node
        (node as any)._$patchingData = patchingData;
    }
    else { // Node is a collection of nodes
 
        node = doc;
 
        // Set the node of the patching data
        patchingData.node = parentNode;
 
        if ((parentNode as any)._$patchingData === undefined &&
            (!(parentNode instanceof DocumentFragment) ||
                parentNode instanceof ShadowRoot)) {
 
            // Attach the patching data to the node if there is none attached
            (parentNode as any)._$patchingData = patchingData;
        }
    }
 
    const rules = compileRules(doc, patcher.rules);
 
    // Update the rules of the patching data
    patchingData.rules = rules;
 
    patcher.firstPatch(doc, rules, values);
 
    return node;
}
 
/**
 * Creates a compiled rule by replacing the path with the reference to the node the rule acts upon
 * @param node The content node of the template
 * @param rules The rules to compile
 * @returns the compiled rules
 */
function compileRules(node: Node, rules: NodePatcherRule[]): CompiledNodePatcherRule[] {
 
    return rules.map(r => {
 
        return {
            node: findNode(node, r.path),
            type: r.type,
            name: r.name
        };
    });
}
 
/**
 * Finds the child node following the path
 * @param node The parent node
 * @param path The path to the child node
 * @returns The child node
 */
function findNode(node: Node, path: number[]): HTMLElement | Comment | Text {
 
    for (let i = 0; i < path.length; ++i) {
 
        node = node.childNodes[path[i]];
    }
 
    return node as HTMLElement | Comment | Text;
}