diff --git a/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp index bbcea80441b..56c0929b3b0 100644 --- a/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp +++ b/Libraries/LibWeb/CSS/StyleValues/RadialGradientStyleValue.cpp @@ -113,7 +113,22 @@ CSSPixelSize RadialGradientStyleValue::resolve_size(Layout::Node const& node, CS auto distance = corner_distance(corner); if (m_properties.ending_shape == EndingShape::Ellipse) { auto shape = get_shape(); - auto aspect_ratio = shape.width() / shape.height(); + CSSPixels height = shape.height(); + CSSPixels width = shape.width(); + + // Prevent division by zero + // https://w3c.github.io/csswg-drafts/css-images/#degenerate-radials + if (height == 0) { + // Render as if the ending shape was an ellipse whose width was an arbitrary very large number and whose height + // was an arbitrary very small number greater than zero. This will make the gradient look like a solid-color image equal + // to the color of the last color-stop, or equal to the average color of the gradient if it’s repeating. + constexpr auto arbitrary_small_number = CSSPixels::smallest_positive_value(); + constexpr auto arbitrary_large_number = CSSPixels::max(); + return CSSPixelSize { arbitrary_large_number, arbitrary_small_number }; + } + + auto aspect_ratio = width / height; + auto p = corner - center; auto radius_a = sqrt(p.y() * p.y() * aspect_ratio * aspect_ratio + p.x() * p.x()); auto radius_b = radius_a / aspect_ratio; diff --git a/Tests/LibWeb/Ref/expected/empty-radial-gradient-crash.html b/Tests/LibWeb/Ref/expected/empty-radial-gradient-crash.html new file mode 100644 index 00000000000..aaad33b14c2 --- /dev/null +++ b/Tests/LibWeb/Ref/expected/empty-radial-gradient-crash.html @@ -0,0 +1,2 @@ + +
Should not crash
diff --git a/Tests/LibWeb/Ref/input/empty-radial-gradient-crash.html b/Tests/LibWeb/Ref/input/empty-radial-gradient-crash.html new file mode 100644 index 00000000000..6bcc33546d3 --- /dev/null +++ b/Tests/LibWeb/Ref/input/empty-radial-gradient-crash.html @@ -0,0 +1,3 @@ + + +
Should not crash