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

91.67% Statements 11/12
63.64% Branches 7/11
100% Functions 4/4
90.91% Lines 10/11

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  4x 4x   4x           12x   12x         12x   48x       12x         11x 11x      
import { VirtualNode } from "../interfaces";
import nodeToVirtualNode from "./nodeToVirtualNode";
import parseFromString from "./parseFromString";
 
export default function markupToVirtualNode(
    markup: string,
    type: 'html' | 'xml' = 'xml',
    options: any = {}
): VirtualNode | string | null {
 
    let nodes = Array.from(parseFromString(markup, type));
 
    Iif (nodes === null) {
 
        return null;
    }
 
    Eif (options.excludeTextWithWhiteSpacesOnly === true) {
 
        nodes = nodes.filter(node => node instanceof HTMLElement ||
            node instanceof Text && !(/^\s*$/g.test((node as Text).textContent)))
    }
 
    return nodes.length > 1 ?
        {
            tag: null,
            attributes: null,
            children: nodes
                .map(n => nodeToVirtualNode(n, options))
                .filter(n => n !== null)
        } :
        nodeToVirtualNode(nodes[0], options);
}