All files / src/support/dom attribute.ts

100% Statements 11/11
100% Branches 14/14
100% Functions 6/6
100% Lines 11/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 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                    29x 12x   17x                                 806x 59x   747x               15x 5x                     13x                     9x                     7x    
import { isUndefined } from 'support/utils/types';
 
type GetSetPropName = 'scrollLeft' | 'scrollTop' | 'value';
 
function getSetProp(
  topLeft: GetSetPropName,
  fallback: number | string,
  elm: HTMLElement | HTMLInputElement | null,
  value?: number | string
): number | string | void {
  if (isUndefined(value)) {
    return elm ? elm[topLeft] : fallback;
  }
  elm && (elm[topLeft] = value);
}
 
/**
 * Gets or sets a attribute with the given attribute of the given element depending whether the value attribute is given.
 * Returns null if the element has no attribute with the given name.
 * @param elm The element of which the attribute shall be get or set.
 * @param attrName The attribute name which shall be get or set.
 * @param value The value of the attribute which shall be set.
 */
export function attr(elm: HTMLElement | null, attrName: string): string | null;
export function attr(elm: HTMLElement | null, attrName: string, value: string): void;
export function attr(
  elm: HTMLElement | null,
  attrName: string,
  value?: string
): string | null | void {
  if (isUndefined(value)) {
    return elm ? elm.getAttribute(attrName) : null;
  }
  elm && elm.setAttribute(attrName, value);
}
 
/**
 * Removes the given attribute from the given element.
 * @param elm The element of which the attribute shall be removed.
 * @param attrName The attribute name.
 */
export const removeAttr = (elm: Element | null, attrName: string): void => {
  elm && elm.removeAttribute(attrName);
};
 
/**
 * Gets or sets the scrollLeft value of the given element depending whether the value attribute is given.
 * @param elm The element of which the scrollLeft value shall be get or set.
 * @param value The scrollLeft value which shall be set.
 */
export function scrollLeft(elm: HTMLElement | null): number;
export function scrollLeft(elm: HTMLElement | null, value: number): void;
export function scrollLeft(elm: HTMLElement | null, value?: number): number | void {
  return getSetProp('scrollLeft', 0, elm, value) as number;
}
 
/**
 * Gets or sets the scrollTop value of the given element depending whether the value attribute is given.
 * @param elm The element of which the scrollTop value shall be get or set.
 * @param value The scrollTop value which shall be set.
 */
export function scrollTop(elm: HTMLElement | null): number;
export function scrollTop(elm: HTMLElement | null, value: number): void;
export function scrollTop(elm: HTMLElement | null, value?: number): number | void {
  return getSetProp('scrollTop', 0, elm, value) as number;
}
 
/**
 * Gets or sets the value of the given input element depending whether the value attribute is given.
 * @param elm The input element of which the value shall be get or set.
 * @param value The value which shall be set.
 */
export function val(elm: HTMLInputElement | null): string;
export function val(elm: HTMLInputElement | null, value: string): void;
export function val(elm: HTMLInputElement | null, value?: string): string | void {
  return getSetProp('value', '', elm, value) as string;
}