All files / utils/algorithms traverse-up-dom.js

100% Statements 45/45
100% Branches 18/18
100% Functions 2/2
100% Lines 45/45

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 461x 1x 1x 1x 1x 1x 23x 23x 84x 72x 84x 6x 6x 6x 84x 11x 11x 55x 55x 23x 1x 1x 1x 1x 1x 1x 1x 300x 300x 745x 446x 745x 4x 4x 4x 745x 20x 20x 20x 745x 1x 1x 421x 421x 300x  
/**
 * Traverse up in the document DOM closest element by passing shadow DOM
 * @param {Element} targetElement - first element to yield from the generator
 * @yields {Element} one element for each subsequent ancestor
 */
export function * traverseUpDom (targetElement) {
  let el = targetElement
  while (el != null) {
    yield el
    const { parentNode, parentElement } = el
    if (parentNode instanceof ShadowRoot) {
      el = parentNode.host
      continue
    }
    if (parentNode instanceof Document || parentElement == null) {
      return
    }
    el = parentElement
  }
}
 
/**
 * Traverse up in the document DOM closest element by passing shadow DOM, passing through slots
 * @param {Element} targetElement - first element to yield from the generator
 * @yields {Element} one element for each subsequent assigned slot or ancestor
 */
export function * traverseUpDomWithSlots (targetElement) {
  let el = targetElement
  while (el != null) {
    yield el
    const { parentNode, parentElement, assignedSlot } = el
    if (assignedSlot) {
      el = assignedSlot
      continue
    }
    if (parentNode instanceof ShadowRoot) {
      el = parentNode.host
      continue
    }
    if (parentNode instanceof Document || parentElement == null) {
      return
    }
    el = parentElement
  }
}