Skip to content

Truly independent web browser

LibWeb: Center button content in auto-height flex columns

sideshowbarker384e5a59cc5f
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 69251c73218..dd3398ea97b 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -3299,7 +3299,15 @@ impl<'pass> FlexFormattingContext<'pass> {
index: usize,
) -> (Option<fragment_tree::RunRootFragment>, formatting_context::PlacedChild) {
let node = self.flex_items[index].box_;
- let cells = self.flex_items[index].cells;
+ let mut cells = self.flex_items[index].cells;
+ // A button centers its content within its final main size even when that size can't resolve percentages.
+ if self.flex_items[index].inputs.uses_button_layout
+ && !self.main_axis_is_horizontal()
+ && !cells.has_definite_block_size
+ {
+ cells.has_definite_block_size = true;
+ cells.has_definite_block_size_only_for_button_content_alignment = true;
+ }
let input = self.item_layout_input(index, &cells, self.available_space_for_items.unwrap().space);
let formatting_context::ChildLayout {
outcome,
diff --git a/Tests/LibWeb/Text/expected/HTML/flex-column-button-content-centering.txt b/Tests/LibWeb/Text/expected/HTML/flex-column-button-content-centering.txt
new file mode 100644
index 00000000000..bef6db0292a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/flex-column-button-content-centering.txt
@@ -0,0 +1,12 @@
+column, automatic height: height=160, content=20, top=70
+column, percentage child: height=160, content=20, top=70
+column, definite container: height=160, content=160, top=0
+column, definite basis: height=160, content=160, top=0
+column, flex button: height=160, content=160, top=0
+column, overflow: height=10, content=20, top=0
+column-reverse, automatic height: height=160, content=20, top=70
+column-reverse, percentage child: height=160, content=20, top=70
+column-reverse, definite container: height=160, content=160, top=0
+column-reverse, definite basis: height=160, content=160, top=0
+column-reverse, flex button: height=160, content=160, top=0
+column-reverse, overflow: height=10, content=20, top=0
diff --git a/Tests/LibWeb/Text/input/HTML/flex-column-button-content-centering.html b/Tests/LibWeb/Text/input/HTML/flex-column-button-content-centering.html
new file mode 100644
index 00000000000..07f2cb2e3ea
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/flex-column-button-content-centering.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .container {
+ display: flex;
+ align-items: flex-start;
+ }
+ button {
+ min-height: 160px;
+ padding: 0;
+ border: 0;
+ }
+ .child {
+ height: 20px;
+ }
+</style>
+<div id="host"></div>
+<script>
+ test(() => {
+ const cases = [
+ ["automatic height", "", "", ""],
+ ["percentage child", "", "", "height: 100%"],
+ ["definite container", "height: 200px", "", "height: 100%"],
+ ["definite basis", "", "flex-basis: 160px", "height: 100%"],
+ ["flex button", "", "display: flex", ""],
+ ["overflow", "", "min-height: 0; max-height: 10px", ""],
+ ];
+ for (const direction of ["column", "column-reverse"]) {
+ for (const [name, containerStyle, buttonStyle, contentStyle] of cases) {
+ const container = document.createElement("div");
+ container.className = "container";
+ container.style.cssText = `flex-direction: ${direction}; ${containerStyle}`;
+ container.innerHTML = `<button style="${buttonStyle}"><div style="${contentStyle}"><div class="child"></div></div></button>`;
+ document.getElementById("host").appendChild(container);
+ const button = container.firstElementChild;
+ const content = button.firstElementChild;
+ const outer = button.getBoundingClientRect();
+ const inner = content.getBoundingClientRect();
+ println(`${direction}, ${name}: height=${outer.height}, content=${inner.height}, top=${inner.top - outer.top}`);
+ container.remove();
+ }
+ }
+ });
+</script>