Truly independent web browser
LibWeb: Size text controls to their content with field-sizing
Andreas Klinga78e8ea1396c
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rsindex c36b541f3c7..49cf662be7d 100644--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs@@ -401,6 +401,7 @@ scalar_accessors! {float_: u8 => float_,clear: u8 => clear,box_sizing: u8 => box_sizing,+ field_sizing: u8 => field_sizing,overflow_x: u8 => overflow_x,overflow_y: u8 => overflow_y,text_overflow: u8 => text_overflow,diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex 0362e8a81cd..a01dceb6bb0 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -1496,7 +1496,9 @@ fn dimension_block_level_root(/// min-height only clamps its block size once its content is laid out, so measuring first would just lay out the/// whole subtree an extra time — on every layout. Form controls center their content within the clamped size, though.fn lays_out_content_against_min_block_size(facts: NodeFacts<'_>, fc_type: FormattingContextType) -> bool {- fc_type != FormattingContextType::Block || facts.uses_button_layout() || facts.is_primitive_text_input()+ fc_type != FormattingContextType::Block+ || facts.uses_button_layout()+ || facts.lays_out_text_input_content_as_ordinary_box()}/// The content block size of a laid out box, for growing a block size transferred through its aspect ratio. Chromediff --git a/Libraries/LibWeb/Rust/src/layout/node_data.rs b/Libraries/LibWeb/Rust/src/layout/node_data.rsindex 676cac30bb7..bcc52e42640 100644--- a/Libraries/LibWeb/Rust/src/layout/node_data.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_data.rs@@ -69,6 +69,7 @@ pub struct FfiReplacedContentFacts {pub default_preferred_width: CssPixels,pub has_default_preferred_height: bool,pub default_preferred_height: CssPixels,+ pub text_entry_content_is_ordinary_box: bool,}impl From<crate::painting::host::FfiNaturalSize> for crate::css::style::NaturalSize {diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rsindex 3f606b123b6..bd324ee2017 100644--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs@@ -65,6 +65,7 @@ struct ReplacedContentStyle {line_height: CssPixels,is_horizontal: bool,appearance_is_none: bool,+ field_sizing_is_content: bool,}impl ReplacedContentStyle {@@ -98,6 +99,7 @@ impl ReplacedContentStyle {line_height: style.line_height(),is_horizontal: style.writing_mode() == crate::css::css_enums::writing_mode::HORIZONTAL_TB,appearance_is_none: style.appearance() == crate::css::css_enums::appearance::NONE,+ field_sizing_is_content: style_sizes_field_to_content(style),}}@@ -170,17 +172,19 @@ fn replaced_content_facts() -> FfiReplacedContentFacts {let mut facts = FfiReplacedContentFacts::default();set_auto_content_size(&mut facts, auto_content_size(kind, style, input));- if style.appearance_is_none- && let ReplacedContentInput::Input {- size,- is_text_entry: true,- } = input+ if let ReplacedContentInput::Input {+ size,+ is_text_entry: true,+ } = input{- let (width, height) = style.text_control_default_preferred_size(size);- facts.has_default_preferred_width = true;- facts.default_preferred_width = width;- facts.has_default_preferred_height = true;- facts.default_preferred_height = height;+ facts.text_entry_content_is_ordinary_box = style.appearance_is_none || style.field_sizing_is_content;+ if style.appearance_is_none && !style.field_sizing_is_content {+ let (width, height) = style.text_control_default_preferred_size(size);+ facts.has_default_preferred_width = true;+ facts.default_preferred_width = width;+ facts.has_default_preferred_height = true;+ facts.default_preferred_height = height;+ }}facts}@@ -217,6 +221,12 @@ fn auto_content_size(kind: NodeKind, style: ReplacedContentStyle, input: Replaceif let Some(explicit_size) = style.size_contained {return AutoContentSize::of_size(explicit_size);}+ // https://drafts.csswg.org/css-forms-1/#valdef-field-sizing-content+ // The UA must determine the element's intrinsic size based on its content, and must ignore any default preferred+ // size defined by the host language for that element.+ if style.field_sizing_is_content && matches!(kind, NodeKind::TextAreaBox | NodeKind::TextInputBox) {+ return AutoContentSize::default();+ }match kind {NodeKind::CheckBox => AutoContentSize::of_size((CssPixels::from_integer(13), CssPixels::from_integer(13))),NodeKind::RadioButton => AutoContentSize::of_size((CssPixels::from_integer(12), CssPixels::from_integer(12))),@@ -408,11 +418,20 @@ pub(crate) fn node_is_fragmented_inline(data: &impl NodeShape, style: Option<Com}pub(crate) fn node_has_auto_content_box_size(data: &NodeData) -> bool {- (kind_is_replaced_box(data.kind.get()) && data.kind.get() != NodeKind::AudioBox)- || matches!(- data.kind.get(),- NodeKind::RangeInputBox | NodeKind::TextAreaBox | NodeKind::TextInputBox- )+ match data.kind.get() {+ NodeKind::AudioBox => false,+ NodeKind::RangeInputBox => true,+ // A text control sized to its content lays out as the block container it is.+ NodeKind::TextAreaBox | NodeKind::TextInputBox => {+ !node_style_view(data).is_some_and(style_sizes_field_to_content)+ }+ kind => kind_is_replaced_box(kind),+ }+}++// https://drafts.csswg.org/css-forms-1/#field-sizing+fn style_sizes_field_to_content(style: ComputedValuesView<'_>) -> bool {+ style.field_sizing() == crate::css::css_enums::field_sizing::CONTENT}// https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context@@ -1107,14 +1126,15 @@ 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 that lays out its text control content as an ordinary box: one without its native+ /// appearance, or one sized to its content with `field-sizing: content`.+ pub(crate) fn lays_out_text_input_content_as_ordinary_box(&self) -> bool {+ self.is_html_input_element() && self.replaced_content().text_entry_content_is_ordinary_box}/// 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()+ self.data().kind.get() == NodeKind::TextInputBox || self.lays_out_text_input_content_as_ordinary_box()}pub(crate) fn is_fieldset_box(&self) -> bool {@@ -1415,6 +1435,7 @@ mod node_facts_tests {line_height: CssPixels::from_integer(16),is_horizontal: true,appearance_is_none: false,+ field_sizing_is_content: false,}}diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rsindex 02f2c63ea6f..d239cb0e7a6 100644--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs@@ -3040,10 +3040,10 @@ impl<'pass> SizingContext<'pass> {let facts = self.facts(node);// 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 {+ // NB: A text input that lays out its content as an ordinary box centers its content too, including content+ // that overflows a content box made shorter by a max-height.+ let text_input_content_is_ordinary_box = facts.lays_out_text_input_content_as_ordinary_box();+ if !facts.uses_button_layout() && !text_input_content_is_ordinary_box {return;}// Flex/grid-inside controls are their own flex/grid container, which aligns their content itself, so there is@@ -3055,7 +3055,8 @@ impl<'pass> SizingContext<'pass> {// 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. 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();+ let max_height_can_clamp_text_input_content =+ text_input_content_is_ordinary_box && !style.max_height().is_auto();if style.height().is_auto() && is_plain_auto(style.min_height()) && !max_height_can_clamp_text_input_content {return;}@@ -3104,7 +3105,7 @@ impl<'pass> SizingContext<'pass> {// 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 {+ let content_box_needs_to_be_definite = if text_input_content_is_ordinary_box {used_block_size != natural} else {used_block_size > naturaldiff --git a/Tests/LibWeb/Layout/expected/field-sizing-content-text-input-centers-text-in-min-height.txt b/Tests/LibWeb/Layout/expected/field-sizing-content-text-input-centers-text-in-min-height.txtnew file mode 100644index 00000000000..3d9346431f7--- /dev/null+++ b/Tests/LibWeb/Layout/expected/field-sizing-content-text-input-centers-text-in-min-height.txt@@ -0,0 +1,23 @@+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 104 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 88 0+0+8] children: inline+ frag 0 from TextInputBox start: 0, length: 0, rect: [11,10 20.796875x40] baseline: 28+ frag 1 from BlockContainer start: 0, length: 0, rect: [11,54 20.796875x40] baseline: 28+ TextInputBox <input> at [11,10] inline-block [0+1+2 20.796875 2+1+0] [0+1+1 40 1+1+0] [BFC] children: not-inline+ Box <div> at [11,10] flex-container(row) [0+0+0 20.796875 0+0+0] [0+0+0 40 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [11,24] flex-item [0+0+0 20.796875 0+0+0] [0+0+0 12 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 3, rect: [11,24 20.796875x12] baseline: 9.59375+ "abc"+ TextNode <#text> (not painted)+ BreakNode <br> (not painted)+ TextNode <#text> (not painted)+ BlockContainer <input> at [11,54] inline-block [0+1+2 20.796875 2+1+0] [0+1+1 40 1+1+0] [BFC] children: not-inline+ Box <div> at [11,54] flex-container(row) [0+0+0 20.796875 0+0+0] [0+0+0 40 0+0+0] [FFC] children: not-inline+ BlockContainer <div> at [11,68] flex-item [0+0+0 20.796875 0+0+0] [0+0+0 12 0+0+0] [BFC] children: inline+ frag 0 from TextNode start: 0, length: 3, rect: [11,68 20.796875x12] baseline: 9.59375+ "abc"+ TextNode <#text> (not painted)+ TextNode <#text> (not painted)++SC for Viewport<#document> [0,0 800x600] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x104] (z-index: auto)diff --git a/Tests/LibWeb/Layout/input/field-sizing-content-text-input-centers-text-in-min-height.html b/Tests/LibWeb/Layout/input/field-sizing-content-text-input-centers-text-in-min-height.htmlnew file mode 100644index 00000000000..548eda54dce--- /dev/null+++ b/Tests/LibWeb/Layout/input/field-sizing-content-text-input-centers-text-in-min-height.html@@ -0,0 +1,4 @@+<!DOCTYPE html>+<style>input { field-sizing: content; min-height: 40px; font: 12px SerenitySans; }</style>+<input value="abc"><br>+<input style="appearance: none" value="abc">diff --git a/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout-ref.htmlnew file mode 100644index 00000000000..19d977fee47--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout-ref.html@@ -0,0 +1,5 @@+<!DOCTYPE html>+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<body>+<input id="inputa" style="field-sizing: content" value="12345"><input>+</body>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout-ref.htmlnew file mode 100644index 00000000000..f7167ed4c31--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout-ref.html@@ -0,0 +1,5 @@+<!DOCTYPE html>+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<body>+<textarea style="field-sizing: content">12345</textarea><textarea></textarea>+</body>diff --git a/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout.html b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout.htmlnew file mode 100644index 00000000000..2e80b59973f--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<html class="reftest-wait">+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<link rel=match href="../../../../../expected/wpt-import/html/rendering/widgets/field-sizing-input-text-relayout-ref.html">+<script src="../../../common/reftest-wait.js"></script>+<body>+<input id="inputa" style="field-sizing: content" value=""><input>+<script>+requestAnimationFrame(() => {+ inputa.value = '';+ requestAnimationFrame(() => {+ inputa.value = '12345';+ takeScreenshot();+ });+});+</script>+</body>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout.html b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout.htmlnew file mode 100644index 00000000000..0f5beb660f0--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<html class="reftest-wait">+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<link rel=match href="../../../../../expected/wpt-import/html/rendering/widgets/field-sizing-textarea-relayout-ref.html">+<meta name="fuzzy" content="maxDifference=0-153; totalPixels=0-2" />+<script src="../../../common/reftest-wait.js"></script>+<body>+<textarea id="textarea" style="field-sizing: content"></textarea><textarea></textarea>+<script>+requestAnimationFrame(() => {+ textarea.value = '';+ requestAnimationFrame(() => {+ textarea.value = '12345';+ takeScreenshot();+ });+});+</script>+</body>+</html>diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-input-text.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-input-text.txtnew file mode 100644index 00000000000..a99d26037bf--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-input-text.txt@@ -0,0 +1,59 @@+Harness status: OK++Found 54 tests++54 Pass+Pass text: Empty value+Pass text: Empty value with a size atttribute+Pass text: Auto width and auto height with a long text+Pass text: Explicit width and heigth+Pass text: Explicit width and auto height+Pass text: Explicit height and auto width+Pass text: Text caret is taller than the placeholder+Pass text: Text caret is shorter than the placeholder+Pass text: Update field-sizing property dynamically+Pass search: Empty value+Pass search: Empty value with a size atttribute+Pass search: Auto width and auto height with a long text+Pass search: Explicit width and heigth+Pass search: Explicit width and auto height+Pass search: Explicit height and auto width+Pass search: Text caret is taller than the placeholder+Pass search: Text caret is shorter than the placeholder+Pass search: Update field-sizing property dynamically+Pass tel: Empty value+Pass tel: Empty value with a size atttribute+Pass tel: Auto width and auto height with a long text+Pass tel: Explicit width and heigth+Pass tel: Explicit width and auto height+Pass tel: Explicit height and auto width+Pass tel: Text caret is taller than the placeholder+Pass tel: Text caret is shorter than the placeholder+Pass tel: Update field-sizing property dynamically+Pass url: Empty value+Pass url: Empty value with a size atttribute+Pass url: Auto width and auto height with a long text+Pass url: Explicit width and heigth+Pass url: Explicit width and auto height+Pass url: Explicit height and auto width+Pass url: Text caret is taller than the placeholder+Pass url: Text caret is shorter than the placeholder+Pass url: Update field-sizing property dynamically+Pass email: Empty value+Pass email: Empty value with a size atttribute+Pass email: Auto width and auto height with a long text+Pass email: Explicit width and heigth+Pass email: Explicit width and auto height+Pass email: Explicit height and auto width+Pass email: Text caret is taller than the placeholder+Pass email: Text caret is shorter than the placeholder+Pass email: Update field-sizing property dynamically+Pass password: Empty value+Pass password: Empty value with a size atttribute+Pass password: Auto width and auto height with a long text+Pass password: Explicit width and heigth+Pass password: Explicit width and auto height+Pass password: Explicit height and auto width+Pass password: Text caret is taller than the placeholder+Pass password: Text caret is shorter than the placeholder+Pass password: Update field-sizing property dynamically\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-textarea.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-textarea.txtnew file mode 100644index 00000000000..24ffe15aaf6--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/widgets/field-sizing-textarea.txt@@ -0,0 +1,15 @@+Harness status: OK++Found 10 tests++10 Pass+Pass Empty value+Pass Empty value with cols/rows+Pass Disabled textarea with an empty value should have one-line height+Pass Auto width and auto height with a long text+Pass Explicit width and heigth+Pass Explicit width and auto height+Pass Explicit height and auto width+Pass Text caret is taller than the placeholder+Pass Text caret is shorter than the placeholder+Pass Update field-sizing property dynamically\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-input-text.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-input-text.htmlnew file mode 100644index 00000000000..e6809fe4e24--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-input-text.html@@ -0,0 +1,154 @@+<!DOCTYPE html>+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<style>+.disable-default {+ field-sizing: content;+}++.small-placeholder {+ font-size: 32px;+}+.small-placeholder::placeholder {+ font-size: 16px;+}++.large-placeholder {+ font-size: 20px;+}+.large-placeholder::placeholder {+ font-size: 40px;+}+</style>+<body>+<div id="container"></div>+<script>+const DISABLE = 'class="disable-default"';+const LONG_TEXT = 'The quick brown fox jumps over the lazy dog.';++function addElements(source) {+ const container = document.querySelector('#container');+ container.innerHTML = source + source;+ container.lastElementChild.classList.add('disable-default');+ return {+ fixed: container.firstElementChild,+ content: container.lastElementChild+ };+}++function addTwoElements(source1, source2) {+ const container = document.querySelector('#container');+ container.innerHTML = source1 + source2;+ return {+ e1: container.firstElementChild,+ e2: container.lastElementChild+ };+}++['text', 'search', 'tel', 'url', 'email', 'password'].forEach(type => {+ test(() => {+ let pair = addElements(`<input type=${type}>`);+ // A text <input> has approximately 20ch width by default.+ // A text <input> with field-sizing:content must be shorter than the default.+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addTwoElements(`<input type=${type} ${DISABLE}>`,+ `<input type=${type} ${DISABLE} value="foo">`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);+ }, `${type}: Empty value`);++ test(() => {+ let pair = addElements(`<input type=${type} size="10">`);+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+ }, `${type}: Empty value with a size atttribute`);++ test(() => {+ let pair = addElements(`<input type=${type} value="${LONG_TEXT}">`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ pair = addElements(`<input type=${type} placeholder="${LONG_TEXT}">`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ }, `${type}: Auto width and auto height with a long text`);++ test(() => {+ let pair = addElements(`<input type=${type} style="width:20ch; height:2lh">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="width:20ch; height:2lh" value="${LONG_TEXT}">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="width:20ch; height:2lh" placeholder="${LONG_TEXT}">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+ }, `${type}: Explicit width and heigth`);++ test(() => {+ let pair = addElements(`<input type=${type} style="width:20ch">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="width:20ch" value="${LONG_TEXT}">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="width:20ch" placeholder="${LONG_TEXT}">`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+ }, `${type}: Explicit width and auto height`);++ test(() => {+ let pair = addElements(`<input type=${type} style="height:2lh">`);+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="height:2lh" value="${LONG_TEXT}">`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<input type=${type} style="height:2lh" placeholder="${LONG_TEXT}">`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+ }, `${type}: Explicit height and auto width`);++ test(() => {+ let pair = addTwoElements(+ `<input type=${type} class="disable-default small-placeholder">`,+ `<input type=${type} class="disable-default small-placeholder" placeholder="foo bar">`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);+ }, `${type}: Text caret is taller than the placeholder`);++ test(() => {+ let pair = addTwoElements(+ `<input type=${type} class="disable-default large-placeholder">`,+ `<input type=${type} class="disable-default large-placeholder" placeholder="foo bar">`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);+ assert_less_than(pair.e1.offsetHeight, pair.e2.offsetHeight);++ pair = addTwoElements(+ `<input type=${type} class="disable-default" style="font-size:40px; padding:16px">`,+ `<input type=${type} class="disable-default large-placeholder"+ placeholder="foo bar" style="font-size:40px; padding:16px">`);+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);+ assert_equals(pair.e1.offsetTop, pair.e2.offsetTop);+ }, `${type}: Text caret is shorter than the placeholder`);++ test(() => {+ const container = document.querySelector('#container');+ container.innerHTML = `<input type=${type}>`;+ const element = container.firstChild;+ const w = element.offsetWidth;+ const h = element.offsetHeight;+ element.classList.add('disable-default');+ assert_less_than(element.offsetWidth, w);+ assert_equals(element.offsetHeight, h);+ }, `${type}: Update field-sizing property dynamically`);++});+</script>+</body>diff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-textarea.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-textarea.htmlnew file mode 100644index 00000000000..147bfad33cf--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/widgets/field-sizing-textarea.html@@ -0,0 +1,152 @@+<!DOCTYPE html>+<link rel="help" href="https://drafts.csswg.org/css-ui-4/#field-sizing">+<script src="../../../resources/testharness.js"></script>+<script src="../../../resources/testharnessreport.js"></script>+<style>+textarea {+ font-family: monospace;+}++.disable-default {+ field-sizing: content;+}++.small-placeholder {+ font-size: 32px;+}+.small-placeholder::placeholder {+ font-size: 16px;+}++.large-placeholder {+ font-size: 20px;+}+.large-placeholder::placeholder {+ font-size: 40px;+}+</style>+<body>+<div id="container"></div>+<script>+const DISABLE = 'class="disable-default"';+const LONG_TEXT = 'The quick brown fox jumps over the lazy dog.';++function addElements(source) {+ const container = document.querySelector('#container');+ container.innerHTML = source + source;+ container.lastElementChild.classList.add('disable-default');+ return {+ fixed: container.firstElementChild,+ content: container.lastElementChild+ };+}++function addTwoElements(source1, source2) {+ const container = document.querySelector('#container');+ container.innerHTML = source1 + source2;+ return {+ e1: container.firstElementChild,+ e2: container.lastElementChild+ };+}++test(() => {+ let pair = addElements('<textarea></textarea>');+ // Historically a <textarea> has approximately 20ch x 2lh size by default.+ // A <textarea> with field-sizing:content must be samller than the default.+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addTwoElements(`<textarea ${DISABLE}></textarea>`, `<textarea ${DISABLE}>foo</textarea>`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);+}, 'Empty value');++test(() => {+ let pair = addElements('<textarea cols="10" rows="3"></textarea>');+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);+}, 'Empty value with cols/rows');++test(() => {+ let pair = addTwoElements(`<textarea ${DISABLE} disabled></textarea>`,+ `<textarea ${DISABLE}></textarea>`);+ assert_equals(pair.e1.offsetWidth, pair.e2.offsetWidth, 'width');+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight, 'height');+}, 'Disabled textarea with an empty value should have one-line height');++test(() => {+ let pair = addElements(`<textarea>${LONG_TEXT}</textarea>`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ pair = addElements(`<textarea placeholder="${LONG_TEXT}"></textarea>`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+}, 'Auto width and auto height with a long text');++test(() => {+ let pair = addElements('<textarea style="width:20ch; height:2lh"></textarea>');+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="width:20ch; height:2lh">${LONG_TEXT}</textarea>`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="width:20ch; height:2lh" placeholder="${LONG_TEXT}"></textarea>`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+}, 'Explicit width and heigth');++test(() => {+ let pair = addElements('<textarea style="width:20ch"></textarea>');+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_less_than(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="width:20ch">${LONG_TEXT}</textarea>`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_greater_than(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="width:20ch" placeholder="${LONG_TEXT}"></textarea>`);+ assert_equals(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_greater_than(pair.content.offsetHeight, pair.fixed.offsetHeight);+}, 'Explicit width and auto height');++test(() => {+ let pair = addElements('<textarea style="height:2lh"></textarea>');+ assert_less_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="height:2lh">${LONG_TEXT}</textarea>`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);++ pair = addElements(`<textarea style="height:2lh" placeholder="${LONG_TEXT}"></textarea>`);+ assert_greater_than(pair.content.offsetWidth, pair.fixed.offsetWidth);+ assert_equals(pair.content.offsetHeight, pair.fixed.offsetHeight);+}, 'Explicit height and auto width');++test(() => {+ let pair = addTwoElements(`<textarea class="disable-default small-placeholder"></textarea>`,+ `<textarea class="disable-default small-placeholder" placeholder="foo"></textarea>`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth);+ assert_equals(pair.e1.offsetHeight, pair.e2.offsetHeight);+}, 'Text caret is taller than the placeholder');++test(() => {+ let pair = addTwoElements(`<textarea class="disable-default large-placeholder"></textarea>`,+ `<textarea class="disable-default large-placeholder" placeholder="foo"></textarea>`);+ assert_less_than(pair.e1.offsetWidth, pair.e2.offsetWidth, 'width');+ assert_less_than(pair.e1.offsetHeight, pair.e2.offsetHeight, 'height');+}, 'Text caret is shorter than the placeholder');++test(() => {+ const container = document.querySelector('#container');+ container.innerHTML = '<textarea></textarea>';+ const element = container.firstChild;+ const w = element.offsetWidth;+ const h = element.offsetHeight;+ element.classList.add('disable-default');+ assert_less_than(element.offsetWidth, w);+ assert_less_than(element.offsetHeight, h);+}, 'Update field-sizing property dynamically');+</script>+</body>