Truly independent web browser
LibWeb: Size number inputs from their bounds and step
sideshowbarkerb63d06063bd7
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cppindex 952c190f084..f2ec309f865 100644--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp@@ -1930,7 +1930,8 @@ static void publish_element_replaced_content_input(DOM::Element& element, StyleEdefault:break;}- set_replaced_content_input(style_engine, node, kind, 0, input->size());+ auto number_size = input->number_input_size_in_characters();+ set_replaced_content_input(style_engine, node, kind, 0, number_size.value_or(input->size()), number_size.has_value());return;}if (auto const* video = as_if<HTML::HTMLVideoElement>(element)) {@@ -3547,7 +3548,7 @@ void record_element_attribute_changed(DOM::Element& element, Utf16FlyString cons// What the replaced content of a form control or a canvas is sized from.if ((is<HTML::HTMLTextAreaElement>(element) && (name == HTML::AttributeNames::cols || name == HTML::AttributeNames::rows))- || (is<HTML::HTMLInputElement>(element) && (name == HTML::AttributeNames::size || name == HTML::AttributeNames::type))+ || (is<HTML::HTMLInputElement>(element) && first_is_one_of(name, HTML::AttributeNames::size, HTML::AttributeNames::type, HTML::AttributeNames::min, HTML::AttributeNames::max, HTML::AttributeNames::step))|| (is<HTML::HTMLCanvasElement>(element) && (name == HTML::AttributeNames::width || name == HTML::AttributeNames::height)))record_element_replaced_content_input(element);// A table's border attribute moves its cells' border hints.diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cppindex 8a0992d9582..78358ab4708 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp@@ -1890,6 +1890,8 @@ void HTMLInputElement::form_associated_element_attribute_changed(Utf16FlyString}update_shadow_tree();} else if (name == HTML::AttributeNames::min || name == HTML::AttributeNames::max || name == HTML::AttributeNames::step) {+ if (type_state() == TypeAttributeState::Number && old_value != value)+ set_needs_layout_update(DOM::SetNeedsLayoutReason::DefaultPreferredSizeAttributeChange);if (type_state() == TypeAttributeState::Range) {m_value = value_sanitization_algorithm(m_value);update_shadow_tree();@@ -2884,6 +2886,61 @@ WebIDL::UnsignedLong HTMLInputElement::size() constreturn 20;}+Optional<u32> HTMLInputElement::number_input_size_in_characters() const+{+ if (type_state() != TypeAttributeState::Number)+ return {};+ if (auto explicit_size = get_attribute(AttributeNames::size); explicit_size.has_value()) {+ auto parsed_size = parse_non_negative_integer(*explicit_size);+ if (parsed_size.has_value() && *parsed_size > 0 && *parsed_size <= 2147483647)+ return *parsed_size;+ }++ auto minimum = get_attribute(AttributeNames::min);+ auto maximum = get_attribute(AttributeNames::max);+ auto step = get_attribute(AttributeNames::step);+ if (!minimum.has_value() || !maximum.has_value() || (step.has_value() && step->equals_ignoring_ascii_case(u"any"sv)))+ return {};++ // An invalid step, e.g. "garbage", falls back to the default step rather than reading as an exponent form.+ auto uses_exponent = [](Utf16View text) {+ return is_valid_floating_point_number(text) && (text.contains(u'e') || text.contains(u'E'));+ };+ if (uses_exponent(*minimum) || uses_exponent(*maximum) || (step.has_value() && uses_exponent(*step)))+ return {};++ u32 integer_columns = 1;+ u32 fractional_columns = 0;+ auto include_number = [&](Utf16View text) {+ if (!is_valid_floating_point_number(text))+ return false;+ auto value = parse_floating_point_number(text);+ if (!value.has_value() || !isfinite(*value))+ return false;+ // Zero takes one column, however it's written, e.g. -0 or 0.000.+ if (*value == 0)+ return true;++ auto negative = text.starts_with(u'-');+ if (negative)+ text = text.substring_view(1);+ auto point = text.find_code_unit_offset(u'.').value_or(text.length_in_code_units());+ auto integer = text.substring_view(0, point).trim("0"sv, TrimMode::Left);+ auto fraction = point < text.length_in_code_units() ? text.substring_view(point + 1) : Utf16View {};++ auto integer_digits = AK::max<size_t>(1, integer.length_in_code_units());+ integer_columns = AK::max(integer_columns, static_cast<u32>(integer_digits) + (negative ? 1u : 0u));+ fractional_columns = AK::max(fractional_columns, static_cast<u32>(fraction.length_in_code_units()));+ return true;+ };++ if (!include_number(*minimum) || !include_number(*maximum))+ return {};+ if (!step.has_value() || !include_number(*step))+ include_number(u"1"sv);+ return integer_columns + fractional_columns + (fractional_columns > 0 ? 1u : 0u);+}+WebIDL::ExceptionOr<void> HTMLInputElement::set_size(WebIDL::UnsignedLong value){if (value == 0)diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.hindex af0c0712b08..2b2c81a88d1 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.h+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h@@ -166,6 +166,7 @@ public:WebIDL::ExceptionOr<void> set_min_length(WebIDL::Long);WebIDL::UnsignedLong size() const;+ Optional<u32> number_input_size_in_characters() const;WebIDL::ExceptionOr<void> set_size(WebIDL::UnsignedLong value);WebIDL::UnsignedLong height() const;diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex 5ed6a816b00..e9d1370b873 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -880,7 +880,8 @@ pub enum FfiReplacedContentInputKind {TextArea = 1,/// An `<input>` whose type makes it no text entry widget: `first` is its `size`.Input = 2,- /// An `<input>` whose type makes it a text entry widget: `first` is its `size`.+ /// A text entry `<input>`: `first` is its preferred character count. For a number input, a nonzero+ /// `second` reserves its stepper beside an explicit or range-derived count.TextEntryInput = 3,/// A `<canvas>`: `first` is its `width`, and `second` its `height`.Canvas = 4,diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rsindex d83b1b3bd69..973d835136a 100644--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs@@ -28,9 +28,13 @@ pub enum ReplacedContentInput {None,/// A `<textarea>`'s `cols` and `rows`: its natural size is that many `ch` by that many `lh`.TextArea { cols: u32, rows: u32 },- /// An `<input>`'s `size`, and whether its type makes it a text entry widget, whose default- /// preferred size is that many `ch` by one line.- Input { size: u32, is_text_entry: bool },+ /// An `<input>`'s preferred character count and whether its type makes it a text entry widget.+ /// Number inputs with a range-derived or explicit count reserve space for their stepper too.+ Input {+ size: u32,+ is_text_entry: bool,+ include_number_stepper: bool,+ },/// A `<canvas>`'s `width` and `height`, its natural size in CSS pixels.Canvas { width: u32, height: u32 },/// The natural size of what an element has loaded, such as a video's or an image's.@@ -69,6 +73,7 @@ impl ReplacedContentInput {kind if kind == Kind::Input as u8 || kind == Kind::TextEntryInput as u8 => Self::Input {size: values[0],is_text_entry: kind == Kind::TextEntryInput as u8,+ include_number_stepper: values[1] != 0,},kind if kind == Kind::Canvas as u8 => Self::Canvas {width: values[0],diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/fork_tests.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/fork_tests.rsindex b7b87a9262a..8e7bfc0a684 100644--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/fork_tests.rs+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/fork_tests.rs@@ -38,6 +38,7 @@ fn styled_document(count: usize) -> StyleEngine {inputs::ReplacedContentInput::Input {size: 20,is_text_entry: true,+ include_number_stepper: false,},);}diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rsindex 50dd6ece58e..fc74f762856 100644--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs@@ -203,9 +203,11 @@ fn replaced_content_facts(if let ReplacedContentInput::Input {size,is_text_entry: true,+ include_number_stepper,} = input{facts.text_entry_content_is_ordinary_box = style.appearance_is_none || style.field_sizing_is_content;+ facts.include_number_stepper = include_number_stepper && !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.default_preferred_width = Some(width);@@ -222,6 +224,7 @@ pub(crate) struct ReplacedContentFacts {pub(crate) default_preferred_width: Option<CssPixels>,pub(crate) default_preferred_height: Option<CssPixels>,pub(crate) text_entry_content_is_ordinary_box: bool,+ pub(crate) include_number_stepper: bool,/// The default object size of an SVG `<image>`, which applies once something has decoded. The SVG formatting/// context reads it as it is: it is no preferred size the sizing of an ordinary replaced box would take.pub(crate) svg_image_default_object_size: CssPixelSize,@@ -972,9 +975,67 @@ impl<'pass> NodeFacts<'pass> {if self.data().kind.get() == NodeKind::SVGSVGBox {return self.svg_root_replaced_content();}- self.pass+ let mut facts = self+ .pass.replaced_content_facts(self.node)- .expect("replaced content facts were not synced to the arena before layout")+ .expect("replaced content facts were not synced to the arena before layout");+ if facts.include_number_stepper && !self.node_has_size_containment() {+ let decoration = self.number_stepper_inline_size();+ if self+ .computed_values_view_if_styled()+ .is_some_and(|style| style.writing_mode() == crate::css::css_enums::writing_mode::HORIZONTAL_TB)+ {+ facts.auto_content_size.width = facts.auto_content_size.width.map(|width| width + decoration);+ facts.default_preferred_width = facts.default_preferred_width.map(|width| width + decoration);+ } else {+ facts.auto_content_size.height = facts.auto_content_size.height.map(|height| height + decoration);+ facts.default_preferred_height = facts.default_preferred_height.map(|height| height + decoration);+ }+ }+ facts+ }++ fn number_stepper_inline_size(&self) -> CssPixels {+ let zero = CssPixels::default();+ let first_child = |node| {+ if node == Node::INVALID {+ Node::INVALID+ } else {+ self.pass.node_data(node).first_child.get()+ }+ };+ let wrapper = first_child(self.node);+ if wrapper == Node::INVALID {+ return zero;+ }+ // The number input's shadow wrapper ends with the stepper container. Its buttons contain fixed-size SVG+ // arrows; use their computed dimensions and padding so the decoration follows its own font and zoom.+ let stepper = self.pass.node_data(wrapper).last_child.get();+ let button = first_child(stepper);+ let arrow = first_child(button);+ if arrow == Node::INVALID || self.pass.node_data(arrow).kind.get() != NodeKind::SVGSVGBox {+ return zero;+ }+ let (Some(arrow_style), Some(button_style), Some(input_style)) = (+ self.pass.computed_values_view_if_styled(arrow),+ self.pass.computed_values_view_if_styled(button),+ self.computed_values_view_if_styled(),+ ) else {+ return zero;+ };+ if input_style.writing_mode() == crate::css::css_enums::writing_mode::HORIZONTAL_TB {+ arrow_style.width().to_px(zero)+ + button_style.padding_left().to_px(zero)+ + button_style.padding_right().to_px(zero)+ + button_style.border_left_width()+ + button_style.border_right_width()+ } else {+ arrow_style.height().to_px(zero)+ + button_style.padding_top().to_px(zero)+ + button_style.padding_bottom().to_px(zero)+ + button_style.border_top_width()+ + button_style.border_bottom_width()+ }}/// An <svg> root's natural size resolves the lengths its element published against its style and the@@ -1584,6 +1645,7 @@ mod node_facts_tests {let input = ReplacedContentInput::Input {size: 20,is_text_entry: true,+ include_number_stepper: false,};let text_input = super::replaced_content_facts(NodeKind::TextInputBox, horizontal_style(), input);assert_eq!(auto_content_size(text_input), Some((160, 16)));@@ -1601,6 +1663,7 @@ mod node_facts_tests {let checkbox_input = ReplacedContentInput::Input {size: 20,is_text_entry: false,+ include_number_stepper: false,};let checkbox = super::replaced_content_facts(NodeKind::BlockContainer, primitive_style, checkbox_input);assert!(checkbox.default_preferred_width.is_none());diff --git a/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/input-number-text-size-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/input-number-text-size-ref.htmlnew file mode 100644index 00000000000..6c831941f08--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/html/rendering/widgets/input-number-text-size-ref.html@@ -0,0 +1,10 @@+<!DOCTYPE html>+<input type=number><br>+<input type=number><br>+<input type=number><br>+<input type=number size=10><br>+<input type=number><br>+<input type=number size=1><br>+<input type=number size=2><br>+<input type=number size=3><br>+<input type=number size=4><br>diff --git a/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/input-number-text-size.tentative.html b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/input-number-text-size.tentative.htmlnew file mode 100644index 00000000000..51cf6c66877--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/html/rendering/widgets/input-number-text-size.tentative.html@@ -0,0 +1,14 @@+<!DOCTYPE html>+<link rel="help" href="https://html.spec.whatwg.org/C/#the-input-element-as-a-text-entry-widget">+<link rel="help" href="https://github.com/whatwg/html/issues/10390">+<title>Test `size` attribute behavior on number input</title>+<link rel="match" href="../../../../../expected/wpt-import/html/rendering/widgets/input-number-text-size-ref.html">+<input type=number><br>+<input type=number min=0><br>+<input type=number max=1><br>+<input type=number size=10 min=0 max=1><br>+<input type=number min=0 max=1 step=any><br>+<input type=number min=0 max=1><br>+<input type=number min=-1 max=1><br>+<input type=number min=0 max=1 step=0.5><br>+<input type=number min=-1 max=1 step=0.5><br>diff --git a/Tests/LibWeb/Text/expected/number-input-range-width.txt b/Tests/LibWeb/Text/expected/number-input-range-width.txtnew file mode 100644index 00000000000..5544de49024--- /dev/null+++ b/Tests/LibWeb/Text/expected/number-input-range-width.txt@@ -0,0 +1,160 @@+PASS: auto bounded input is narrower+PASS: auto rgb+PASS: auto rgb keeps size IDL+PASS: auto negative bound+PASS: auto negative bound keeps size IDL+PASS: auto fraction and sign+PASS: auto fraction and sign keeps size IDL+PASS: auto decimal trailing zero+PASS: auto decimal trailing zero keeps size IDL+PASS: auto positive exponent+PASS: auto positive exponent keeps size IDL+PASS: auto decimal exponent+PASS: auto decimal exponent keeps size IDL+PASS: auto fractional step+PASS: auto fractional step keeps size IDL+PASS: auto exponent step+PASS: auto exponent step keeps size IDL+PASS: auto step trailing zeros+PASS: auto step trailing zeros keeps size IDL+PASS: auto negative step+PASS: auto negative step keeps size IDL+PASS: auto zero step+PASS: auto zero step keeps size IDL+PASS: auto zero step with fraction digits+PASS: auto zero step with fraction digits keeps size IDL+PASS: auto invalid step+PASS: auto invalid step keeps size IDL+PASS: auto invalid step containing an e+PASS: auto invalid step containing an e keeps size IDL+PASS: auto negative zero+PASS: auto negative zero keeps size IDL+PASS: auto long integer+PASS: auto long integer keeps size IDL+PASS: auto long fractional prefix+PASS: auto long fractional prefix keeps size IDL+PASS: auto tiny exponent+PASS: auto tiny exponent keeps size IDL+PASS: auto exponent underflow+PASS: auto exponent underflow keeps size IDL+PASS: auto exponent min+PASS: auto exponent min keeps size IDL+PASS: auto uppercase exponent min+PASS: auto uppercase exponent min keeps size IDL+PASS: auto uppercase exponent max+PASS: auto uppercase exponent max keeps size IDL+PASS: auto uppercase exponent step+PASS: auto uppercase exponent step keeps size IDL+PASS: auto leading integer zeros+PASS: auto leading integer zeros keeps size IDL+PASS: auto signed leading zeros+PASS: auto signed leading zeros keeps size IDL+PASS: auto zero fraction+PASS: auto zero fraction keeps size IDL+PASS: auto leading decimal point+PASS: auto leading decimal point keeps size IDL+PASS: auto default+PASS: auto default keeps size IDL+PASS: auto only min+PASS: auto only min keeps size IDL+PASS: auto only max+PASS: auto only max keeps size IDL+PASS: auto invalid min+PASS: auto invalid min keeps size IDL+PASS: auto invalid max+PASS: auto invalid max keeps size IDL+PASS: auto overflow max+PASS: auto overflow max keeps size IDL+PASS: auto any step+PASS: auto any step keeps size IDL+PASS: auto size overrides range with step 0.001+PASS: auto size overrides range with step 1e-3+PASS: auto set bounds+PASS: auto change step+PASS: auto change max+PASS: auto change min+PASS: auto remove min+PASS: auto remove max+PASS: auto remove step+PASS: none bounded input is narrower+PASS: none rgb+PASS: none rgb keeps size IDL+PASS: none negative bound+PASS: none negative bound keeps size IDL+PASS: none fraction and sign+PASS: none fraction and sign keeps size IDL+PASS: none decimal trailing zero+PASS: none decimal trailing zero keeps size IDL+PASS: none positive exponent+PASS: none positive exponent keeps size IDL+PASS: none decimal exponent+PASS: none decimal exponent keeps size IDL+PASS: none fractional step+PASS: none fractional step keeps size IDL+PASS: none exponent step+PASS: none exponent step keeps size IDL+PASS: none step trailing zeros+PASS: none step trailing zeros keeps size IDL+PASS: none negative step+PASS: none negative step keeps size IDL+PASS: none zero step+PASS: none zero step keeps size IDL+PASS: none zero step with fraction digits+PASS: none zero step with fraction digits keeps size IDL+PASS: none invalid step+PASS: none invalid step keeps size IDL+PASS: none invalid step containing an e+PASS: none invalid step containing an e keeps size IDL+PASS: none negative zero+PASS: none negative zero keeps size IDL+PASS: none long integer+PASS: none long integer keeps size IDL+PASS: none long fractional prefix+PASS: none long fractional prefix keeps size IDL+PASS: none tiny exponent+PASS: none tiny exponent keeps size IDL+PASS: none exponent underflow+PASS: none exponent underflow keeps size IDL+PASS: none exponent min+PASS: none exponent min keeps size IDL+PASS: none uppercase exponent min+PASS: none uppercase exponent min keeps size IDL+PASS: none uppercase exponent max+PASS: none uppercase exponent max keeps size IDL+PASS: none uppercase exponent step+PASS: none uppercase exponent step keeps size IDL+PASS: none leading integer zeros+PASS: none leading integer zeros keeps size IDL+PASS: none signed leading zeros+PASS: none signed leading zeros keeps size IDL+PASS: none zero fraction+PASS: none zero fraction keeps size IDL+PASS: none leading decimal point+PASS: none leading decimal point keeps size IDL+PASS: none default+PASS: none default keeps size IDL+PASS: none only min+PASS: none only min keeps size IDL+PASS: none only max+PASS: none only max keeps size IDL+PASS: none invalid min+PASS: none invalid min keeps size IDL+PASS: none invalid max+PASS: none invalid max keeps size IDL+PASS: none overflow max+PASS: none overflow max keeps size IDL+PASS: none any step+PASS: none any step keeps size IDL+PASS: none size overrides range with step 0.001+PASS: none size overrides range with step 1e-3+PASS: none set bounds+PASS: none change step+PASS: none change max+PASS: none change min+PASS: none remove min+PASS: none remove max+PASS: none remove step+PASS: width: 120px overrides range sizing+PASS: field-sizing: content overrides range sizing+PASS: vertical-rl sizes the inline axis+PASS: vertical-lr sizes the inline axisdiff --git a/Tests/LibWeb/Text/expected/number-input-value-fits-preferred-width.txt b/Tests/LibWeb/Text/expected/number-input-value-fits-preferred-width.txtnew file mode 100644index 00000000000..473bd8715f4--- /dev/null+++ b/Tests/LibWeb/Text/expected/number-input-value-fits-preferred-width.txt@@ -0,0 +1,12 @@+PASS: auto, zoom 1, size+PASS: auto, zoom 1, bounds+PASS: auto, zoom 2, size+PASS: auto, zoom 2, bounds+PASS: none, zoom 1, size+PASS: none, zoom 1, bounds+PASS: none, zoom 2, size+PASS: none, zoom 2, bounds+PASS: textfield, zoom 1, size+PASS: textfield, zoom 1, bounds+PASS: textfield, zoom 2, size+PASS: textfield, zoom 2, boundsdiff --git a/Tests/LibWeb/Text/input/number-input-range-width.html b/Tests/LibWeb/Text/input/number-input-range-width.htmlnew file mode 100644index 00000000000..4eb0cf6c035--- /dev/null+++ b/Tests/LibWeb/Text/input/number-input-range-width.html@@ -0,0 +1,106 @@+<!doctype html>+<script src="include.js"></script>+<style>+ @font-face { font-family: Ahem; src: url("../../Ref/data/fonts/Ahem.ttf"); }+ body { width: 5000px; }+ input { font: 10px/20px Ahem; }+</style>+<script>+ asyncTest(async done => {+ await document.fonts.load("10px Ahem");+ const cases = [+ ["rgb", {"min": "0", "max": "255"}, 3],+ ["negative bound", {"min": "-999", "max": "0"}, 4],+ ["fraction and sign", {"min": "-10", "max": "0.25", "step": "0.001"}, 7],+ ["decimal trailing zero", {"min": "0", "max": "100.0"}, 5],+ ["positive exponent", {"min": "0", "max": "1e2"}, null],+ ["decimal exponent", {"min": "0", "max": "1.00e2"}, null],+ ["fractional step", {"min": "0", "max": "1", "step": "0.001"}, 5],+ ["exponent step", {"min": "0", "max": "1", "step": "1e-3"}, null],+ ["step trailing zeros", {"min": "0", "max": "1", "step": "0.000100"}, 8],+ ["negative step", {"min": "0", "max": "1", "step": "-0.01"}, 5],+ ["zero step", {"min": "0", "max": "1", "step": "0"}, 1],+ ["zero step with fraction digits", {"min": "0", "max": "1", "step": "-0.000000"}, 1],+ ["invalid step", {"min": "0", "max": "1", "step": "bad"}, 1],+ ["invalid step containing an e", {"min": "0", "max": "1", "step": "garbage"}, 1],+ ["negative zero", {"min": "-0", "max": "0"}, 1],+ ["long integer", {"min": "0", "max": "1234567890123456789012"}, 22],+ ["long fractional prefix", {"min": "0", "max": "1", "step": "0.00000000000000000001234567890123456789"}, 40],+ ["tiny exponent", {"min": "0", "max": "1", "step": "1e-400"}, null],+ ["exponent underflow", {"min": "0", "max": "1", "step": "1e-1024"}, null],+ ["exponent min", {"min": "-1e2", "max": "0"}, null],+ ["uppercase exponent min", {"min": "-1E2", "max": "0"}, null],+ ["uppercase exponent max", {"min": "0", "max": "1E2"}, null],+ ["uppercase exponent step", {"min": "0", "max": "1", "step": "1E-3"}, null],+ ["leading integer zeros", {"min": "0", "max": "000123"}, 3],+ ["signed leading zeros", {"min": "-00012", "max": "0"}, 3],+ ["zero fraction", {"min": "0", "max": "0.000"}, 1],+ ["leading decimal point", {"min": "0", "max": ".25"}, 4],+ ["default", {}, null],+ ["only min", {"min": "0"}, null],+ ["only max", {"max": "255"}, null],+ ["invalid min", {"min": "bad", "max": "255"}, null],+ ["invalid max", {"min": "0", "max": "bad"}, null],+ ["overflow max", {"min": "0", "max": "1e400"}, null],+ ["any step", {"min": "0", "max": "1", "step": "any"}, null],+ ];+ function input(attributes, appearance) {+ const element = document.createElement("input");+ element.type = "number";+ element.style.appearance = appearance;+ for (const [name, value] of Object.entries(attributes)) element.setAttribute(name, value);+ document.body.append(element);+ return element;+ }+ function width(element) { return element.getBoundingClientRect().width; }+ function check(name, actual, expected) {+ println(`${Math.abs(actual - expected) < 0.1 ? "PASS" : "FAIL"}: ${name}`);+ }+ for (const appearance of ["auto", "none"]) {+ const defaultWidth = width(input({}, appearance));+ const singleWidth = width(input({ min: "0", max: "9" }, appearance));+ println(`${singleWidth < defaultWidth ? "PASS" : "FAIL"}: ${appearance} bounded input is narrower`);+ for (const [name, attributes, characters] of cases) {+ const element = input(attributes, appearance);+ check(`${appearance} ${name}`, width(element), characters === null ? defaultWidth : singleWidth + 10 * (characters - 1));+ check(`${appearance} ${name} keeps size IDL`, element.size, 20);+ }+ for (const step of ["0.001", "1e-3"]) {+ const explicit = input({ min: "-100", max: "1e3", step, size: "4" }, appearance);+ check(`${appearance} size overrides range with step ${step}`, width(explicit), singleWidth + 30);+ }+ const element = input({}, appearance);+ element.min = "0";+ element.max = "255";+ check(`${appearance} set bounds`, width(element), singleWidth + 20);+ element.step = "0.01";+ check(`${appearance} change step`, width(element), singleWidth + 50);+ element.max = "9";+ check(`${appearance} change max`, width(element), singleWidth + 30);+ element.min = "-99";+ check(`${appearance} change min`, width(element), singleWidth + 50);+ element.removeAttribute("min");+ check(`${appearance} remove min`, width(element), defaultWidth);+ element.min = "0";+ element.removeAttribute("max");+ check(`${appearance} remove max`, width(element), defaultWidth);+ element.max = "9";+ element.removeAttribute("step");+ check(`${appearance} remove step`, width(element), singleWidth);+ }+ for (const css of ["width: 120px", "field-sizing: content"]) {+ const bounded = input({ min: "0", max: "255" }, "auto");+ const unbounded = input({}, "auto");+ bounded.style.cssText = unbounded.style.cssText = css;+ bounded.value = unbounded.value = "255";+ check(`${css} overrides range sizing`, width(bounded), width(unbounded));+ }+ for (const writingMode of ["vertical-rl", "vertical-lr"]) {+ const one = input({ min: "0", max: "9" }, "auto");+ const three = input({ min: "0", max: "255" }, "auto");+ one.style.writingMode = three.style.writingMode = writingMode;+ check(`${writingMode} sizes the inline axis`, three.getBoundingClientRect().height - one.getBoundingClientRect().height, 20);+ }+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/number-input-value-fits-preferred-width.html b/Tests/LibWeb/Text/input/number-input-value-fits-preferred-width.htmlnew file mode 100644index 00000000000..b0fa6a4c625--- /dev/null+++ b/Tests/LibWeb/Text/input/number-input-value-fits-preferred-width.html@@ -0,0 +1,38 @@+<!doctype html>+<script src="include.js"></script>+<style>+ @font-face { font-family: Ahem; src: url("../../Ref/data/fonts/Ahem.ttf"); }+ input { font: 10px/20px Ahem; }+</style>+<script>+ asyncTest(async done => {+ await document.fonts.load("10px Ahem");+ for (const appearance of ["auto", "none", "textfield"]) {+ for (const zoom of [1, 2]) {+ for (const bounded of [false, true]) {+ const input = document.createElement("input");+ input.type = "number";+ input.value = "255";+ input.style.appearance = appearance;+ input.style.zoom = zoom;+ if (bounded) {+ input.min = "0";+ input.max = "255";+ } else {+ input.size = 3;+ }+ document.body.append(input);+ const shadow = internals.getShadowRoot(input);+ const text = document.createTreeWalker(shadow, NodeFilter.SHOW_TEXT).nextNode();+ const range = document.createRange();+ range.selectNodeContents(text);+ const valueRect = range.getBoundingClientRect();+ const clipRect = text.parentElement.getBoundingClientRect();+ const fits = valueRect.left >= clipRect.left - 0.1 && valueRect.right <= clipRect.right + 0.1;+ println(`${fits ? "PASS" : "FAIL"}: ${appearance}, zoom ${zoom}, ${bounded ? "bounds" : "size"}`);+ }+ }+ }+ done();+ });+</script>