All files / src/renderer/dom setAttribute.ts

94.44% Statements 17/18
94.74% Branches 18/19
100% Functions 1/1
94.44% Lines 17/18

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 5617x   255x           14x     14x   3x       11x         241x           241x   241x   4x   4x   237x   81x   81x       156x   5x     156x            
export default function setAttribute(node: HTMLElement, key: string, value: string) {
 
    if (value === undefined ||
        value === 'undefined' ||
        value === 'null' ||
        value === '' ||
        value === 'false') {
 
        node.removeAttribute(key);
 
        // Reset the value in any case
        if (key === 'value' && value === undefined) { // It fails with undefined for a text field value
 
            (node as any)[key] = '';
        }
        else {
 
            (node as any)[key] = value;
        }
    }
    else {
 
        Iif (value === 'true') {
 
            node.setAttribute(key, '');
        }
        else {
 
            const type = typeof value;
 
            if (type === 'function') {
 
                node.removeAttribute(key); // It is similar to an event. Do not show as attribute
 
                (node as any)[key] = value; // Bypass the stringification of the attribute
            }
            else if (type === 'object') {
 
                value = JSON.stringify(value);
 
                node.setAttribute(key, value);
            }
            else { // Any other type
 
                if (key === 'value') { // Set the value besides setting the attribute
 
                    (node as HTMLInputElement).value = value;
                }
 
                node.setAttribute(key, value);
            }
        }
    }
}