function getRangeClientRectsChrome()

in src/component/selection/getRangeClientRects.js [25:55]


function getRangeClientRectsChrome(range: Range): Array<ClientRect> {
  const tempRange = range.cloneRange();
  const clientRects = [];

  for (
    let ancestor: ?Node = range.endContainer;
    ancestor != null;
    ancestor = ancestor.parentNode
  ) {
    // If we've climbed up to the common ancestor, we can now use the
    // original start point and stop climbing the tree.
    const atCommonAncestor = ancestor === range.commonAncestorContainer;
    if (atCommonAncestor) {
      tempRange.setStart(range.startContainer, range.startOffset);
    } else {
      tempRange.setStart(tempRange.endContainer, 0);
    }
    const rects = Array.from(tempRange.getClientRects());
    clientRects.push(rects);
    if (atCommonAncestor) {
      clientRects.reverse();
      return [].concat(...clientRects);
    }
    tempRange.setEndBefore(ancestor);
  }

  invariant(
    false,
    'Found an unexpected detached subtree when getting range client rects.',
  );
}