<commit_before>// @flow
import type { Rect } from '../types';
import getBoundingClientRect from './getBoundingClientRect';
import listScrollParents from './listScrollParents';
import getScrollSum from './getScrollSum';
import getOffsetParent from './getOffsetParent';
import getWindowScroll from './getWindowScroll';

// Returns the width, height and offsets of the provided element relative to the
// offsetParent
export default (element: Element): Rect => {
  const rect = getBoundingClientRect(element);
  const offsetParent = getOffsetParent(element);
  const scrollParentsScroll = getScrollSum(listScrollParents(element));
  const windowScroll = getWindowScroll(element);
  const scrollLeft = scrollParentsScroll.scrollLeft - windowScroll.scrollLeft;
  const scrollTop = scrollParentsScroll.scrollTop - windowScroll.scrollTop;
  const { left, top } =
    offsetParent instanceof Element
      ? getBoundingClientRect(offsetParent)
      : { left: 0, top: 0 };

  const width = rect.width;
  const height = rect.height;
  const x = rect.left + scrollLeft - left;
  const y = rect.top + scrollTop - top;

  return { width, height, x, y };
};<commit_msg>fix: handle offsetParent window scroll<commit_after>// @flow
import type { Rect } from '../types';
import getBoundingClientRect from './getBoundingClientRect';
import listScrollParents from './listScrollParents';
import getScrollSum from './getScrollSum';
import getOffsetParent from './getOffsetParent';
import getWindowScroll from './getWindowScroll';

// Returns the width, height and offsets of the provided element relative to the
// offsetParent
// TODO: take into account nested offsetParents
export default (element: Element): Rect => {
  const rect = getBoundingClientRect(element);
  const scrollParentsScroll = getScrollSum(listScrollParents(element));
  const windowScroll = getWindowScroll(element);
  const offsetParent = getOffsetParent(element);
  const isOffsetParentElement = offsetParent instanceof Element;
  const offsetParentRect = isOffsetParentElement
    ? getBoundingClientRect(offsetParent)
    : { left: 0, top: 0 };

  const scrollLeft =
    scrollParentsScroll.scrollLeft -
    (isOffsetParentElement ? windowScroll.scrollLeft : 0);
  const scrollTop =
    scrollParentsScroll.scrollTop -
    (isOffsetParentElement ? windowScroll.scrollTop : 0);

  const width = rect.width;
  const height = rect.height;
  const x = rect.left + scrollLeft - offsetParentRect.left;
  const y = rect.top + scrollTop - offsetParentRect.top;

  return { width, height, x, y };
};
{'fraction_non_alphanumeric': 0.09701492537313433, 'fraction_numerical': 0.002356637863315004, 'mean_word_length': 4.762443438914027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3816094', 'n_tokens_mistral': 690, 'n_tokens_neox': 676, 'n_words': 212}