| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1x 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;
}
|