All files / src/core/dom attributes.ts

100% Statements 18/18
100% Branches 8/8
100% Functions 5/5
100% Lines 18/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 56 57 58 59              4x 7x 5x   2x               4x 1x               4x 3x 2x   1x               4x 3x 2x   1x               4x 5x 3x   2x    
/**
 * 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 const attr: (elm: Element, attrName: string, value?: string) => string | null | void = (elm, attrName, value) => {
  if (value === undefined) {
    return elm.getAttribute(attrName);
  }
  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, attrName: string) => void = (elm, attrName) => {
  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 const scrollLeft: (elm: HTMLElement, value?: number) => number | void = (elm, value) => {
  if (value === undefined) {
    return elm.scrollLeft;
  }
  elm.scrollLeft = value;
};
 
/**
 * 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 const scrollTop: (elm: HTMLElement, value?: number) => number | void = (elm, value) => {
  if (value === undefined) {
    return elm.scrollTop;
  }
  elm.scrollTop = value;
};
 
/**
 * 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 const val: (elm: HTMLInputElement, value?: string) => string | void = (elm, value) => {
  if (value === undefined) {
    return elm.value;
  }
  elm.value = value;
};