Skip to content

Resolve a rendered legend fit-content inline size before layout - #11047

Merged
kalenikaliaksandr merged 1 commit into
LadybirdBrowser:masterfrom
kalenikaliaksandr:cherry-pick-89f7d331
Aug 9, 2026
Merged

Resolve a rendered legend fit-content inline size before layout#11047
kalenikaliaksandr merged 1 commit into
LadybirdBrowser:masterfrom
kalenikaliaksandr:cherry-pick-89f7d331

Conversation

@kalenikaliaksandr

Copy link
Copy Markdown
Member

A fieldset's rendered legend with 'width: auto' uses its fit-content inline size. That size was applied as an override written to the legend's box after its layout returned, so the legend's children were laid out against a provisional stretch size. The fit-content rule now resolves inside the shared block-level inline-size resolution, before the legend's children are laid out, so they resolve against the used inline size. Covered by a new regression test.

A fieldset's rendered legend with 'width: auto' uses its fit-content
inline size. That size was applied as an override written to the
legend's box after its layout returned, so the legend's children were
laid out against a provisional stretch size. The fit-content rule now
resolves inside the shared block-level inline-size resolution, before
the legend's children are laid out, so they resolve against the used
inline size. Covered by a new regression test.
@coderabbitai

coderabbitai Bot commented Aug 9, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Rendered fieldset legends with auto inline size now use fit-content sizing during root inline metric resolution. The fieldset layout path no longer repeats this sizing. A text test covers flex, overflow, and default legend formatting contexts.

Changes

Fieldset legend sizing

Layer / File(s) Summary
Root inline metric resolution
Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
Auto-width rendered legends now receive fit-content inline sizing during root inline metric resolution. The later fieldset-specific recalculation was removed.
Legend sizing test coverage
Tests/LibWeb/Text/input/fieldset-legend-independent-formatting-context-fit-content.html, Tests/LibWeb/Text/expected/fieldset-legend-independent-formatting-context-fit-content.txt
The test covers flex, overflow, and default legend formatting contexts and records their expected dimensions and positions.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant FieldsetLayout
  participant RootInlineMetrics
  participant RenderedLegend
  FieldsetLayout->>RootInlineMetrics: Resolve root inline metrics
  RootInlineMetrics->>RenderedLegend: Detect auto computed width
  RootInlineMetrics->>RenderedLegend: Calculate fit-content inline size
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The description accurately explains the legend sizing change and the added regression test.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs`:
- Around line 519-537: Remove the early return in the block sizing flow and
store the fieldset rendered legend’s fit-content result from the
`style.width().is_auto()` branch as the common block-sizing input. Ensure that
value continues through the existing automatic margin resolution and
`min-width`/`max-width` calculations in the block sizing path, including the
logic around `SizingAxis::Inline` and the later sizing passes.

In
`@Tests/LibWeb/Text/input/fieldset-legend-independent-formatting-context-fit-content.html`:
- Line 7: Update the regression test’s child content and measurement logic:
replace the fixed-size .box setup with size-dependent or wrapping content, and
record each child’s rectangles in addition to the legend rectangles for every
formatting-context case covered by the test. Ensure the assertions observe child
layout at the legend’s available inline size rather than only the legend
geometry.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 3f4474c3-8b58-40ff-8fdd-f9a43137287e

📥 Commits

Reviewing files that changed from the base of the PR and between 8d225a3 and e7308d9.

📒 Files selected for processing (3)
  • Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs
  • Tests/LibWeb/Text/expected/fieldset-legend-independent-formatting-context-fit-content.txt
  • Tests/LibWeb/Text/input/fieldset-legend-independent-formatting-context-fit-content.html

Comment on lines +519 to +537
// https://html.spec.whatwg.org/multipage/rendering.html#the-fieldset-and-legend-elements
// A rendered legend with a computed inline size of auto uses the
// fit-content size, resolved here so the legend's children are laid
// out against the used size rather than a provisional stretch size.
if style.width().is_auto() {
let container = self.containing_block(node);
if !container.is_invalid()
&& self.facts(container).is_fieldset_box()
&& self.facts(container).rendered_legend() == node
{
return Some(sizing.calculate_fit_content_size(
node,
SizingAxis::Inline,
remaining_available_space,
constraints,
));
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Keep fit-content sizing on the common block-sizing path.

The early return bypasses automatic margin resolution and the min-width/max-width passes at Lines 661-694. For example, max-width: 20px will not constrain an 80px legend, and margin-inline: auto will remain unresolved.

Store the fit-content value as the common path’s input, then apply the existing margin and min/max calculations.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs` around lines
519 - 537, Remove the early return in the block sizing flow and store the
fieldset rendered legend’s fit-content result from the `style.width().is_auto()`
branch as the common block-sizing input. Ensure that value continues through the
existing automatic margin resolution and `min-width`/`max-width` calculations in
the block sizing path, including the logic around `SizingAxis::Inline` and the
later sizing passes.

body { margin: 0; }
fieldset { margin: 0; border: 2px solid black; padding: 10px; width: 300px; }
legend { padding: 0; }
.box { width: 40px; height: 16px; background: papayawhip; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Make the regression test observe child layout.

The fixed 40px children do not depend on the legend’s available inline size. The test also records only the legend rectangles. A regression that lays out children at the provisional fieldset width can therefore pass.

Add size-dependent or wrapping child content, and record the child rectangles for each formatting context.

Also applies to: 27-30

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@Tests/LibWeb/Text/input/fieldset-legend-independent-formatting-context-fit-content.html`
at line 7, Update the regression test’s child content and measurement logic:
replace the fixed-size .box setup with size-dependent or wrapping content, and
record each child’s rectangles in addition to the legend rectangles for every
formatting-context case covered by the test. Ensure the assertions observe child
layout at the legend’s available inline size rather than only the legend
geometry.

@kalenikaliaksandr
kalenikaliaksandr merged commit abdb9c5 into LadybirdBrowser:master Aug 9, 2026
13 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant