All files / renderer createNode.ts

3.33% Statements 1/30
0% Branches 0/7
0% Functions 0/3
3.57% Lines 1/28

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              2x                                                                                                                                                                                              
import { NodePatchingData, NodePatcherRule, 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[] {
 
    const compiledRules: CompiledNodePatcherRule[] = [];
 
    const {
        length
    } = rules;
 
    for (let i = 0; i < length; ++i) {
 
        const {
            type,
            path,
            name
        } = rules[i];
 
        compiledRules.push({
            type,
            name,
            node: findNode(node, path)
        });
    }
 
    return compiledRules;
}
 
function findNode(node: Node, path: number[]): HTMLElement | Comment | Text {
 
    let p = path;
 
    for (let i = 0; i < p.length; ++i) {
 
        const index = p[i];
 
        node = node.childNodes[index];
    }
 
    return node as HTMLElement | Comment | Text;
}