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 308x 308x 769x 462x 769x 4x 4x 4x 769x 22x 22x 22x 769x 1x 1x 435x 435x 308x  
/**
 * 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
  }
}