Skip to content

Commit 99aefad

Browse files
OskarEichlermeta-codesync[bot]
authored andcommitted
Preserve DOMRect special number values (#58241)
Summary: The shared DOMRect numeric conversion currently defaults every falsy value to zero, replacing valid `NaN` and changing `-0` to `+0`. Default only nullish optional values, otherwise apply normal Number conversion, and cover both read-only construction and mutable setters. Fixes #58240. ## Changelog: [GENERAL] [FIXED] - Preserve NaN and signed zero in DOMRect values. Pull Request resolved: #58241 Test Plan: - Exact upstream focused regression: 0/2 assertions passed. - Fixed focused Fantom regression: 2/2 passed. - Fresh Flow check: 0 errors. - Targeted no-ignore ESLint, Prettier, and `git diff --check` passed. Only observably incorrect special-number normalization changes; ordinary and omitted values are unchanged. Reviewed By: christophpurrer Differential Revision: D123889534 Pulled By: rubennorte fbshipit-source-id: 19b6633c2fb9879b2c7927afe662bc9a44196ee6
1 parent da651fd commit 99aefad

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/react-native/src/private/webapis/geometry/DOMRectReadOnly.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export interface DOMRectInit {
2626
}
2727

2828
function castToNumber(value: unknown): number {
29-
return value ? Number(value) : 0;
29+
return value == null ? 0 : Number(value);
3030
}
3131

3232
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
12+
13+
import DOMRect from '../DOMRect';
14+
import DOMRectReadOnly from '../DOMRectReadOnly';
15+
16+
describe('DOMRect', () => {
17+
it('preserves NaN and signed zero constructor values', () => {
18+
const rect = new DOMRectReadOnly(-0, NaN, -0, NaN);
19+
20+
expect(Object.is(rect.x, -0)).toBe(true);
21+
expect(Number.isNaN(rect.y)).toBe(true);
22+
expect(Object.is(rect.width, -0)).toBe(true);
23+
expect(Number.isNaN(rect.height)).toBe(true);
24+
});
25+
26+
it('preserves NaN and signed zero assigned values', () => {
27+
const rect = new DOMRect();
28+
29+
rect.x = -0;
30+
rect.y = NaN;
31+
rect.width = -0;
32+
rect.height = NaN;
33+
34+
expect(Object.is(rect.x, -0)).toBe(true);
35+
expect(Number.isNaN(rect.y)).toBe(true);
36+
expect(Object.is(rect.width, -0)).toBe(true);
37+
expect(Number.isNaN(rect.height)).toBe(true);
38+
});
39+
});

0 commit comments

Comments
 (0)