Truly independent web browser
LibWeb: Size a primitive text input by its default preferred size
sideshowbarkera66f2c44f208
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rsindex c9213158614..5bee4bde767 100644--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs@@ -1925,11 +1925,12 @@ impl AbsposEngine<'_> {// https://drafts.csswg.org/css-position-3/#abspos-auto-size// Form controls use non-replaced sizing here, so their intrinsic dimensions don't prevent stretching. That covers- // checkboxes and radio buttons too, which pages lay over a styled box between zero insets to make it clickable.+ // checkboxes and radio buttons too, which pages lay over a styled box between zero insets to make it clickable,+ // and text inputs without their native appearance, which have a default preferred size instead.fn form_control_stretches(&self, sized: OutOfFlowBox<'_>, equation: AxisEquation, axis: SizingAxis) -> bool {let style = self.style_of(sized);let facts = self.facts(sized.node);- (facts.is_native_form_control_box() || facts.is_non_devolvable_widget())+ (facts.is_native_form_control_box() || facts.is_non_devolvable_widget() || facts.is_text_input())&& axis.select(style.width(), style.height()).is_auto()&& !self.sizes_to_fit_content(sized, equation, matches!(axis, SizingAxis::Inline))}diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rsindex 81d5db9e8a1..01a6331267d 100644--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs@@ -1254,6 +1254,14 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {}return true;}+ // https://drafts.csswg.org/css-forms-1/#valdef-field-sizing-fixed+ // For element with default preferred size, the UA must set the intrinsic size to the default preferred size+ // defined by the host language for that element.+ // NB: A form control without its native appearance has a default preferred size where its native box had a+ // natural size, and is sized from it the same way.+ if facts.default_preferred_width().is_some() {+ return true;+ }false}@@ -2414,6 +2422,9 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {.cells.update_cells(|cells| cells.has_definite_block_size = true);}+ // NB: A text input without its native appearance is sized from its default preferred size, and still+ // centers its content in a block size its min-height or max-height clamps.+ self.make_form_control_content_box_definite(sized_cells, available_space, constraints, None);return None;}@@ -3166,6 +3177,13 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {if let Some(width) = auto_size.width {return answered_from_style(width);}+ // https://drafts.csswg.org/css-forms-1/#valdef-field-sizing-fixed+ // For element with default preferred size, the UA must set the intrinsic size to the default preferred size+ // defined by the host language for that element.+ // NB: That holds for a text entry widget without its native appearance too, so a long value doesn't widen it.+ if let Some(width) = facts.default_preferred_width() {+ return answered_from_style(width);+ }if facts.is_replaced_box() && facts.has_preferred_aspect_ratio() && auto_size.height.is_some() {return self.max_content_inline_size_answered_without_measuring_content(sized,diff --git a/Tests/LibWeb/Text/expected/text-input-without-appearance-intrinsic-size.txt b/Tests/LibWeb/Text/expected/text-input-without-appearance-intrinsic-size.txtnew file mode 100644index 00000000000..9c8c66e6cf7--- /dev/null+++ b/Tests/LibWeb/Text/expected/text-input-without-appearance-intrinsic-size.txt@@ -0,0 +1,14 @@+default width is narrower than the container: true+grid: the container's width+grid, size=5: the container's width+flex row: the default width+flex row, flex: 1: the container's width+width: min-content: the default width+width: fit-content: the default width+float: the default width+password in grid: the container's width+search in grid: the container's width+abspos between insets: the container's width+abspos with one inset: the default width+display: block: the default width+field-sizing: content: wider than the default widthdiff --git a/Tests/LibWeb/Text/input/text-input-without-appearance-intrinsic-size.html b/Tests/LibWeb/Text/input/text-input-without-appearance-intrinsic-size.htmlnew file mode 100644index 00000000000..a2b1d90579c--- /dev/null+++ b/Tests/LibWeb/Text/input/text-input-without-appearance-intrinsic-size.html@@ -0,0 +1,61 @@+<!doctype html>+<script src="include.js"></script>+<style>+ input {+ font: 16px sans-serif;+ margin: 0;+ appearance: none;+ border: 1px solid;+ padding: 2px;+ }+ .container {+ width: 230px;+ }+</style>+<script>+ test(() => {+ const value = "averyveryverylongemailaddress.with.many.parts@example-company-domain.com";+ const reference = document.createElement("input");+ document.body.append(reference);+ const defaultWidth = reference.getBoundingClientRect().width;+ reference.remove();+ // NB: The default width depends on the font, so each width is described against it and the container's width.+ function describe(width, containerWidth) {+ if (width === defaultWidth)+ return "the default width";+ if (width === containerWidth)+ return "the container's width";+ return width > defaultWidth ? "wider than the default width" : "narrower than the default width";+ }+ const cases = [+ ["grid", "display: grid", ""],+ ["grid, size=5", "display: grid", "", 5],+ ["flex row", "display: flex", ""],+ ["flex row, flex: 1", "display: flex", "flex: 1"],+ ["width: min-content", "", "width: min-content"],+ ["width: fit-content", "width: 50px", "width: fit-content"],+ ["float", "width: 50px", "float: left"],+ ["password in grid", "display: grid", "", 0, "password"],+ ["search in grid", "display: grid", "", 0, "search"],+ ["abspos between insets", "position: relative; height: 50px", "position: absolute; inset: 0"],+ ["abspos with one inset", "position: relative; height: 50px", "position: absolute; left: 0"],+ ["display: block", "", "display: block"],+ ["field-sizing: content", "", "field-sizing: content"],+ ];+ println(`default width is narrower than the container: ${defaultWidth < 230}`);+ for (const [name, containerStyle, inputStyle, size, type] of cases) {+ const container = document.createElement("div");+ container.className = "container";+ container.style.cssText = containerStyle;+ const input = document.createElement("input");+ if (type) input.type = type;+ if (size) input.size = size;+ input.value = value;+ input.style.cssText = inputStyle;+ container.append(input);+ document.body.append(container);+ println(`${name}: ${describe(input.getBoundingClientRect().width, container.getBoundingClientRect().width)}`);+ container.remove();+ }+ });+</script>