All files / src/virtual-dom/nodes ElementNode.ts

85.45% Statements 47/55
68.97% Branches 20/29
100% Functions 7/7
84.91% Lines 45/53

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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 1833x     3x   3x             21x         21x         21x           8x                         15x   15x     15x       15x               15x     15x   15x   15x   9x     15x                           7x   7x   7x   7x   1x     7x   1x                   7x         7x   7x   7x   7x   9x   9x               9x   9x   2x   2x         7x           17x                 17x           17x             7x   7x   7x   9x   9x     7x    
import patchChildren from "./helpers/patchChildren";
import TextNode from "./TextNode";
 
const svgElements = ['svg', 'use', 'symbol', 'path', 'g', 'defs', 'title'];
 
export default class ElementNode {
 
    constructor(
 
        /**
         * The name of the element
         */
        public tag: string,
 
        /**
         * The attributes of the element
         */
        public attributes: Record<PropertyKey, any> | null,
 
        /**
         * The children of the element or the text
         */
        public children: (ElementNode | TextNode)[]
 
    ) { }
 
    get key(): string {
 
        return this.attributes?.key;
    }
 
    /**
     * Creates a DOM node from this virtual node
     * @returns The text node
     */
    createDom(): HTMLElement {
 
        const {
            tag,
            attributes,
            children,
        } = this;
 
        let node = null;
 
        // Create the element
        Iif (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 key in Object(attributes)) {
 
            setAttribute(node, key, attributes[key]);  
        }
        for (let i = 0; i < children.length; ++i) {
 
            node.appendChild(children[i].createDom());
        }
 
        return node;
    }
 
    /**
     * Patches the DOM node according to the internal state
     * @param value 
     * @returns 
     */
    patchDom(node: HTMLElement): boolean {
 
        const {
            tag,
            attributes,
            children,
        } = this;
 
        let updated = false;
 
        Eif (tag.toUpperCase() === node.tagName) { // Check if the attributes and children changed
 
            if (this._patchAttributes(node, attributes || {}) === true) {
 
                updated = true;
            }
 
            if (patchChildren(node, children) === true) {
 
                updated = true;
            }
 
        }
        else { // Different tags, replace the element
 
            throw `patchDom not implemented for different tags. vnode: ${tag.toUpperCase()}, node: ${node.tagName}`;
            // return vnode.$node = node = createNode(vnode);
        }
 
        return updated;
    }
 
    private _patchAttributes(node: HTMLElement, attributes: Record<PropertyKey, any> = {}): boolean {
 
        let updated = false;
 
        const nodeAttributes = toRecord(node.attributes);
 
        const mergedAttributes = { ...nodeAttributes, ...attributes};
 
        for (const key of Object.keys(mergedAttributes)) {
 
            const value = attributes[key];
 
            Iif (value === undefined) { // It was removed in the new virtual node
 
                node.removeAttribute(key);
 
                updated = true;
            }
            else {
 
                const oldValue = nodeAttributes[key];
 
                if (value !== oldValue) {
 
                    setAttribute(node, key, value);
 
                    updated = true;
                }
            }
        }
 
        return updated;
    }
}
 
function setAttribute(node: HTMLElement, key: string, value: string) {
 
    Iif (value === 'undefined' ||
        value === 'null' ||
        value === '' ||
        value === 'false') {
 
        (node as HTMLElement).removeAttribute(key);
    }
    else {
 
        Iif (value === 'true') {
 
            (node as HTMLElement).setAttribute(key, '');
        }
        else {
 
            (node as HTMLElement).setAttribute(key, value);
        }
    }
}
 
function toRecord(attributes: NamedNodeMap): Record<PropertyKey, string> {
 
    const record: Record<PropertyKey, string> = {};
 
    const length = attributes.length;
 
    for (let i = 0; i < length; ++i) {
 
        const attribute = attributes[i];
 
        record[attribute.name] = attribute.value;
    }
 
    return record;
}