Skip to content

Truly independent web browser

LibWeb: Keep a button’s baseline when its overflow is hidden

sideshowbarkera8cff13249e7
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index b0fcd465d67..d6e1d2c7ea5 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -694,9 +694,14 @@ pub(crate) fn box_baseline_with_content_baselines(
// baseline sets always derive from content; so do flex and grid containers, which are not block containers.
// FIXME: Per CSS Align, a scroll container's content-derived baseline position should be clamped to its border
// edge.
+ // AD-HOC: A box with button layout derives its baselines from its content even when its overflow makes it a
+ // scroll container, rather than taking its block-end margin edge, so that a button with overflow: hidden
+ // sits on the line by the text it shows, as pages expect.
+ // https://html.spec.whatwg.org/multipage/rendering.html#button-layout
let derive_baseline_from_content = baseline_set == BaselineSet::First
|| style.baseline_source() != crate::css::css_enums::baseline_source::AUTO
|| is_flex_or_grid_container
+ || facts.uses_button_layout()
|| !facts.is_scroll_container();
// AD-HOC: We also use the content-derived baseline for <input> elements with block children, even when their
@@ -712,7 +717,17 @@ pub(crate) fn box_baseline_with_content_baselines(
if let Some(content_baseline) = content_baseline
&& (derive_baseline_from_content || input_derives_from_children)
{
- return metrics.margin_box_top(collapsed) + content_baseline;
+ let baseline = metrics.margin_box_top(collapsed) + content_baseline;
+ // https://drafts.csswg.org/css-align-3/#baseline-export
+ // "if the position of a scroll container's first/last baseline is outside its border edge, that baseline's
+ // position is clamped to the border edge."
+ // NB: Only a button takes a scroll container's baseline from its content here.
+ if facts.uses_button_layout() && facts.is_scroll_container() {
+ let border_box_top = metrics.margin_top();
+ let border_box_bottom = border_box_top + metrics.border_box_block_size(collapsed);
+ return baseline.max(border_box_top).min(border_box_bottom);
+ }
+ return baseline;
}
// AD-HOC: Inline buttons without a content baseline synthesize one from their content-box bottom edge in
diff --git a/Tests/LibWeb/Layout/expected/button-with-overflow-hidden-baseline.txt b/Tests/LibWeb/Layout/expected/button-with-overflow-hidden-baseline.txt
new file mode 100644
index 00000000000..dc60eae3005
--- /dev/null
+++ b/Tests/LibWeb/Layout/expected/button-with-overflow-hidden-baseline.txt
@@ -0,0 +1,65 @@
+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 168 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [8,16] [8+0+0 784 0+0+8] [8+0+0 136 0+0+8] children: not-inline
+ BlockContainer <p> at [8,16] [0+0+0 784 0+0+0] [16+0+0 28 0+0+16] children: inline
+ frag 0 from TextNode start: 0, length: 9, rect: [8,22 80.484375x16] baseline: 13
+ "Read the "
+ frag 1 from BlockContainer start: 0, length: 0, rect: [88.484375,22 45.953125x16] baseline: 13
+ frag 2 from TextNode start: 0, length: 9, rect: [134.4375,22 75.6875x16] baseline: 13
+ " and the "
+ frag 3 from BlockContainer start: 0, length: 0, rect: [220.125,22 109.0625x16] baseline: 19
+ frag 4 from TextNode start: 0, length: 18, rect: [339.1875,22 151.34375x16] baseline: 13
+ " before you agree."
+ TextNode <#text> (not painted)
+ BlockContainer <button> at [88.484375,22] inline-block [0+0+0 45.953125 0+0+0] [0+0+0 16 0+0+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [88.484375,22] flex-container(column) [0+0+0 45.953125 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [88.484375,22] flex-item [0+0+0 45.953125 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [88.484375,22 45.953125x16] baseline: 13
+ "terms"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <button.padded> at [220.125,22] inline-block [0+2+8 109.0625 8+2+0] [0+2+4 16 4+2+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [220.125,22] flex-container(column) [0+0+0 109.0625 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [220.125,22] flex-item [0+0+0 109.0625 0+0+0] [0+0+0 16 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 14, rect: [220.125,22 109.0625x16] baseline: 13
+ "privacy policy"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <p> at [8,60] [0+0+0 784 0+0+0] [16+0+0 33 0+0+16] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [8,77 9.703125x16] baseline: 13
+ "x"
+ frag 1 from BlockContainer start: 0, length: 0, rect: [19.703125,62 36x26] baseline: 30
+ frag 2 from TextNode start: 0, length: 1, rect: [57.703125,77 7.171875x16] baseline: 13
+ "y"
+ TextNode <#text> (not painted)
+ BlockContainer <button.short> at [19.703125,62] inline-block [0+2+0 36 0+2+0] [0+2+0 26 0+2+0] [BFC] children: not-inline overflow: [19.703125,62 42.421875x80]
+ BlockContainer <(anonymous)> at [19.703125,62] flex-container(column) [0+0+0 36 0+0+0] [0+0+0 26 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [19.703125,62] flex-item [0+0+0 36 0+0+0] [0+0+0 26 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 3, rect: [23.359375,64 28.6875x16] baseline: 13
+ "one"
+ frag 1 from TextNode start: 4, length: 3, rect: [23.484375,84 28.4375x16] baseline: 13
+ "two"
+ frag 2 from TextNode start: 8, length: 5, rect: [19.703125,104 42.421875x16] baseline: 13
+ "three"
+ frag 3 from TextNode start: 14, length: 4, rect: [20,124 35.390625x16] baseline: 13
+ "four"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+ BlockContainer <p> at [8,109] [0+0+0 784 0+0+0] [16+0+0 43 0+0+16] children: inline
+ frag 0 from TextNode start: 0, length: 1, rect: [8,109 9.703125x16] baseline: 13
+ "x"
+ frag 1 from BlockContainer start: 0, length: 0, rect: [19.703125,124 48.65625x26] baseline: 0
+ frag 2 from TextNode start: 0, length: 1, rect: [70.359375,109 7.171875x16] baseline: 13
+ "y"
+ TextNode <#text> (not painted)
+ BlockContainer <button.raised> at [19.703125,124] inline-block [0+2+0 48.65625 0+2+0] [0+2+0 26 0+2+0] [BFC] children: not-inline
+ BlockContainer <(anonymous)> at [19.703125,124] flex-container(column) [0+0+0 48.65625 0+0+0] [0+0+0 26 0+0+0] [FFC] children: not-inline
+ BlockContainer <(anonymous)> at [19.703125,137] flex-item [0+0+0 48.65625 0+0+0] [0+0+0 0 0+0+0] [BFC] children: not-inline
+ BlockContainer <span> at [19.703125,97] [0+0+0 48.65625 0+0+0] [-40+0+0 16 0+0+0] children: inline
+ frag 0 from TextNode start: 0, length: 6, rect: [19.703125,97 48.65625x16] baseline: 13
+ "raised"
+ TextNode <#text> (not painted)
+ TextNode <#text> (not painted)
+
+SC for Viewport<#document> [0,0 800x600] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x168] (z-index: auto)
diff --git a/Tests/LibWeb/Layout/input/button-with-overflow-hidden-baseline.html b/Tests/LibWeb/Layout/input/button-with-overflow-hidden-baseline.html
new file mode 100644
index 00000000000..3ec7dc1c0e5
--- /dev/null
+++ b/Tests/LibWeb/Layout/input/button-with-overflow-hidden-baseline.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<style>
+ button { font: inherit; overflow: hidden; padding: 0; border: 0; background: none; text-decoration: underline; }
+ .padded { padding: 4px 8px; border: 2px solid; }
+ .short { height: 30px; width: 40px; line-height: 20px; border: 2px solid; }
+ .raised { height: 30px; border: 2px solid; }
+ .raised span { display: block; margin-top: -40px; }
+</style>
+<p>Read the <button>terms</button> and the <button class="padded">privacy policy</button> before you agree.</p>
+<p>x<button class="short">one two three four</button>y</p>
+<p>x<button class="raised"><span>raised</span></button>y</p>