All files / Scripts/ui domUtils.ts

100% Statements 67/67
100% Branches 34/34
100% Functions 28/28
100% Lines 58/58

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254        2x             107x                 21x               5x 5x                 28x 28x                 5x 5x               6x 6x               6x 6x               3x 6x                   17x 17x 1x   16x                   4x 4x 4x                   4x 4x 3x                     3x 3x 2x                       5x 5x 4x             2x             5x 5x 5x 5x 13x                   4x 4x 4x 4x 5x                       6x 6x 9x 9x                   3x 3x                 3x 3x                 6x 6x                 5x 5x                 23x 23x                   12x 12x      
/**
 * DOM utility class for HTML manipulation
 * Provides a clean API for common DOM operations with null safety
 */
export class DomUtils {
    /**
     * Get a single element by CSS selector
     * @param selector CSS selector string
     * @returns HTMLElement or null if not found
     */
    static getElement(selector: string): HTMLElement | null {
        return document.querySelector(selector) as HTMLElement;
    }
 
    /**
     * Get multiple elements by CSS selector
     * @param selector CSS selector string
     * @returns NodeList of HTMLElements
     */
    static getElements(selector: string): NodeListOf<HTMLElement> {
        return document.querySelectorAll(selector) as NodeListOf<HTMLElement>;
    }
 
    /**
     * Clear the innerHTML of an element
     * @param selector CSS selector string
     */
    static clearElement(selector: string): void {
        const element = this.getElement(selector);
        if (element) element.innerHTML = "";
    }
 
    /**
     * Set the text content of an element
     * @param selector CSS selector string
     * @param text Text content to set
     */
    static setText(selector: string, text: string): void {
        const element = this.getElement(selector);
        if (element) element.textContent = text;
    }
 
    /**
     * Get the text content of an element
     * @param selector CSS selector string
     * @returns The text content or empty string if not found
     */
    static getText(selector: string): string {
        const element = this.getElement(selector);
        return element ? element.textContent || "" : "";
    }
 
    /**
     * Show an element by setting display to block
     * @param selector CSS selector string
     */
    static showElement(selector: string): void {
        const element = this.getElement(selector);
        if (element) element.style.display = "block";
    }
 
    /**
     * Hide an element by setting display to none
     * @param selector CSS selector string
     */
    static hideElement(selector: string): void {
        const element = this.getElement(selector);
        if (element) element.style.display = "none";
    }
 
    /**
     * Hide all elements matching selector by setting display to none
     * @param selector CSS selector string
     */
    static hideAllElements(selector: string): void {
        const elements = this.getElements(selector);
        elements.forEach(element => element.style.display = "none");
    }
 
    /**
     * Clone a template element's content
     * @param templateId ID of the template element
     * @returns DocumentFragment containing cloned content
     * @throws Error if template not found
     */
    static cloneTemplate(templateId: string): DocumentFragment {
        const template = document.getElementById(templateId) as HTMLTemplateElement;
        if (!template) {
            throw new Error(`Template with id "${templateId}" not found`);
        }
        return template.content.cloneNode(true) as DocumentFragment;
    }
 
    /**
     * Clone a template and append it to a parent element
     * @param templateId ID of the template element
     * @param parent Parent element to append to
     * @returns DocumentFragment that was appended
     */
    static appendTemplate(templateId: string, parent: HTMLElement): DocumentFragment {
        const clone = this.cloneTemplate(templateId);
        parent.appendChild(clone);
        return clone;
    }
 
    /**
     * Set text content on an element within a DocumentFragment
     * @param clone DocumentFragment to search within
     * @param selector CSS selector string
     * @param text Text content to set
     */
    static setTemplateText(clone: DocumentFragment, selector: string, text: string): void {
        const element = clone.querySelector(selector) as HTMLElement;
        if (element) {
            element.textContent = text;
        }
    }
 
