Jelajahi Sumber

LibWeb: Check radius sign in CanvasRenderingContext2D::{arc, ellipse}

As required by the specification: 'If either radiusX or radiusY are
negative, then throw an "IndexSizeError" DOMException.'
Idan Horowitz 4 tahun lalu
induk
melakukan
c5769a7033

+ 12 - 3
Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp

@@ -204,13 +204,21 @@ void CanvasRenderingContext2D::quadratic_curve_to(float cx, float cy, float x, f
     m_path.quadratic_bezier_curve_to({ cx, cy }, { x, y });
 }
 
-void CanvasRenderingContext2D::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
+DOM::ExceptionOr<void> CanvasRenderingContext2D::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
 {
-    ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
+    if (radius < 0)
+        return DOM::IndexSizeError::create(String::formatted("The radius provided ({}) is negative.", radius));
+    return ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
 }
 
-void CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
+DOM::ExceptionOr<void> CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
 {
+    if (radius_x < 0)
+        return DOM::IndexSizeError::create(String::formatted("The major-axis radius provided ({}) is negative.", radius_x));
+
+    if (radius_y < 0)
+        return DOM::IndexSizeError::create(String::formatted("The minor-axis radius provided ({}) is negative.", radius_y));
+
     if ((!counter_clockwise && (end_angle - start_angle) >= M_TAU)
         || (counter_clockwise && (start_angle - end_angle) >= M_TAU)) {
         start_angle = 0;
@@ -261,6 +269,7 @@ void CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float r
     m_path.elliptical_arc_to(end_point, { radius_x, radius_y }, rotation, delta_theta > M_PI, !counter_clockwise);
 
     m_path.close();
+    return {};
 }
 
 void CanvasRenderingContext2D::rect(float x, float y, float width, float height)

+ 2 - 2
Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h

@@ -75,8 +75,8 @@ public:
     void line_to(float x, float y);
     void quadratic_curve_to(float cx, float cy, float x, float y);
 
-    void arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
-    void ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
+    DOM::ExceptionOr<void> arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
+    DOM::ExceptionOr<void> ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
     void rect(float x, float y, float width, float height);
     void stroke();