From 1560bfc6c9b4ea6c844a4da1e34b348744711763 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 17 Feb 2024 08:09:53 +0000 Subject: [PATCH] LibWeb: Ensure DOMRect top, bottom, left and right handle NaN correctly --- .../Text/expected/geometry/domrect-create.txt | 8 +++++ .../Text/input/geometry/domrect-create.html | 12 +++++++ .../LibWeb/Geometry/DOMRectReadOnly.h | 31 ++++++++++++++++--- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Tests/LibWeb/Text/expected/geometry/domrect-create.txt b/Tests/LibWeb/Text/expected/geometry/domrect-create.txt index 33a8860498..9ec818a50a 100644 --- a/Tests/LibWeb/Text/expected/geometry/domrect-create.txt +++ b/Tests/LibWeb/Text/expected/geometry/domrect-create.txt @@ -3,8 +3,16 @@ Testing DOMRect: 2. {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10} 3. {"x":-10,"y":-20,"width":30,"height":40,"top":-20,"right":20,"bottom":20,"left":-10} 4. {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10} +5. {"x":null,"y":20,"width":30,"height":40,"top":20,"right":null,"bottom":60,"left":null} +6. {"x":10,"y":null,"width":30,"height":40,"top":null,"right":40,"bottom":null,"left":10} +7. {"x":10,"y":20,"width":null,"height":40,"top":20,"right":null,"bottom":60,"left":null} +8. {"x":10,"y":20,"width":30,"height":null,"top":null,"right":40,"bottom":null,"left":10} Testing DOMRectReadOnly: 1. {"x":0,"y":0,"width":0,"height":0,"top":0,"right":0,"bottom":0,"left":0} 2. {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10} 3. {"x":-10,"y":-20,"width":30,"height":40,"top":-20,"right":20,"bottom":20,"left":-10} 4. {"x":10,"y":20,"width":30,"height":40,"top":20,"right":40,"bottom":60,"left":10} +5. {"x":null,"y":20,"width":30,"height":40,"top":20,"right":null,"bottom":60,"left":null} +6. {"x":10,"y":null,"width":30,"height":40,"top":null,"right":40,"bottom":null,"left":10} +7. {"x":10,"y":20,"width":null,"height":40,"top":20,"right":null,"bottom":60,"left":null} +8. {"x":10,"y":20,"width":30,"height":null,"top":null,"right":40,"bottom":null,"left":10} diff --git a/Tests/LibWeb/Text/input/geometry/domrect-create.html b/Tests/LibWeb/Text/input/geometry/domrect-create.html index 3c892afe50..64c0a1cb09 100644 --- a/Tests/LibWeb/Text/input/geometry/domrect-create.html +++ b/Tests/LibWeb/Text/input/geometry/domrect-create.html @@ -26,6 +26,18 @@ // 4. Creating a DOMRect with DOMRect.fromRect() testPart(() => Rect.fromRect({ x: 10, y: 20, width: 30, height: 40 })); + // 5. Creating a DOMRect with NaN x value + testPart(() => new Rect(NaN, 20, 30, 40)); + + // 6. Creating a DOMRect with NaN y value + testPart(() => new Rect(10, NaN, 30, 40)); + + // 7. Creating a DOMRect with NaN width value + testPart(() => new Rect(10, 20, NaN, 40)); + + // 8. Creating a DOMRect with NaN height value + testPart(() => new Rect(10, 20, 30, NaN)); + testCounter = 1; } }); diff --git a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h index 8d2a02cc61..0d1883972d 100644 --- a/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h +++ b/Userland/Libraries/LibWeb/Geometry/DOMRectReadOnly.h @@ -36,10 +36,33 @@ public: double width() const { return m_rect.width(); } double height() const { return m_rect.height(); } - double top() const { return min(y(), y() + height()); } - double right() const { return max(x(), x() + width()); } - double bottom() const { return max(y(), y() + height()); } - double left() const { return min(x(), x() + width()); } + double top() const + { + if (isnan(y()) || isnan(height())) + return NAN; + return min(y(), y() + height()); + } + + double right() const + { + if (isnan(x()) || isnan(width())) + return NAN; + return max(x(), x() + width()); + } + + double bottom() const + { + if (isnan(y()) || isnan(height())) + return NAN; + return max(y(), y() + height()); + } + + double left() const + { + if (isnan(x()) || isnan(width())) + return NAN; + return min(x(), x() + width()); + } protected: DOMRectReadOnly(JS::Realm&, double x, double y, double width, double height);