Skip to content

Commit ca56f32

Browse files
jeasonnowcursoragentsantree.gafc163
authored
fix: #556 guard against undefined window in getScroll (#567)
Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: santree.g <santree.g@mexc.com> Co-authored-by: afc163 <afc163@gmail.com>
1 parent 3ebf65c commit ca56f32

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/util.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ export function getMotionName(prefixCls: string, transitionName?: string, animat
88
}
99

1010
// =============================== Offset ===============================
11-
function getScroll(w: Window, top?: boolean): number {
11+
function getScroll(w: Window | null | undefined, top?: boolean): number {
12+
if (!w) {
13+
return 0;
14+
}
1215
let ret = w[`page${top ? 'Y' : 'X'}Offset`];
1316
const method = `scroll${top ? 'Top' : 'Left'}`;
1417
if (typeof ret !== 'number') {

tests/util.spec.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,33 @@ describe('Dialog.Util', () => {
4848
top: 1128,
4949
});
5050
});
51+
52+
it('returns zero offset when defaultView and parentWindow are missing', () => {
53+
const element = {
54+
ownerDocument: {},
55+
getBoundingClientRect: () => ({ left: 0, top: 0 }),
56+
} as any;
57+
58+
expect(offset(element)).toEqual({
59+
left: 0,
60+
top: 0,
61+
});
62+
});
63+
64+
it('returns zero offset when defaultView and parentWindow are null or undefined', () => {
65+
const element = {
66+
ownerDocument: {
67+
defaultView: null,
68+
parentWindow: undefined,
69+
},
70+
getBoundingClientRect: () => ({ left: 0, top: 0 }),
71+
} as any;
72+
73+
expect(offset(element)).toEqual({
74+
left: 0,
75+
top: 0,
76+
});
77+
});
5178
});
5279

5380
describe('getMotionName', () => {

0 commit comments

Comments
 (0)