From fc2c77be8363759657b50992b93d08d40f8fbf1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BClker?= Date: Fri, 11 Oct 2024 04:43:44 +0200 Subject: [PATCH] Use NaN-safe comparisons in DOMQuad::GetBounds (#33794) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use nan-safe comparison in DOMQuad::GetBounds This fixes at least the following WPT test: * css/geometry/DOMQuad-nan.html Signed-off-by: Simon Wülker * Update WPT expectations Signed-off-by: Simon Wülker --------- Signed-off-by: Simon Wülker --- components/script/dom/domquad.rs | 77 +++++++++++++------ .../meta/css/geometry/DOMQuad-001.html.ini | 4 - .../meta/css/geometry/DOMQuad-nan.html.ini | 25 ------ 3 files changed, 52 insertions(+), 54 deletions(-) delete mode 100644 tests/wpt/meta/css/geometry/DOMQuad-001.html.ini delete mode 100644 tests/wpt/meta/css/geometry/DOMQuad-nan.html.ini diff --git a/components/script/dom/domquad.rs b/components/script/dom/domquad.rs index b787c7d5e72..dbd78c94a18 100644 --- a/components/script/dom/domquad.rs +++ b/components/script/dom/domquad.rs @@ -16,7 +16,7 @@ use crate::dom::domrect::DOMRect; use crate::dom::globalscope::GlobalScope; use crate::script_runtime::CanGc; -// https://drafts.fxtf.org/geometry/#DOMQuad +/// #[dom_struct] pub struct DOMQuad { reflector_: Reflector, @@ -137,31 +137,58 @@ impl DOMQuadMethods for DOMQuad { // https://drafts.fxtf.org/geometry/#dom-domquad-getbounds fn GetBounds(&self) -> DomRoot { - let left = self - .p1 - .X() - .min(self.p2.X()) - .min(self.p3.X()) - .min(self.p4.X()); - let top = self - .p1 - .Y() - .min(self.p2.Y()) - .min(self.p3.Y()) - .min(self.p4.Y()); - let right = self - .p1 - .X() - .max(self.p2.X()) - .max(self.p3.X()) - .max(self.p4.X()); - let bottom = self - .p1 - .Y() - .max(self.p2.Y()) - .max(self.p3.Y()) - .max(self.p4.Y()); + // https://drafts.fxtf.org/geometry/#nan-safe-minimum + let nan_safe_minimum = |a: f64, b: f64| { + if a.is_nan() || b.is_nan() { + f64::NAN + } else { + a.min(b) + } + }; + // https://drafts.fxtf.org/geometry/#nan-safe-maximum + let nan_safe_maximum = |a: f64, b: f64| { + if a.is_nan() || b.is_nan() { + f64::NAN + } else { + a.max(b) + } + }; + + // Step 1. Let bounds be a DOMRect object. + // NOTE: We construct the object at the end + + // Step 2. Let left be the NaN-safe minimum of point 1’s x coordinate, + // point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate. + let left = nan_safe_minimum( + nan_safe_minimum(self.p1.X(), self.p2.X()), + nan_safe_minimum(self.p3.X(), self.p4.X()), + ); + + // Step 3. Let top be the NaN-safe minimum of point 1’s y coordinate, + // point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate. + let top = nan_safe_minimum( + nan_safe_minimum(self.p1.Y(), self.p2.Y()), + nan_safe_minimum(self.p3.Y(), self.p4.Y()), + ); + + // Step 4. Let right be the NaN-safe maximum of point 1’s x coordinate, + // point 2’s x coordinate, point 3’s x coordinate and point 4’s x coordinate. + let right = nan_safe_maximum( + nan_safe_maximum(self.p1.X(), self.p2.X()), + nan_safe_maximum(self.p3.X(), self.p4.X()), + ); + + // Step 5. Let bottom be the NaN-safe maximum of point 1’s y coordinate, + // point 2’s y coordinate, point 3’s y coordinate and point 4’s y coordinate. + let bottom = nan_safe_maximum( + nan_safe_maximum(self.p1.Y(), self.p2.Y()), + nan_safe_maximum(self.p3.Y(), self.p4.Y()), + ); + + // Step 6. Set x coordinate of bounds to left, y coordinate of bounds to top, + // width dimension of bounds to right - left and height dimension of bounds to bottom - top. + // NOTE: Combined with Step 1. DOMRect::new( &self.global(), left, diff --git a/tests/wpt/meta/css/geometry/DOMQuad-001.html.ini b/tests/wpt/meta/css/geometry/DOMQuad-001.html.ini deleted file mode 100644 index fc64685677a..00000000000 --- a/tests/wpt/meta/css/geometry/DOMQuad-001.html.ini +++ /dev/null @@ -1,4 +0,0 @@ -[DOMQuad-001.html] - [fromRect() method on DOMQuad with Infinity: bounds] - expected: FAIL - diff --git a/tests/wpt/meta/css/geometry/DOMQuad-nan.html.ini b/tests/wpt/meta/css/geometry/DOMQuad-nan.html.ini deleted file mode 100644 index 063e41d8ed8..00000000000 --- a/tests/wpt/meta/css/geometry/DOMQuad-nan.html.ini +++ /dev/null @@ -1,25 +0,0 @@ -[DOMQuad-nan.html] - [Setting DOMQuad's p1.x to NaN] - expected: FAIL - - [Setting DOMQuad's p2.y to NaN] - expected: FAIL - - [Setting DOMQuad's p2.x to NaN] - expected: FAIL - - [Setting DOMQuad's p4.x to NaN] - expected: FAIL - - [Setting DOMQuad's p3.x to NaN] - expected: FAIL - - [Setting DOMQuad's p3.y to NaN] - expected: FAIL - - [Setting DOMQuad's p4.y to NaN] - expected: FAIL - - [Setting DOMQuad's p1.y to NaN] - expected: FAIL -