All files / src/core/dom traversal.ts

100% Statements 30/30
100% Branches 25/25
100% Functions 9/9
100% Lines 26/26

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    4x   4x 6x   6x 18x     6x     19x   4x 31x 22x 2x   20x 2x   18x 6x     21x     4x 3x   3x 6x 3x 1x     3x       3x     38x   14x  
import { each } from 'core/utils/arrays';
 
const elementIsVisible: (elm: HTMLElement) => boolean = (elm) => !!(elm.offsetWidth || elm.offsetHeight || elm.getClientRects().length);
 
export const find: (selector: string, elm?: Element | null) => ReadonlyArray<Element> = (selector, elm?) => {
  const arr: Array<Element> = [];
 
  each((elm || document).querySelectorAll(selector), (e: Element) => {
    arr.push(e);
  });
 
  return arr;
};
 
export const findFirst: (selector: string, elm?: Element | null) => Element | null = (selector, elm?) => (elm || document).querySelector(selector);
 
export const is: (elm: Element | null, selector: string) => boolean = (elm, selector) => {
  if (elm) {
    if (selector === ':visible') {
      return elementIsVisible(elm as HTMLElement);
    }
    if (selector === ':hidden') {
      return !elementIsVisible(elm as HTMLElement);
    }
    if (elm.matches(selector)) {
      return true;
    }
  }
  return false;
};
 
export const children: (elm: Element | null, selector?: string) => ReadonlyArray<Element> = (elm, selector?) => {
  const childs: Array<Element> = [];
 
  each(elm && elm.children, (child: Element) => {
    if (selector) {
      if (child.matches(selector)) {
        childs.push(child);
      }
    } else {
      childs.push(child);
    }
  });
 
  return childs;
};
 
export const contents: (elm: Element | null) => ReadonlyArray<ChildNode> = (elm) => (elm ? Array.from<ChildNode>(elm.childNodes) : []);
 
export const parent: (elm: Node | null) => Node | null = (elm) => (elm ? elm.parentElement : null);