All files / src/renderer/lib/helpers dom.ts

20.93% Statements 9/43
0% Branches 0/40
0% Functions 0/4
12.82% Lines 5/39

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 931x                             1x                         1x                     1x                                       1x                                                                  
export const getEventNodeName = (e: unknown) => {
  if (!e) {
    return "";
  }
  return (
    (
      e as {
        target?: {
          nodeName?: string;
        };
      }
    ).target?.nodeName ?? ""
  );
};
 
export const getDeepActiveElement = (): Element | null => {
  let activeElement: Element | null = document.activeElement;
 
  while (
    activeElement instanceof HTMLElement &&
    activeElement.shadowRoot?.activeElement
  ) {
    activeElement = activeElement.shadowRoot.activeElement;
  }
 
  return activeElement;
};
 
const selectableInputTypes = new Set([
  "",
  "text",
  "search",
  "url",
  "tel",
  "password",
  "email",
  "number",
]);
 
export const canPerformSelectAll = (element: Element | null): boolean => {
  if (!element) {
    return false;
  }
 
  if (element instanceof HTMLInputElement) {
    return !element.disabled && selectableInputTypes.has(element.type);
  }
 
  if (element instanceof HTMLTextAreaElement) {
    return !element.disabled;
  }
 
  if (element instanceof HTMLElement && element.isContentEditable) {
    return true;
  }
 
  return false;
};
 
export const performSelectAll = (element: Element): boolean => {
  if (!canPerformSelectAll(element)) {
    return false;
  }
 
  if (element instanceof HTMLInputElement) {
    element.select();
    return true;
  }
 
  if (element instanceof HTMLTextAreaElement) {
    element.select();
    return true;
  }
 
  if (element instanceof HTMLElement && element.isContentEditable) {
    const selection = window.getSelection();
 
    if (!selection) {
      return false;
    }
 
    const range = document.createRange();
    range.selectNodeContents(element);
 
    selection.removeAllRanges();
    selection.addRange(range);
 
    return true;
  }
 
  return false;
};