Skip to content

Truly independent web browser

LibWeb: Give -webkit-box items their fit-content width

sideshowbarker3c9bb97fd740
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 0b236579b76..64d37bd9f12 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -562,6 +562,15 @@ impl<'pass> FlexFormattingContext<'pass> {
self.container.is_legacy_webkit_box
}
+ fn item_of_legacy_webkit_box_fits_its_content(&self, index: usize) -> bool {
+ let inputs = &self.flex_item_details[index].inputs;
+ self.is_legacy_webkit_box()
+ && self.is_row_layout()
+ && !inputs.flex_basis_is_content
+ && inputs.flex_basis.is_auto()
+ && inputs.preferred_main_size.is_auto()
+ }
+
/// The order of an item styled `style` in order-modified document order.
fn order_of_item(&self, style: StyleValues<'_>) -> i32 {
// AD-HOC: The items of a legacy webkit box are ordered by their -webkit-box-ordinal-group, and order doesn't
@@ -1528,6 +1537,12 @@ impl<'pass> FlexFormattingContext<'pass> {
max_cross,
)
}
+ // AD-HOC: An item of a horizontal legacy webkit box whose flex basis and width are auto gets a flex base
+ // size of its fit-content width rather than its max-content width, so it's never wider than the
+ // box unless its content can't fit, matching other engines.
+ UsedFlexBasis::Content if self.item_of_legacy_webkit_box_fits_its_content(index) => {
+ self.calculate_fit_content_main_size(index)
+ }
// C. If the used flex basis is content or depends on its available space,
// and the flex container is being sized under a min-content or max-content constraint
// (e.g. when performing automatic table layout [CSS21]), size the item under that constraint.
diff --git a/Tests/LibWeb/Text/expected/Layout/webkit-box-item-gets-fit-content-width.txt b/Tests/LibWeb/Text/expected/Layout/webkit-box-item-gets-fit-content-width.txt
new file mode 100644
index 00000000000..c6f8ed99822
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Layout/webkit-box-item-gets-fit-content-width.txt
@@ -0,0 +1,11 @@
+text: 300x60 at 0
+image-in-picture: 300x150 at 0
+short-text: 46.75x20 at 0
+fixed-width: 400x20 at 0
+min-width: 350x20 at 0
+nowrap: 483.0625x20 at 0
+with-margins: 290x40 at 15
+packed-center: 300x40 at 0
+first-of-two: 300x40 at 0
+second-of-two: 300x40 at 300
+in-inline-box: 300x40 at 0
diff --git a/Tests/LibWeb/Text/input/Layout/webkit-box-item-gets-fit-content-width.html b/Tests/LibWeb/Text/input/Layout/webkit-box-item-gets-fit-content-width.html
new file mode 100644
index 00000000000..cbcefb9267a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Layout/webkit-box-item-gets-fit-content-width.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ .box { display: -webkit-box; width: 300px; margin-bottom: 8px; font: 20px/1 serif; }
+ img { display: block; max-width: 100%; height: auto; }
+</style>
+<!-- An item of a horizontal -webkit-box with an automatic width gets its fit-content width, so it's never wider than the
+ box unless its content can't fit: Its text wraps, and an image with max-width: 100% scales down. -->
+<div class="box"><div id="text">aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj kkkk llll mmmm nnnn</div></div>
+<div class="box"><picture id="image-in-picture"><img width="1000" height="500" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1000' height='500'/%3E"></picture></div>
+<div class="box"><div id="short-text">aaaa</div></div>
+<div class="box"><div id="fixed-width" style="width: 400px">aaaa</div></div>
+<div class="box"><div id="min-width" style="min-width: 350px">aaaa</div></div>
+<div class="box"><div id="nowrap" style="white-space: nowrap">aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj</div></div>
+<div class="box" style="padding: 0 10px"><div id="with-margins" style="margin: 0 5px">aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj</div></div>
+<div class="box" style="-webkit-box-pack: center"><div id="packed-center">aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj</div></div>
+<div class="box"><div id="first-of-two">aaaa bbbb cccc dddd eeee ffff gggg hhhh</div><div id="second-of-two">iiii jjjj kkkk llll mmmm nnnn oooo</div></div>
+<div style="width: 300px"><div class="box" style="display: -webkit-inline-box; width: auto"><div id="in-inline-box">aaaa bbbb cccc dddd eeee ffff gggg hhhh iiii jjjj</div></div></div>
+<script>
+ function run(report) {
+ for (const element of document.querySelectorAll("[id]:not(#out)")) {
+ const rect = element.getBoundingClientRect();
+ report(`${element.id}: ${rect.width}x${rect.height} at ${rect.left}`);
+ }
+ }
+</script>
+<script>
+ if (window.asyncTest)
+ asyncTest(done => addEventListener("load", () => { run(println); done(); }));
+ else
+ addEventListener("load", () => { run(line => console.log(line)); window.engineCompareDone = true; });
+</script>