    /**
     * Set HTML content on an element within a DocumentFragment
     * @param clone DocumentFragment to search within
     * @param selector CSS selector string
     * @param html HTML content to set
     */
    static setTemplateHTML(clone: DocumentFragment, selector: string, html: string): void {
        const element = clone.querySelector(selector) as HTMLElement;
        if (element) {
            element.innerHTML = html;
        }
    }
 
    /**
     * Set an attribute on an element within a DocumentFragment
     * @param clone DocumentFragment to search within
     * @param selector CSS selector string
     * @param attribute Attribute name
     * @param value Attribute value
     */
    static setTemplateAttribute(clone: DocumentFragment, selector: string, attribute: string, value: string): void {
        const element = clone.querySelector(selector) as HTMLElement;
        if (element) {
            element.setAttribute(attribute, value);
        }
    }
 
    /**
     * Common focusable element selector
     */
    static readonly focusableElements = "a, button, input, textarea, select, [tabindex]:not([tabindex=\"-1\"])";
 
    /**
     * Make elements non-tabbable by setting tabindex to -1
     * @param selector CSS selector string
     */
    static makeFocusableElementsNonTabbable(selector: string): void {
        const elements = this.getElements(selector);
        elements.forEach(container => {
            const focusableElements = container.querySelectorAll(this.focusableElements);
            focusableElements.forEach((el) => {
                (el as HTMLElement).setAttribute("tabindex", "-1");
            });
        });
    }
 
    /**
     * Restore tabbing by removing tabindex=-1 from elements
     * @param selector CSS selector string
     */
    static restoreFocusableElements(selector: string): void {
        const elements = this.getElements(selector);
        elements.forEach(container => {
            const focusableElements = container.querySelectorAll("[tabindex=\"-1\"]");
            focusableElements.forEach((el) => {
                (el as HTMLElement).removeAttribute("tabindex");
            });
        });
    }
 
    /**
     * Set accessibility attributes on elements
     * @param selector CSS selector string
     * @param hidden Whether elements should be hidden from screen readers
     * @param visible Whether elements should be visible
     */
    static setAccessibilityState(selector: string, hidden: boolean, visible: boolean): void {
        const elements = this.getElements(selector);
        elements.forEach(element => {
            element.setAttribute("aria-hidden", hidden.toString());
            element.style.visibility = visible ? "visible" : "hidden";
        });
    }
 
    /**
     * Get the value of an input element
     * @param selector CSS selector string
     * @returns The input value or empty string if not found
     */
    static getValue(selector: string): string {
        const element = this.getElement(selector) as HTMLInputElement;
        return element ? element.value : "";
    }
 
    /**
     * Set the value of an input element
     * @param selector CSS selector string
     * @param value Value to set
     */
    static setValue(selector: string, value: string): void {
        const element = this.getElement(selector) as HTMLInputElement;
        if (element) element.value = value;
    }
 
    /**
     * Add a CSS class to an element
     * @param selector CSS selector string
     * @param className Class name to add
     */
    static addClass(selector: string, className: string): void {
        const element = this.getElement(selector);
        if (element) element.classList.add(className);
    }
 
    /**
     * Remove a CSS class from an element
     * @param selector CSS selector string
     * @param className Class name to remove
     */
    static removeClass(selector: string, className: string): void {
        const element = this.getElement(selector);
        if (element) element.classList.remove(className);
    }
 
    /**
     * Toggle a CSS class on an element
     * @param selector CSS selector string
     * @param className Class name to toggle
     */
    static toggleClass(selector: string, className: string): void {
        const element = this.getElement(selector);
        if (element) element.classList.toggle(className);
    }
 
    /**
     * Check if an element has a CSS class
     * @param selector CSS selector string
     * @param className Class name to check
     * @returns True if element has the class, false otherwise
     */
    static hasClass(selector: string, className: string): boolean {
        const element = this.getElement(selector);
        return element ? element.classList.contains(className) : false;
    }
}