Skip to content

Truly independent web browser

LibWeb: Let a fieldset's content box fill its definite block size

Aliaksandr Kalenik1e2e7614aa00
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
index 385cae8a5fe..9c2351126f6 100644
--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
@@ -1957,11 +1957,21 @@ impl<'pass> BlockFormattingContext<'pass> {
// Margins of elements that establish new formatting contexts do not collapse with their in-flow children
self.margin_state.borrow_mut().reset();
+ let fills_fieldset = container_facts.is_fieldset_box()
+ && node_facts::node_is_fieldset_content_wrapper(facts.data(), facts.computed_values_view_if_styled())
+ && self.used(block_container).has_definite_block_size();
let inside_layout_input = LayoutInput {
available_space,
containing_block_constraints: input.containing_block_constraints,
content_box_position_in_bfc_root: None,
sizing: RootSizingDirectives {
+ forced_content_block_size: fills_fieldset.then(|| {
+ (self.used(block_container).content_block_size.get()
+ - content_block_offset
+ - self.used(node).border_box_bottom(false)
+ - self.used(node).margin_bottom.get())
+ .max(CssPixels::default())
+ }),
forced_min_border_box_block_size: if is_table_formatting_context {
input.sizing.forced_min_border_box_block_size
} else {
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index cc807289e3e..c66421c7655 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -1431,6 +1431,13 @@ fn dimension_block_level_root(
let sizing = run.sizing();
let style = StyleValues::for_node(&run.callbacks, node);
let mut body_input = body_input_with_inner_available_space(run, input);
+ if let Some(block_size) = input.sizing.forced_content_block_size {
+ let used = run.records.used_values(node);
+ used.set_content_block_size(block_size);
+ used.has_definite_block_size.set(true);
+ body_input.available_space.block_size = AvailableSize::definite(block_size);
+ return body_input;
+ }
let mut measured_content_block_size = None;
if sizing.should_treat_block_size_as_auto(node, available_space, constraints)
&& !style.min_height().is_auto()
@@ -1522,7 +1529,7 @@ fn finalize_block_level_root(run: &FormattingContextRun, input: &LayoutInput, bo
.set_content_inline_size(body_result.automatic_content_inline_size);
}
let style = StyleValues::for_node(&run.callbacks, node);
- if !style.display().is_table_inside() {
+ if !style.display().is_table_inside() && input.sizing.forced_content_block_size.is_none() {
let sizing = run.sizing();
let resolution_space = sizing.available_space_for_block_size_resolution(
node,
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index e2320a21684..0cf385735fb 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -463,6 +463,16 @@ pub(crate) fn node_creates_block_formatting_context(
parent_is_flex_or_grid_container
}
+pub(crate) fn node_is_fieldset_content_wrapper(data: &NodeData, style: Option<ComputedValuesView<'_>>) -> bool {
+ has_flag(data, NodeFlag::Anonymous)
+ && data.kind.get() == NodeKind::BlockContainer
+ && data.generated_for.get() == 0
+ && style.is_some_and(|style| {
+ let display = style.display();
+ display.is_block_outside() && (display.is_flow_root_inside() || display.is_flex_inside())
+ })
+}
+
pub(crate) fn node_is_flex_or_grid_container(style: Option<ComputedValuesView<'_>>) -> bool {
style.is_some_and(|style| style.display().is_flex_inside() || style.display().is_grid_inside())
}
diff --git a/Tests/LibWeb/Text/expected/fieldset-content-box-fills-definite-block-size.txt b/Tests/LibWeb/Text/expected/fieldset-content-box-fills-definite-block-size.txt
new file mode 100644
index 00000000000..c0c733a56ba
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/fieldset-content-box-fills-definite-block-size.txt
@@ -0,0 +1,3 @@
+percentage height: 82
+stretched flex item height: 82
+clipped content hit below the fieldset: false
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.txt
new file mode 100644
index 00000000000..e666a267792
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test content's block-size
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.txt b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.txt
new file mode 100644
index 00000000000..3ec2a5c2b09
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.txt
@@ -0,0 +1,16 @@
+Harness status: OK
+
+Found 10 tests
+
+6 Pass
+4 Fail
+Pass rendered legend (writing-mode: horizontal-tb)
+Pass second legend (writing-mode: horizontal-tb)
+Pass div (writing-mode: horizontal-tb)
+Pass rendered legend (writing-mode: vertical-lr)
+Fail second legend (writing-mode: vertical-lr)
+Fail div (writing-mode: vertical-lr)
+Pass rendered legend (writing-mode: vertical-rl)
+Fail second legend (writing-mode: vertical-rl)
+Fail div (writing-mode: vertical-rl)
+Pass Fieldset with a percentage height should not increase the height on every reflow.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/fieldset-content-box-fills-definite-block-size.html b/Tests/LibWeb/Text/input/fieldset-content-box-fills-definite-block-size.html
new file mode 100644
index 00000000000..7380ff35b99
--- /dev/null
+++ b/Tests/LibWeb/Text/input/fieldset-content-box-fills-definite-block-size.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body { margin: 0; }
+ fieldset { width: 200px; height: 100px; margin: 0 0 50px; padding: 0; border: 2px solid black; }
+ legend { height: 20px; padding: 0; }
+</style>
+<fieldset><legend></legend><div id="percentage" style="height: 100%"></div></fieldset>
+<fieldset style="display: flex"><legend></legend><div id="stretched" style="width: 50px"></div></fieldset>
+<fieldset id="clipping" style="overflow: hidden"><legend></legend><div id="tall" style="height: 300px"></div></fieldset>
+<script>
+ test(() => {
+ println(`percentage height: ${percentage.getBoundingClientRect().height}`);
+ println(`stretched flex item height: ${stretched.getBoundingClientRect().height}`);
+ const clippingRect = clipping.getBoundingClientRect();
+ println(`clipped content hit below the fieldset: ${document.elementFromPoint(10, clippingRect.bottom + 10) === tall}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.html
new file mode 100644
index 00000000000..9e070af0337
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-block-size.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<link rel="help" href="https://html.spec.whatwg.org/C/#the-fieldset-and-legend-elements">
+<!-- A test for the following paragraph:
+For the purpose of calculating the used 'block-size', if the computed
+'block-size' is not 'auto', the space allocated for the rendered legend's
+margin box that spills out past the border, if any, is expected to be
+subtracted from the 'block-size'. If the content box's block-size would be
+negative, then let the content box's block-size be zero instead.
+-->
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<style>
+fieldset {
+ margin: 0;
+ padding: 0;
+ border: 2px solid black;
+}
+legend {
+ height: 102px;
+ background-color: yellow;
+}
+.content {
+ background-color: lime;
+}
+</style>
+<fieldset style="block-size: 200px;">
+<legend>Legend</legend>
+<div class="content" style="height:100%"></div>
+</fieldset>
+
+<fieldset style="block-size: 40px;">
+<legend>Legend</legend>
+<div class="content" style="height:100%"></div>
+</fieldset>
+
+<script>
+test(() => {
+ let cs = document.querySelectorAll('.content');
+ assert_equals(cs[0].offsetHeight, Math.max(202 - 102, 0));
+ assert_equals(cs[1].offsetHeight, Math.max(42 - 102, 0));
+}, 'Test content\'s block-size');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.html b/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.html
new file mode 100644
index 00000000000..76e4db5a524
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/rendering/non-replaced-elements/the-fieldset-and-legend-elements/fieldset-percentage-block-size.html
@@ -0,0 +1,64 @@
+<!doctype html>
+<title>fieldset percentage block size</title>
+<script src=../../../../resources/testharness.js></script>
+<script src=../../../../resources/testharnessreport.js></script>
+<style>
+ fieldset { block-size: 100px; margin: 20px; padding: 0; border: 10px solid; }
+ .rendered-legend { block-size: 80%; background: lime; padding: 0; }
+ .second-legend { block-size: 100%; background: yellow; padding: 0; }
+ fieldset > div { block-size: 100%; background: fuchsia; }
+</style>
+<div style="writing-mode: horizontal-tb">
+ <fieldset>
+ <legend class="rendered-legend">rendered legend</legend>
+ <legend class="second-legend">second legend</legend>
+ <div>div</div>
+ </fieldset>
+</div>
+
+<div style="writing-mode: vertical-lr">
+ <fieldset>
+ <legend class="rendered-legend">rendered legend</legend>
+ <legend class="second-legend">second legend</legend>
+ <div>div</div>
+ </fieldset>
+</div>
+
+<div style="writing-mode: vertical-rl">
+ <fieldset>
+ <legend class="rendered-legend">rendered legend</legend>
+ <legend class="second-legend">second legend</legend>
+ <div>div</div>
+ </fieldset>
+</div>
+
+<table cellspacing="0" cellpadding="0" style="width:100px; height:60px;">
+ <tr>
+ <td>
+ <fieldset style="border:none; padding:0; height:100%; margin:0; margin-top:13px;">
+ <div><div id="elm"></div></div>
+ </fieldset>
+ </td>
+ </tr>
+</table>
+
+<script>
+ for (const div of document.querySelectorAll('div[style]')) {
+ for (const el of div.firstElementChild.children) {
+ test(() => {
+ const expected = el.textContent === 'rendered legend' ? '80px' : '30px';
+ // 30px because: 100px - (max(0, legend-block-size - border-block-start))
+ assert_equals(getComputedStyle(el).blockSize, expected);
+ }, `${el.textContent} (${div.getAttribute('style')})`);
+ }
+ }
+
+// crbug.com/1138204. Though the specification doesn't mention this behavior,
+// there must be no doubt about the expected behavior.
+test(() => {
+ const fieldset = document.querySelector('table fieldset');
+ const initialHeight = fieldset.offsetHeight;
+ document.querySelector('#elm').style.display = 'none';
+ assert_equals(fieldset.offsetHeight, initialHeight);
+}, 'Fieldset with a percentage height should not increase the height on every reflow.');
+</script>