浏览代码

LibWeb: Treat width: {min,max,fit}-content as auto if ratio unresolvable

This appears to match other engines.
Andreas Kling 1 年之前
父节点
当前提交
ae906ca497

+ 12 - 0
Tests/LibWeb/Layout/expected/treat-intrinsic-sizing-keywords-as-auto-when-aspect-ratio-cant-be-resolved.txt

@@ -0,0 +1,12 @@
+Viewport <#document> at (0,0) content-size 800x600 children: not-inline
+  BlockContainer <html> at (0,0) content-size 800x800 [BFC] children: not-inline
+    BlockContainer <body> at (8,8) content-size 784x784 children: inline
+      frag 0 from SVGSVGBox start: 0, length: 0, rect: [8,8 784x784] baseline: 784
+      SVGSVGBox <svg> at (8,8) content-size 784x784 [SVG] children: not-inline
+        SVGGeometryBox <rect> at (8,8) content-size 392x392 children: not-inline
+
+ViewportPaintable (Viewport<#document>) [0,0 800x600] overflow: [0,0 800x800]
+  PaintableWithLines (BlockContainer<HTML>) [0,0 800x800]
+    PaintableWithLines (BlockContainer<BODY>) [8,8 784x784]
+      SVGSVGPaintable (SVGSVGBox<svg>) [8,8 784x784]
+        SVGPathPaintable (SVGGeometryBox<rect>) [8,8 392x392]

+ 3 - 0
Tests/LibWeb/Layout/input/treat-intrinsic-sizing-keywords-as-auto-when-aspect-ratio-cant-be-resolved.html

@@ -0,0 +1,3 @@
+<!DOCTYPE html><style>
+    svg { width: max-content; }
+</style><body><svg viewBox="0 0 24 24"><rect x=0 y=0 width=12 height=12></svg>

+ 9 - 2
Userland/Libraries/LibWeb/Layout/FormattingContext.cpp

@@ -1767,14 +1767,21 @@ CSSPixels FormattingContext::calculate_stretch_fit_height(Box const& box, Availa
 
 bool FormattingContext::should_treat_width_as_auto(Box const& box, AvailableSpace const& available_space)
 {
-    if (box.computed_values().width().is_auto())
+    auto const& computed_width = box.computed_values().width();
+    if (computed_width.is_auto())
         return true;
-    if (box.computed_values().width().contains_percentage()) {
+    if (computed_width.contains_percentage()) {
         if (available_space.width.is_max_content())
             return true;
         if (available_space.width.is_indefinite())
             return true;
     }
+    // AD-HOC: If the box has a preferred aspect ratio and no natural height,
+    //         we treat the width as auto, since it can't be resolved through the ratio.
+    if (computed_width.is_min_content() || computed_width.is_max_content() || computed_width.is_fit_content()) {
+        if (box.has_preferred_aspect_ratio() && !box.has_natural_height())
+            return true;
+    }
     return false;
 }