Skip to content

Truly independent web browser

LibWeb: Size range inputs by their natural size, not UA styles

sideshowbarkerb72fee696fcd
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.css
index 64af6ec6a59..dc0381f4699 100644
--- a/Libraries/LibWeb/CSS/Default.css
+++ b/Libraries/LibWeb/CSS/Default.css
@@ -151,8 +151,6 @@ option, optgroup {
/* Custom <input type="range"> styles */
input[type=range] {
display: inline-block;
- width: 129px;
- height: 16px;
margin: 2px;
&::slider-track {
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index f2ec309f865..4e63aee8a69 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -1916,6 +1916,10 @@ static void publish_element_replaced_content_input(DOM::Element& element, StyleE
set_image_natural_size_input(style_engine, node, *input);
return;
}
+ if (input->type_state() == HTML::HTMLInputElement::TypeAttributeState::Range) {
+ set_replaced_content_input(style_engine, node, Kind::RangeInput, 0, 0);
+ return;
+ }
auto kind = Kind::Input;
switch (input->type_state()) {
case HTML::HTMLInputElement::TypeAttributeState::Text:
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index d68bca51efb..cf9cb2dc475 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -893,6 +893,8 @@ pub enum FfiReplacedContentInputKind {
NaturalSize = 5,
/// An SVG `<image>` whose image has decoded: its natural size, as `NaturalSize`.
DecodedSvgImage = 6,
+ /// An `<input>` in the Range state, which has no values.
+ RangeInput = 7,
}
/// The bits of a replaced content input's `present`, for the kinds whose values can be missing.
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 973d835136a..57bc54dc8e5 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -35,6 +35,8 @@ pub enum ReplacedContentInput {
is_text_entry: bool,
include_number_stepper: bool,
},
+ /// An `<input>` in the Range state, a slider.
+ RangeInput,
/// 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.
@@ -75,6 +77,7 @@ impl ReplacedContentInput {
is_text_entry: kind == Kind::TextEntryInput as u8,
include_number_stepper: values[1] != 0,
},
+ kind if kind == Kind::RangeInput as u8 => Self::RangeInput,
kind if kind == Kind::Canvas as u8 => Self::Canvas {
width: values[0],
height: values[1],
diff --git a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
index 5bee4bde767..ef2e0c605bb 100644
--- a/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
+++ b/Libraries/LibWeb/Rust/src/layout/abspos_engine.rs
@@ -1926,11 +1926,14 @@ 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,
- // and text inputs without their native appearance, which have a default preferred size instead.
+ // and text inputs and sliders 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_text_input())
+ (facts.is_native_form_control_box()
+ || facts.is_non_devolvable_widget()
+ || facts.is_text_input()
+ || facts.is_slider())
&& 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/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index fd08afcb088..d51d92f96b8 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -109,6 +109,7 @@ struct ReplacedContentStyle {
is_horizontal: bool,
appearance_is_none: bool,
field_sizing_is_content: bool,
+ zoom: f32,
}
impl ReplacedContentStyle {
@@ -154,9 +155,17 @@ impl ReplacedContentStyle {
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),
+ zoom: style.effective_zoom(),
}
}
+ // AD-HOC: A slider has no in-flow content to size itself from, so it gets a default content-box size of 129px by
+ // 16px in its writing mode, which zoom scales.
+ fn slider_size(self) -> (CssPixels, CssPixels) {
+ let zoomed = |length: i64| CssPixels::from_integer(length).scaled(f64::from(self.zoom));
+ self.in_writing_mode(zoomed(129), zoomed(16))
+ }
+
/// `count` characters, the `ch` unit.
fn characters(self, count: u32) -> CssPixels {
CssPixels::nearest_value_for(f64::from(count) * self.zero_advance.to_double())
@@ -237,6 +246,15 @@ fn replaced_content_facts(
facts.default_preferred_height = Some(height);
}
}
+ if matches!(input, ReplacedContentInput::RangeInput) {
+ facts.is_slider = true;
+ // NB: A slider without its native appearance keeps the size of one.
+ if style.appearance_is_none {
+ let (width, height) = style.slider_size();
+ facts.default_preferred_width = Some(width);
+ facts.default_preferred_height = Some(height);
+ }
+ }
facts
}
@@ -248,6 +266,8 @@ pub(crate) struct ReplacedContentFacts {
pub(crate) default_preferred_height: Option<CssPixels>,
pub(crate) text_entry_content_is_ordinary_box: bool,
pub(crate) include_number_stepper: bool,
+ /// Whether the box is a slider's, with or without its native appearance.
+ pub(crate) is_slider: 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,
@@ -318,9 +338,7 @@ fn auto_content_size(
NodeKind::RadioButton => {
ReplacedIntrinsicSize::of_size((CssPixels::from_integer(12), CssPixels::from_integer(12)))
}
- // AD-HOC: A slider has no in-flow content to size itself from, so provide a default content-box size for when
- // its `width` or `height` is `auto`: 20ch by 16px.
- NodeKind::RangeInputBox => ReplacedIntrinsicSize::of_size((style.characters(20), CssPixels::from_integer(16))),
+ NodeKind::RangeInputBox => ReplacedIntrinsicSize::of_size(style.slider_size()),
NodeKind::TextAreaBox => {
let ReplacedContentInput::TextArea { cols, rows } = input else {
panic!("a textarea publishes its cols and rows as it arrives");
@@ -990,9 +1008,8 @@ impl<'pass> NodeFacts<'pass> {
let size = &mut facts.auto_content_size;
let zoomed = |length: Option<CssPixels>| length.map(|length| length.scaled(f64::from(zoom)));
match self.data().kind.get() {
- // NB: These dimensions are derived from font metrics that already include zoom.
- NodeKind::TextAreaBox | NodeKind::TextInputBox => {}
- NodeKind::RangeInputBox => size.height = zoomed(size.height),
+ // NB: These dimensions are derived from font metrics that already include zoom, or zoomed already.
+ NodeKind::TextAreaBox | NodeKind::TextInputBox | NodeKind::RangeInputBox => {}
_ => {
size.width = zoomed(size.width);
size.height = zoomed(size.height);
@@ -1130,14 +1147,20 @@ impl<'pass> NodeFacts<'pass> {
/// https://drafts.csswg.org/css-sizing-3/#min-content-zero
/// A box whose cyclic percentage width resolves against zero for its min-content contribution: a replaced box, or a
- /// compressible form control (a text entry input, a textarea, or a select).
+ /// compressible form control (a text entry input, a slider, a textarea, or a select).
pub(crate) fn resolves_cyclic_percentages_like_replaced_box(&self) -> bool {
self.is_replaced_box()
|| self.is_text_input()
+ || self.is_slider()
|| self.data().kind.get() == NodeKind::TextAreaBox
|| self.is_select_box()
}
+ /// Whether the box is a range input's, with or without its native appearance.
+ pub(crate) fn is_slider(&self) -> bool {
+ self.is_range_input_box() || self.replaced_content().is_slider
+ }
+
/// Whether the box is a select element's own box. Its kind is that of any box with its display type, so only the
/// element's construction facts tell it apart, and reading them takes a lookup in the style store.
pub(crate) fn is_select_box(&self) -> bool {
@@ -1663,6 +1686,7 @@ mod node_facts_tests {
is_horizontal: true,
appearance_is_none: false,
field_sizing_is_content: false,
+ zoom: 1.0,
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 01a6331267d..f1e829bea94 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -2228,9 +2228,11 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
let facts = self.facts(node);
let box_is_sized_as_replaced_element =
self.box_is_sized_as_replaced_element(sized, available_space, constraints);
- let mut makes_block_size_definite = sized.metrics().has_definite_inline_size()
- && facts.has_preferred_aspect_ratio()
- && box_is_sized_as_replaced_element;
+ // NB: A slider's block size follows from its intrinsic size, with no layout of its content, so its internal
+ // track resolves a percentage block size against it, and centers in it.
+ let mut makes_block_size_definite = box_is_sized_as_replaced_element
+ && ((sized.metrics().has_definite_inline_size() && facts.has_preferred_aspect_ratio())
+ || facts.is_slider());
// NB: Replaced sizing resolves a calc-size() itself.
let mut block_size = if box_is_sized_as_replaced_element {
self.compute_block_size_for_replaced_element(sized, available_space, constraints)
@@ -2416,8 +2418,14 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
let block_size_is_definite_from_aspect_ratio = sized_cells.cells.has_definite_inline_size()
&& facts.has_preferred_aspect_ratio()
&& block_size_is_automatic;
+ // NB: A slider's automatic block size follows from its intrinsic size, with no layout of its content, so
+ // its internal track resolves a percentage block size against it, and centers in it.
+ let block_size_is_definite_from_slider_size = facts.is_slider() && block_size_is_automatic;
- if native_control_block_size_constraint_is_active || block_size_is_definite_from_aspect_ratio {
+ if native_control_block_size_constraint_is_active
+ || block_size_is_definite_from_aspect_ratio
+ || block_size_is_definite_from_slider_size
+ {
sized_cells
.cells
.update_cells(|cells| cells.has_definite_block_size = true);
diff --git a/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt b/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
index 51ede88f990..00637c0b334 100644
--- a/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
+++ b/Tests/LibWeb/Layout/expected/input-range-stretch-in-grid.txt
@@ -6,12 +6,12 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextNode start: 0, length: 5, rect: [8,8 53.3125x20] baseline: 16
"Label"
TextNode <#text> (not painted)
- RangeInputBox <input> at [63.3125,10] [2+0+0 129 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline
- Box <div> at [63.3125,10] flex-container(row) [0+0+0 129 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [63.3125,16] positioned flex-item [0+0+0 129 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [63.3125,16] positioned [0+0+0 64.5 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [127.8125,18] positioned [64.5+0+0 16 0+0+48.5] [0+0+0 16 0+0+0] children: not-inline
+ RangeInputBox <input> at [63.3125,10] [2+0+0 342.6875 0+0+2] [2+0+0 16 0+0+2] [BFC] children: not-inline
+ Box <div> at [63.3125,10] flex-container(row) [0+0+0 342.6875 0+0+0] [0+0+0 16 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [63.3125,16] positioned flex-item [0+0+0 342.6875 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [63.3125,16] positioned [0+0+0 171.34375 0+0+0] [0+0+0 4 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [234.65625,18] positioned [171.34375+0+0 16 0+0+155.34375] [0+0+0 16 0+0+0] children: not-inline
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
SC for BlockContainer<HTML> [0,0 800x36] (z-index: auto)
- SC for BlockContainer<DIV> [127.8125,18 16x16] (z-index: auto), has_transform
+ SC for BlockContainer<DIV> [234.65625,18 16x16] (z-index: auto), has_transform
diff --git a/Tests/LibWeb/Text/expected/input-range-intrinsic-size.txt b/Tests/LibWeb/Text/expected/input-range-intrinsic-size.txt
new file mode 100644
index 00000000000..af9cc6f0272
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/input-range-intrinsic-size.txt
@@ -0,0 +1,17 @@
+default: 129x16
+display: block: 129x16
+vertical-lr: 16x129
+in a vertical-lr container: 16x129
+grid item: 296x16
+grid item, justify-self: start: 129x16
+column flex item: 296x16
+row flex item: 129x46
+abspos between insets: 300x50
+zoom: 2: 258x32
+aspect-ratio: 1: 129x129
+width: 50%: 150x16
+height: 40px: 129x40
+appearance: none: 129x16
+appearance: none, display: block: 129x16
+appearance: none, grid item: 296x16
+appearance: none, abspos between insets: 300x50
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-001.txt
index 69ba51fa39f..c7699a67047 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-001.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 15 tests
-14 Pass
-1 Fail
+15 Pass
Pass .flexbox 1
Pass .flexbox 2
Pass .flexbox 3
@@ -15,7 +14,7 @@ Pass .flexbox 8
Pass .flexbox 9
Pass .flexbox 10
Pass .flexbox 11
-Fail .flexbox 12
+Pass .flexbox 12
Pass .flexbox 13
Pass .flexbox 14
Pass .flexbox 15
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-002.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-002.txt
index 69ba51fa39f..c7699a67047 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-002.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/flex-item-compressible-002.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 15 tests
-14 Pass
-1 Fail
+15 Pass
Pass .flexbox 1
Pass .flexbox 2
Pass .flexbox 3
@@ -15,7 +14,7 @@ Pass .flexbox 8
Pass .flexbox 9
Pass .flexbox 10
Pass .flexbox 11
-Fail .flexbox 12
+Pass .flexbox 12
Pass .flexbox 13
Pass .flexbox 14
Pass .flexbox 15
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/input-range-intrinsic-size.html b/Tests/LibWeb/Text/input/input-range-intrinsic-size.html
new file mode 100644
index 00000000000..2cdeff3e54b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/input-range-intrinsic-size.html
@@ -0,0 +1,37 @@
+<!doctype html>
+<script src="include.js"></script>
+<script>
+ test(() => {
+ const cases = [
+ ["default", "", ""],
+ ["display: block", "width: 300px", "display: block"],
+ ["vertical-lr", "", "writing-mode: vertical-lr"],
+ ["in a vertical-lr container", "writing-mode: vertical-lr; height: 300px", ""],
+ ["grid item", "display: grid; width: 300px", ""],
+ ["grid item, justify-self: start", "display: grid; width: 300px", "justify-self: start"],
+ ["column flex item", "display: flex; flex-direction: column; width: 300px", ""],
+ ["row flex item", "display: flex; width: 300px; height: 50px", ""],
+ ["abspos between insets", "position: relative; width: 300px; height: 50px", "position: absolute; inset: 0; margin: 0"],
+ ["zoom: 2", "zoom: 2", ""],
+ ["aspect-ratio: 1", "", "aspect-ratio: 1"],
+ ["width: 50%", "width: 300px", "width: 50%"],
+ ["height: 40px", "", "height: 40px"],
+ ["appearance: none", "", "appearance: none"],
+ ["appearance: none, display: block", "width: 300px", "appearance: none; display: block"],
+ ["appearance: none, grid item", "display: grid; width: 300px", "appearance: none"],
+ ["appearance: none, abspos between insets", "position: relative; width: 300px; height: 50px", "appearance: none; position: absolute; inset: 0; margin: 0"],
+ ];
+ for (const [name, containerStyle, inputStyle] of cases) {
+ const container = document.createElement("div");
+ container.style.cssText = containerStyle;
+ const input = document.createElement("input");
+ input.type = "range";
+ input.style.cssText = inputStyle;
+ container.append(input);
+ document.body.append(container);
+ const rect = input.getBoundingClientRect();
+ println(`${name}: ${rect.width}x${rect.height}`);
+ container.remove();
+ }
+ });
+</script>