LibWeb: Avoid division by zero when calculating box aspect ratio

This commit is contained in:
Tim Ledbetter 2024-03-23 15:46:08 +00:00 committed by Andreas Kling
parent 2227674b91
commit e1fbb08747
3 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,16 @@
Viewport <#document> at (0,0) content-size 800x600 children: not-inline
BlockContainer <html> at (0,0) content-size 800x33 [BFC] children: not-inline
BlockContainer <body> at (8,8) content-size 784x17 children: not-inline
BlockContainer <div> at (8,8) content-size 784x17 children: inline
frag 0 from TextNode start: 0, length: 22, rect: [8,8 186.515625x17] baseline: 13.296875
"Pass if we don't crash"
TextNode <#text>
BlockContainer <(anonymous)> at (8,25) content-size 784x0 children: inline
TextNode <#text>
ViewportPaintable (Viewport<#document>) [0,0 800x600]
PaintableWithLines (BlockContainer<HTML>) [0,0 800x33]
PaintableWithLines (BlockContainer<BODY>) [8,8 784x17]
PaintableWithLines (BlockContainer<DIV>) [8,8 784x17]
TextPaintable (TextNode<#text>)
PaintableWithLines (BlockContainer(anonymous)) [8,25 784x0]

View file

@ -0,0 +1,5 @@
<!DOCTYPE html><style>
div {
aspect-ratio: 0 / 0;
}
</style><div>Pass if we don't crash</div>

View file

@ -85,7 +85,15 @@ Optional<CSSPixelFraction> Box::preferred_aspect_ratio() const
auto computed_aspect_ratio = computed_values().aspect_ratio();
if (computed_aspect_ratio.use_natural_aspect_ratio_if_available && natural_aspect_ratio().has_value())
return natural_aspect_ratio();
return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return CSSPixelFraction(CSSPixels(ratio.numerator()), CSSPixels(ratio.denominator())); });
if (!computed_aspect_ratio.preferred_ratio.has_value())
return {};
auto ratio = computed_aspect_ratio.preferred_ratio.release_value();
if (ratio.is_degenerate())
return {};
return CSSPixelFraction(CSSPixels(ratio.numerator()), CSSPixels(ratio.denominator()));
}
}