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

90% Statements 18/20
75% Branches 6/8
100% Functions 1/1
89.47% Lines 17/19

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    6x           6x   172x   153x             19x   19x         19x   5x   14x               14x     19x   2x           2x       19x   163x     19x   19x  
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) {
 
    if (typeof vnode === 'string') {
 
        return document.createTextNode(vnode);
    }
 
    const {
        tag,
        attributes,
        children
    } = vnode;
 
    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);
 
        //isSvg = true;
 
    } else {
 
        node = document.createElement(tag);
    }
 
    for (let name in Object(attributes)) {
 
        Iif (name in node) { // There is a setter defined in the element
 
            node[name] = attributes[name];
        }
        else {
 
            node.setAttribute(name, attributes[name]);
        }
    }
 
    for (let i = 0; i < children.length; ++i) {
 
        node.appendChild(createNode(children[i]));
    }
 
    vnode.$node = node;
 
    return node;
}