All files / src/virtual-dom/dom createNode.ts

85.71% Statements 18/21
75% Branches 6/8
100% Functions 1/1
85% Lines 17/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    13x           13x   45x   4x               41x   41x         41x         41x   16x   25x               25x     22x   6x     22x   19x     8x   8x  
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;
 
    Iif ($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;
}