Skip to content

Truly independent web browser

LibWeb: Center button content in flex items with definite cross size

Andreas Kling07c0e0e7f526
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 6836a21ee60..67b291b096d 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -4053,19 +4053,38 @@ impl<'pass> FlexFormattingContext<'pass> {
// the cross sizes of items in auto-sized flex containers are also considered definite for the purpose of layout.
// NB: This refers to step 11, which stretches items. Other engines leave the content-based cross size of an item
// that is not stretched indefinite.
- if self.should_treat_cross_size_as_auto(self.run.box_) {
- for index in 0..self.flex_items.len() {
- // NB: A button or a text input centers its content within its used block size, so it keeps it definite.
- let item_facts = self.facts(self.flex_items[index].box_);
- if !self.flex_item_is_stretched(index)
- && !item_facts.uses_button_layout()
- && !item_facts.is_text_input()
- {
- continue;
- }
- let size = self.flex_items[index].cross_size.unwrap();
- self.set_cross_size(index, size);
- self.set_has_definite_cross_size(index);
+ // NB: A button or a text input centers its content within its used block size, so it keeps it definite, even
+ // in a flex container with a definite cross size.
+ let container_cross_size_is_auto = self.should_treat_cross_size_as_auto(self.run.box_);
+ for index in 0..self.flex_items.len() {
+ let item_facts = self.facts(self.flex_items[index].box_);
+ let item_style = self.style(self.flex_items[index].box_);
+ // NB: A flex or grid button has no anonymous button content box, so it does not center its content.
+ let uses_button_layout = item_facts.uses_button_layout()
+ && !item_style.display().is_flex_inside()
+ && !item_style.display().is_grid_inside();
+ let centers_its_content = uses_button_layout || item_facts.is_text_input();
+ if !centers_its_content && !container_cross_size_is_auto {
+ continue;
+ }
+ let is_stretched = self.flex_item_is_stretched(index);
+ if !centers_its_content && !is_stretched {
+ continue;
+ }
+ let size = self.flex_items[index].cross_size.unwrap();
+ self.set_cross_size(index, size);
+ self.set_has_definite_cross_size(index);
+ // NB: The content-based cross size of a button that is not stretched stays indefinite for percentages
+ // inside it.
+ if uses_button_layout
+ && !is_stretched
+ && !self.cross_axis_is_horizontal()
+ && (self.should_treat_cross_size_as_auto(self.flex_items[index].box_)
+ || item_style.height().is_intrinsic_sizing_constraint())
+ {
+ self.flex_items[index]
+ .cells
+ .has_definite_block_size_only_for_button_content_alignment = true;
}
}
// 16. Align all flex lines (per align-content)
diff --git a/Tests/LibWeb/Layout/expected/flex-item-flex-button-percentage-height-child.txt b/Tests/LibWeb/Layout/expected/flex-item-flex-button-percentage-height-child.txt
new file mode 100644
index 00000000000..384c965e60d
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/flex-item-flex-button-percentage-height-child.txt
@@ -0,0 +1,22 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 296 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 280 0+0+8] children: not-inline
+ Box <div.c> at [8,8] flex-container(row) [0+0+0 784 0+0+0] [0+0+0 200 0+0+0] [FFC] children: not-inline
+ Box <button#b1> at [8,8] flex-container(row) flex-item [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ BlockContainer <div.b> at [8,8] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [13.140625,8 9.703125x16] baseline: 12.796875
+ "x"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,208] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div.ca> at [8,208] flex-container(row) [0+0+0 784 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ Box <button#b2> at [8,208] flex-container(row) flex-item [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ BlockContainer <div.b> at [8,208] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [13.140625,208 9.703125x16] baseline: 12.796875
+ "x"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,288] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x296] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/flex-item-max-content-button-percentage-height-child.txt b/Tests/LibWeb/Layout/expected/flex-item-max-content-button-percentage-height-child.txt
new file mode 100644
index 00000000000..f75efada579
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/flex-item-max-content-button-percentage-height-child.txt
@@ -0,0 +1,26 @@
+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 296 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 280 0+0+8] children: not-inline
+ Box <div.c> at [8,8] flex-container(row) [0+0+0 784 0+0+0] [0+0+0 200 0+0+0] [FFC] children: not-inline
+ BlockContainer <button#b1> at [8,8] flex-item [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [8,8] flex-container(column) [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [8,40] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.b> at [8,40] [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [13.140625,40 9.703125x16] baseline: 12.796875
+ "x"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,208] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+ Box <div.ca> at [8,208] flex-container(row) [0+0+0 784 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ BlockContainer <button#b2> at [8,208] flex-item [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [8,208] flex-container(column) [0+0+0 20 0+0+0] [0+0+0 80 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [8,240] flex-item [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.b> at [8,240] [0+0+0 20 0+0+0] [0+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [13.140625,240 9.703125x16] baseline: 12.796875
+ "x"
+ TextNode <#text> (not painted)
+ BlockContainer <(anonymous)> at [8,288] [0+0+0 784 0+0+0] [0+0+0 0 0+0+0] children: inline
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x296] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/expected/pdf-viewer.txt b/Tests/LibWeb/Layout/expected/pdf-viewer.txt
index 6c16cfae061..91a6e233f6b 100644
--- a/Tests/LibWeb/Layout/expected/pdf-viewer.txt
+++ b/Tests/LibWeb/Layout/expected/pdf-viewer.txt
@@ -102,10 +102,10 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
TextNode <#text> (not painted)
TextInputBox <input#pageNumber.toolbarField> at [166,8] flex-item [0+1+7 40 7+1+0] [3+1+4 16 4+1+3] [BFC] children: not-inline
- Box <div> at [166,8] flex-container(row) [0+0+0 40 0+0+0] [0+0+0 13 0+0+0] [FFC] children: not-inline
- Box <div> at [166,8] flex-container(row) flex-item [0+0+0 40 0+0+0] [0+0+0 13 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [166,8] flex-item [0+0+0 40 0+0+0] [0+0+0 13 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 1, rect: [200.84375,8 5.15625x13] baseline: 10.390625
+ Box <div> at [166,8] flex-container(row) [0+0+0 40 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ Box <div> at [166,9.5] flex-container(row) flex-item [0+0+0 40 0+0+0] [0+0+0 13 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [166,9.5] flex-item [0+0+0 40 0+0+0] [0+0+0 13 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [200.84375,9.5 5.15625x13] baseline: 10.390625
"1"
TextNode <#text> (not painted)
BlockContainer <(anonymous)> (not painted) [BFC] children: inline
diff --git a/Tests/LibWeb/Layout/input/flex-item-flex-button-percentage-height-child.html b/Tests/LibWeb/Layout/input/flex-item-flex-button-percentage-height-child.html
new file mode 100644
index 00000000000..f4fa44bf733
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/flex-item-flex-button-percentage-height-child.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html><style>
+.c{display:flex;align-items:flex-start;height:200px}
+.ca{display:flex;align-items:flex-start}
+button{display:flex;align-items:flex-start;min-height:80px;padding:0;border:0}
+.b{width:20px;height:100%;background:green}
+</style>
+<div class=c><button id=b1><div class=b>x</div></button></div>
+<div class=ca><button id=b2><div class=b>x</div></button></div>
diff --git a/Tests/LibWeb/Layout/input/flex-item-max-content-button-percentage-height-child.html b/Tests/LibWeb/Layout/input/flex-item-max-content-button-percentage-height-child.html
new file mode 100644
index 00000000000..bd58c195459
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/flex-item-max-content-button-percentage-height-child.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html><style>
+.c{display:flex;align-items:flex-start;height:200px}
+.ca{display:flex;align-items:flex-start}
+button{height:max-content;min-height:80px;padding:0;border:0}
+.b{width:20px;height:50%;background:green}
+</style>
+<div class=c><button id=b1><div class=b>x</div></button></div>
+<div class=ca><button id=b2><div class=b>x</div></button></div>
diff --git a/Tests/LibWeb/Ref/expected/flex-item-button-centers-content-in-clamped-automatic-height-ref.html b/Tests/LibWeb/Ref/expected/flex-item-button-centers-content-in-clamped-automatic-height-ref.html
new file mode 100644
index 00000000000..7949a7ea505
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/flex-item-button-centers-content-in-clamped-automatic-height-ref.html
@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ }
+ .row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ width: 400px;
+ height: 60px;
+ margin-bottom: 8px;
+ background: lightblue;
+ }
+ .column {
+ display: flex;
+ flex-direction: column;
+ align-items: flex-start;
+ width: 400px;
+ height: 60px;
+ background: lightblue;
+ }
+ button, input {
+ box-sizing: border-box;
+ margin: 0;
+ padding: 0;
+ border: 0;
+ font: 14px sans-serif;
+ background: pink;
+ height: 42px;
+ }
+ button {
+ width: 42px;
+ }
+ input {
+ appearance: none;
+ width: 80px;
+ }
+ .icon {
+ display: inline-block;
+ vertical-align: top;
+ width: 20px;
+ height: 20px;
+ background: green;
+ }
+</style>
+<div class="row">
+ <button><span class="icon"></span></button>
+ <button>Xyz</button>
+ <input value="Xyz">
+</div>
+<div class="row" style="align-items: flex-start">
+ <button><span class="icon"></span></button>
+ <button>Xyz</button>
+ <input value="Xyz">
+</div>
+<div class="row">
+ <button><div style="width: 20px; background: blue"><span class="icon"></span></div></button>
+</div>
+<div class="row" style="height: auto">
+ <div style="height: 60px"></div>
+ <button><div style="width: 20px; background: blue"><span class="icon"></span></div></button>
+</div>
+<div class="column">
+ <button><span class="icon"></span></button>
+</div>
diff --git a/Tests/LibWeb/Ref/input/flex-item-button-centers-content-in-clamped-automatic-height.html b/Tests/LibWeb/Ref/input/flex-item-button-centers-content-in-clamped-automatic-height.html
new file mode 100644
index 00000000000..c45b1fdf868
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/flex-item-button-centers-content-in-clamped-automatic-height.html
@@ -0,0 +1,67 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/flex-item-button-centers-content-in-clamped-automatic-height-ref.html">
+<style>
+ body {
+ margin: 0;
+ }
+ .row {
+ display: flex;
+ align-items: center;
+ gap: 8px;
+ width: 400px;
+ height: 60px;
+ margin-bottom: 8px;
+ background: lightblue;
+ }
+ .column {
+ display: flex;
+ flex-direction: column;
+ align-items: flex-start;
+ width: 400px;
+ height: 60px;
+ background: lightblue;
+ }
+ button, input {
+ box-sizing: border-box;
+ margin: 0;
+ padding: 0;
+ border: 0;
+ font: 14px sans-serif;
+ background: pink;
+ min-height: 42px;
+ }
+ button {
+ width: 42px;
+ }
+ input {
+ appearance: none;
+ width: 80px;
+ }
+ .icon {
+ display: inline-block;
+ vertical-align: top;
+ width: 20px;
+ height: 20px;
+ background: green;
+ }
+</style>
+<div class="row">
+ <button><span class="icon"></span></button>
+ <button>Xyz</button>
+ <input value="Xyz">
+</div>
+<div class="row" style="align-items: flex-start">
+ <button><span class="icon"></span></button>
+ <button>Xyz</button>
+ <input value="Xyz">
+</div>
+<div class="row">
+ <button><div style="height: 100%; width: 20px; background: blue"><span class="icon"></span></div></button>
+</div>
+<div class="row" style="height: auto">
+ <div style="height: 60px"></div>
+ <button><div style="height: 100%; width: 20px; background: blue"><span class="icon"></span></div></button>
+</div>
+<div class="column">
+ <button><span class="icon"></span></button>
+</div>