Skip to content

Truly independent web browser

LibWeb: Center text input content within a clamped automatic height

Aliaksandr Kalenika53d70cb2d2c
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 7305d33768a..2471b72447d 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1870,8 +1870,13 @@ impl<'pass> AbsposEngine<'pass> {
}
}
- self.sizing()
- .make_button_content_box_definite(node, LayoutMode::Normal, available_space, constraints, None);
+ self.sizing().make_form_control_content_box_definite(
+ node,
+ LayoutMode::Normal,
+ available_space,
+ constraints,
+ None,
+ );
}
pub(crate) fn finalize_out_of_flow_root_after_inside_layout(
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 47d6e45b94f..6a191c08f91 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -3313,8 +3313,11 @@ impl<'pass> FlexFormattingContext<'pass> {
// 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 centers its content within its used block size, so it keeps it definite.
- if !self.flex_item_is_stretched(index) && !self.facts(self.flex_items[index].box_).uses_button_layout()
+ // 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;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index dc555fea7fd..8f26983dff0 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1432,7 +1432,7 @@ fn dimension_block_level_root(run: &FormattingContextRun, input: &LayoutInput) -
body_input.available_space.block_size = AvailableSize::definite(min_block_size);
}
}
- sizing.make_button_content_box_definite(
+ sizing.make_form_control_content_box_definite(
node,
run.layout_mode,
available_space,
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index 09a9457b996..c32e7f73292 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -1067,6 +1067,16 @@ impl<'pass> NodeFacts<'pass> {
has_flag(self.data(), NodeFlag::IsHtmlInputElement)
}
+ /// A text entry input with `appearance: none`, which lays out its text control content as an ordinary box.
+ pub(crate) fn is_primitive_text_input(&self) -> bool {
+ self.is_html_input_element() && self.has_default_preferred_height()
+ }
+
+ /// A text entry input, with or without its native appearance.
+ pub(crate) fn is_text_input(&self) -> bool {
+ self.data().kind.get() == NodeKind::TextInputBox || self.is_primitive_text_input()
+ }
+
pub(crate) fn is_fieldset_box(&self) -> bool {
self.data().kind.get() == NodeKind::FieldSetBox
}
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 09b92332088..c0dc39d6b70 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -1756,7 +1756,7 @@ impl<'pass> SizingContext<'pass> {
block_size: AvailableSize::Indefinite,
};
self.resolve_used_block_size_if_not_treated_as_auto(node, inline_definite_space, constraints);
- self.make_button_content_box_definite(node, layout_mode, available_space, constraints, None);
+ self.make_form_control_content_box_definite(node, layout_mode, available_space, constraints, None);
sizing_repeats_for_available_inline_sizes_at_or_above
}
@@ -2808,7 +2808,9 @@ impl<'pass> SizingContext<'pass> {
.automatic_content_block_size
}
- pub(crate) fn make_button_content_box_definite(
+ /// Buttons and text inputs center their content within their content box, which therefore has to be definite
+ /// before the content is laid out, also where a min or max block size decides an automatic block size.
+ pub(crate) fn make_form_control_content_box_definite(
&self,
node: Node,
layout_mode: LayoutMode,
@@ -2817,18 +2819,25 @@ impl<'pass> SizingContext<'pass> {
measured_content_block_size: Option<CssPixels>,
) {
let facts = self.facts(node);
- if !facts.uses_button_layout() {
+ // https://drafts.csswg.org/css-ui-4/#input-rules
+ // The content is vertically centered
+ // NB: A text input without its native appearance centers its content too, including content that overflows a
+ // content box made shorter by a max-height.
+ let is_primitive_text_input = facts.is_primitive_text_input();
+ if !facts.uses_button_layout() && !is_primitive_text_input {
return;
}
- // Flex/grid-inside buttons are their own flex/grid container and get no anonymous content wrapper,
- // so there is nothing to make definite for centering.
+ // Flex/grid-inside controls are their own flex/grid container, which aligns their content itself, so there is
+ // nothing to make definite for centering.
let style = self.style(node);
if style.display().is_flex_inside() || style.display().is_grid_inside() {
return;
}
// With auto height and no min-height the content box already exactly wraps the content, so there is
- // no extra space to center within and no need to force a definite content box.
- if style.height().is_auto() && style.min_height().is_auto() {
+ // no extra space to center within and no need to force a definite content box. A text input's max-height can
+ // still make its content box shorter than its content.
+ let max_height_can_clamp_text_input_content = is_primitive_text_input && !style.max_height().is_auto();
+ if style.height().is_auto() && style.min_height().is_auto() && !max_height_can_clamp_text_input_content {
return;
}
if self.used(node).has_definite_block_size() {
@@ -2872,17 +2881,23 @@ impl<'pass> SizingContext<'pass> {
constraints,
));
}
- // Only force a definite content box when the button's used block size exceeds its content block size, so a larger
- // preferred or minimum size has room to center within. A content-sized box stays indefinite, so an intrinsic
- // keyword does not resolve percentage-sized descendants.
- if used_block_size <= natural {
+ // Only force a definite content box when a button's used block size exceeds its content block size, so a larger
+ // preferred or minimum size has room to center within, or when a text input's differs from it in either
+ // direction. A content-sized box stays indefinite, so an intrinsic keyword does not resolve percentage-sized
+ // descendants.
+ let content_box_needs_to_be_definite = if is_primitive_text_input {
+ used_block_size != natural
+ } else {
+ used_block_size > natural
+ };
+ if !content_box_needs_to_be_definite {
return;
}
let used = self.used(node);
used.set_content_block_size(used_block_size);
used.has_definite_block_size.set(true);
used.has_definite_block_size_only_for_button_content_alignment
- .set(block_size_is_automatic);
+ .set(block_size_is_automatic && facts.uses_button_layout());
}
pub(crate) fn table_box_inside_wrapper(&self, wrapper: Node) -> Node {
diff --git a/Tests/LibWeb/Ref/expected/text-input-centers-text-in-clamped-automatic-height-ref.html b/Tests/LibWeb/Ref/expected/text-input-centers-text-in-clamped-automatic-height-ref.html
new file mode 100644
index 00000000000..75c2651ff79
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/text-input-centers-text-in-clamped-automatic-height-ref.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<style>
+ body {
+ margin: 0;
+ font-size: 0;
+ }
+ .row {
+ display: flex;
+ align-items: flex-start;
+ gap: 8px;
+ margin-bottom: 8px;
+ }
+ .block-wrapper {
+ display: block;
+ width: 120px;
+ overflow: hidden;
+ line-height: normal;
+ background: lightblue;
+ }
+ input {
+ box-sizing: border-box;
+ width: 120px;
+ margin: 0;
+ border: 0;
+ font: 14px sans-serif;
+ background: pink;
+ }
+ .primitive {
+ appearance: none;
+ }
+ .shrunk-by-max-height {
+ padding: 16px 0;
+ line-height: 48px;
+ height: 48px;
+ }
+ .grown-by-min-height {
+ padding: 4px 0;
+ line-height: normal;
+ height: 40px;
+ }
+</style>
+<div class="row">
+ <span class="block-wrapper"><input class="primitive shrunk-by-max-height" value="Xyz"></span>
+ <input class="primitive shrunk-by-max-height" value="Xyz">
+ <input class="shrunk-by-max-height" value="Xyz">
+</div>
+<div class="row">
+ <span class="block-wrapper"><input class="primitive grown-by-min-height" value="Xyz"></span>
+ <input class="primitive grown-by-min-height" value="Xyz">
+ <input class="grown-by-min-height" value="Xyz">
+</div>
diff --git a/Tests/LibWeb/Ref/input/text-input-centers-text-in-clamped-automatic-height.html b/Tests/LibWeb/Ref/input/text-input-centers-text-in-clamped-automatic-height.html
new file mode 100644
index 00000000000..5d67fe9a396
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/text-input-centers-text-in-clamped-automatic-height.html
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/text-input-centers-text-in-clamped-automatic-height-ref.html">
+<style>
+ body {
+ margin: 0;
+ font-size: 0;
+ }
+ .row {
+ display: flex;
+ align-items: flex-start;
+ gap: 8px;
+ margin-bottom: 8px;
+ }
+ .block-wrapper {
+ display: block;
+ width: 120px;
+ overflow: hidden;
+ line-height: normal;
+ background: lightblue;
+ }
+ input {
+ box-sizing: border-box;
+ width: 120px;
+ margin: 0;
+ border: 0;
+ font: 14px sans-serif;
+ background: pink;
+ height: auto;
+ }
+ .primitive {
+ appearance: none;
+ }
+ .shrunk-by-max-height {
+ padding: 16px 0;
+ line-height: 48px;
+ min-height: 48px;
+ max-height: 48px;
+ }
+ .grown-by-min-height {
+ padding: 4px 0;
+ line-height: normal;
+ min-height: 40px;
+ }
+</style>
+<div class="row">
+ <span class="block-wrapper"><input class="primitive shrunk-by-max-height" value="Xyz"></span>
+ <input class="primitive shrunk-by-max-height" value="Xyz">
+ <input class="shrunk-by-max-height" value="Xyz">
+</div>
+<div class="row">
+ <span class="block-wrapper"><input class="primitive grown-by-min-height" value="Xyz"></span>
+ <input class="primitive grown-by-min-height" value="Xyz">
+ <input class="grown-by-min-height" value="Xyz">
+</div>