• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

react-component / dom-align / 10520691070

23 Aug 2024 05:59AM UTC coverage: 62.108%. First build
10520691070

push

github

web-flow
Update .github/workflows/ci.yml

202 of 382 branches covered (52.88%)

Branch coverage included in aggregate %.

352 of 510 relevant lines covered (69.02%)

32.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/getVisibleRectForElement.ts
1
import utils from './utils';
2
import getOffsetParent from './getOffsetParent';
3
import isAncestorFixed from './isAncestorFixed';
4

5
/**
6
 * 获得元素的显示部分的区域
7
 */
8
function getVisibleRectForElement(element, alwaysByViewport) {
9
  const visibleRect = {
41✔
10
    left: 0,
11
    right: Infinity,
12
    top: 0,
13
    bottom: Infinity,
14
  };
15
  let el = getOffsetParent(element);
41✔
16
  const doc = utils.getDocument(element);
41✔
17
  const win = doc.defaultView || doc.parentWindow;
41!
18
  const body = doc.body;
41✔
19
  const documentElement = doc.documentElement;
41✔
20

21
  // Determine the size of the visible rect by climbing the dom accounting for
22
  // all scrollable containers.
23
  while (el) {
41✔
24
    // clientWidth is zero for inline block elements in ie.
25
    if (
76✔
26
      (navigator.userAgent.indexOf('MSIE') === -1 || el.clientWidth !== 0) &&
256!
27
      // body may have overflow set on it, yet we still get the entire
28
      // viewport. In some browsers, el.offsetParent may be
29
      // document.documentElement, so check for that too.
30
      el !== body &&
31
      el !== documentElement &&
32
      utils.css(el, 'overflow') !== 'visible'
33
    ) {
34
      const pos = utils.offset(el);
18✔
35
      // add border
36
      pos.left += el.clientLeft;
18✔
37
      pos.top += el.clientTop;
18✔
38
      visibleRect.top = Math.max(visibleRect.top, pos.top);
18✔
39
      visibleRect.right = Math.min(
18✔
40
        visibleRect.right,
41
        // consider area without scrollBar
42
        pos.left + el.clientWidth,
43
      );
44
      visibleRect.bottom = Math.min(visibleRect.bottom, pos.top + el.clientHeight);
18✔
45
      visibleRect.left = Math.max(visibleRect.left, pos.left);
18✔
46
    } else if (el === body || el === documentElement) {
58✔
47
      break;
24✔
48
    }
49
    el = getOffsetParent(el);
52✔
50
  }
51

52
  // Set element position to fixed
53
  // make sure absolute element itself don't affect it's visible area
54
  // https://github.com/ant-design/ant-design/issues/7601
55
  let originalPosition = null;
41✔
56
  if (!utils.isWindow(element) && element.nodeType !== 9) {
41!
57
    originalPosition = element.style.position;
41✔
58
    const position = utils.css(element, 'position');
41✔
59
    if (position === 'absolute') {
41✔
60
      element.style.position = 'fixed';
17✔
61
    }
62
  }
63

64
  const scrollX = utils.getWindowScrollLeft(win);
41✔
65
  const scrollY = utils.getWindowScrollTop(win);
41✔
66
  const viewportWidth = utils.viewportWidth(win);
41✔
67
  const viewportHeight = utils.viewportHeight(win);
41✔
68
  let documentWidth = documentElement.scrollWidth;
41✔
69
  let documentHeight = documentElement.scrollHeight;
41✔
70

71
  // scrollXXX on html is sync with body which means overflow: hidden on body gets wrong scrollXXX.
72
  // We should cut this ourself.
73
  const bodyStyle = window.getComputedStyle(body);
41✔
74
  if (bodyStyle.overflowX === 'hidden') {
41!
75
    documentWidth = win.innerWidth;
×
76
  }
77
  if (bodyStyle.overflowY === 'hidden') {
41!
78
    documentHeight = win.innerHeight;
×
79
  }
80

81
  // Reset element position after calculate the visible area
82
  if (element.style) {
41!
83
    element.style.position = originalPosition;
41✔
84
  }
85

86
  if (alwaysByViewport || isAncestorFixed(element)) {
41✔
87
    // Clip by viewport's size.
88
    visibleRect.left = Math.max(visibleRect.left, scrollX);
4✔
89
    visibleRect.top = Math.max(visibleRect.top, scrollY);
4✔
90
    visibleRect.right = Math.min(visibleRect.right, scrollX + viewportWidth);
4✔
91
    visibleRect.bottom = Math.min(visibleRect.bottom, scrollY + viewportHeight);
4✔
92
  } else {
93
    // Clip by document's size.
94
    const maxVisibleWidth = Math.max(documentWidth, scrollX + viewportWidth);
37✔
95
    visibleRect.right = Math.min(visibleRect.right, maxVisibleWidth);
37✔
96

97
    const maxVisibleHeight = Math.max(documentHeight, scrollY + viewportHeight);
37✔
98
    visibleRect.bottom = Math.min(visibleRect.bottom, maxVisibleHeight);
37✔
99
  }
100

101
  return visibleRect.top >= 0 &&
41✔
102
    visibleRect.left >= 0 &&
103
    visibleRect.bottom > visibleRect.top &&
104
    visibleRect.right > visibleRect.left
105
    ? visibleRect
106
    : null;
107
}
108

109
export default getVisibleRectForElement;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc