All files / virtual-dom/dom createNode.ts

90.48% Statements 19/21
87.5% Branches 7/8
100% Functions 1/1
90% Lines 18/20

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    10x           10x   94x   42x               52x   52x   16x     36x         36x   15x   21x               21x     36x   5x     36x   58x     36x   36x  
import { VirtualNode } from "../interfaces";
 
const svgElements = ['svg', 'use', 'symbol', 'path', 'g', 'defs', 'title'];
 
/**
 * Creates the DOM element from the virtual node
 * @param vnode The virtual node to create the element from
 */
export default function createNode(vnode: VirtualNode | string): Node {
 
    if (typeof vnode === 'string') {
 
        return document.createTextNode(vnode);
    }
 
    const {
        tag,
        attributes,
        children,
        $node
    } = vnode;
 
    if ($node !== undefined) { // If there is a node attached to the virtual node, return it
 
        return $node;
    }
 
    let node = null;
 
    //let isSvg = false;
 
    // Create the element
    if (tag === null) { // Fragment node
 
        node = document.createDocumentFragment();
    }
    else Iif (svgElements.includes(tag) === true) {
 
        node = document.createElementNS('http://www.w3.org/2000/svg', tag);
 
        node.isSvg = true;
 
    } else {
 
        node = document.createElement(tag);
    }
 
    for (let name in Object(attributes)) {
 
        node.setAttribute(name, attributes[name]);
    }
 
    for (let i = 0; i < children.length; ++i) {
 
        node.appendChild(createNode(children[i]));
    }
 
    vnode.$node = node;
 
    return node;
}