LibWeb: Avoid division by zero when calculating SVG viewbox aspect ratio

This commit is contained in:
Tim Ledbetter 2024-03-17 08:37:45 +00:00 committed by Andreas Kling
parent b61aab66d9
commit e9383b9c86
Notes: sideshowbarker 2024-07-17 09:49:48 +09:00
3 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1 @@
PASS (didn't crash)

View File

@ -0,0 +1,10 @@
<script src="../include.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" id="svg-element"></svg>
<script>
test(() => {
const svgElement = document.getElementById("svg-element");
svgElement.height = 1;
svgElement.setAttribute("viewBox", "0 0 1 0");
println("PASS (didn't crash)");
});
</script>

View File

@ -73,7 +73,11 @@ Optional<CSSPixelFraction> SVGSVGBox::calculate_intrinsic_aspect_ratio() const
auto const& viewbox = dom_node().view_box().value();
// 2. return viewbox.width / viewbox.height
return CSSPixels::nearest_value_for(viewbox.width) / CSSPixels::nearest_value_for(viewbox.height);
auto height = CSSPixels::nearest_value_for(viewbox.height);
if (height != 0)
return CSSPixels::nearest_value_for(viewbox.width) / CSSPixels::nearest_value_for(viewbox.height);
return {};
}
// 4. return null