All files / utilities selectors.ts

93.75% Statements 15/16
100% Branches 2/2
100% Functions 2/2
93.33% Lines 14/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 241x 2x 1x 6x 5x       1x 1x 1x   1x 2x 2x 2x 1x   1x          
export function* ancestors(element: HTMLElement): IterableIterator<HTMLElement> {
  let cur = element.parentElement;
  while (cur) {
    yield cur;
    cur = cur.parentElement;
  }
}
 
export function closest(element: HTMLElement, sel: string): HTMLElement {
  const a = ancestors(element);
  let iter = a.next();
 
  while (!iter.done) {
    const cur = iter.value;
    const m = cur.querySelector(sel);
    if (m) {
      return <any>m;
    }
    iter = a.next();
  }
 
  return null;
}