Commit Graph

780 Commits

Author SHA1 Message Date
Andreas Kling
73c9d1a606 LibWeb: Implement the body element fills the html element quirk
In quirks mode, the body element expands to fill its parent (the html
element) when height is auto, per the quirks spec section 3.7.

This quirk applies when:
- The document is in quirks mode
- The body element has height: auto
- The body is not absolutely/fixed positioned
- The body is not floated
- The body is not inline-level
2026-01-26 16:48:21 +01:00
Andreas Kling
640ec0b64e LibWeb: Fix percentage height resolution in quirks mode
The quirks mode percentage height calculation quirk was incorrectly
applied to anonymous boxes (like the internal flex wrapper inside
buttons), causing buttons to collapse to zero height.

Per the quirks spec, the percentage height quirk:
- Only applies to DOM elements, not anonymous boxes
- Does not apply to flex/grid items (they resolve against their
  container)
- Does not apply to table-related display types

This patch:
1. Excludes anonymous boxes and flex/grid items from the quirk in
   should_treat_height_as_auto()
2. Adds quirks mode percentage height walk-up in
   calculate_inner_height() for inline-level boxes
3. Removes the incorrect flex/grid container exclusion from
   BlockFormattingContext (the quirk applies to containers, not items)
2026-01-26 16:48:21 +01:00
Jelle Raaijmakers
a6958e5a72 LibWeb: Do not generate marker boxes for list-style-type: none
This is expected by the spec and makes it a bit easier to reason about
our layout tree.
2026-01-26 16:41:42 +01:00
Jelle Raaijmakers
01518ba6a6 LibWeb: Use list item's line-height to provide space for marker
Empty list items should still have a default height if a marker is
present.

Fixes #3762.
2026-01-26 16:41:42 +01:00
Jelle Raaijmakers
19882e1ed6 LibWeb: Check available space for float: right boxes
For `float: left` boxes, we would take the available space into account
and push boxes down as required. This applies that same logic to `float:
right` boxes, where we would previously only compare their offset from
the edge using `>= 0`, which was almost always true.

Fixes #4750.
2026-01-26 16:41:42 +01:00
Tim Ledbetter
631e73676e LibWeb: Resolve margins for block-level buttons with width:auto
Previously, buttons with `display: block` and `width: auto` would take
an early return path in compute_width() that set the content width to
fit-content but skipped all margin resolution. This meant `margin: auto`
would not center the button horizontally.
2026-01-26 10:00:17 +01:00
Andreas Kling
492629e3d8 LibWeb: Don't resolve percentage heights against min-height
Per CSS 2.1 Section 10.5, percentage heights should only resolve when
the containing block's height is "specified explicitly". This means a
containing block with `height: auto` and `min-height: 50px` does NOT
provide a definite height for percentage resolution - the child's
`height: 100%` should be treated as `auto`.

Previously, we checked `available_space.height.is_indefinite()` to
determine if percentage heights should become auto. However, this
conflated "available layout space" with "containing block height for
percentage resolution" - these are distinct concepts.

Now we check the containing block's `has_definite_height()` flag, which
correctly reflects whether the containing block has an explicit height
property. This handles:

- Anonymous wrapper blocks (skip them to find real containing block)
- Quirks mode (has special percentage height handling)
- Absolutely positioned elements (excluded, different rules apply)

Also update `calculate_inner_height()` to use the containing block's
actual used height when resolving percentages with indefinite available
space, which fixes inline-block and similar cases.
2026-01-25 20:29:44 +01:00
Callum Law
afdde488c3 LibWeb: Correctly parse logical border-*-*-radius shorthands
Builds on #7609 by parsing these properties correctly in the first place
2026-01-25 10:22:10 +01:00
Aliaksandr Kalenik
2075eddbf1 LibWeb: Fix logical border-radius properties not being applied
Logical border-radius properties are parsed as Percentage/Length values,
not BorderRadiusStyleValue. Handle these types when applying style.
2026-01-24 21:43:23 +01:00
Aliaksandr Kalenik
c3bc64d1cc LibWeb: Fix SVG viewport_size compounding in nested clips
When userSpaceOnUse clips are nested inside objectBoundingBox masks,
the viewport_size was compounding incorrectly because it was calculated
after content scaling by m_parent_viewbox_transform. For userSpaceOnUse
clips (which have no viewBox), the fallback to content_width() returned
the already-scaled value, causing sizes to explode with each nesting
level.

Fix by calculating viewport dimensions before the scaling block. This
ensures m_viewport_size represents the coordinate system dimensions,
not the final pixel dimensions.
2026-01-23 16:23:06 +01:00
Jelle Raaijmakers
6a29b8cc03 LibWeb: Derive inline-block baseline from nested content
Compute inline-block baselines by traversing into nested block children
to find the last in-flow line box, using correct offsets relative to the
margin box edge.

Also ensure inline-flex and inline-grid containers always derive their
baseline from content (per CSS Align), and add special handling for
<input> elements which have `overflow: clip` in the UA stylesheet but
should still align adjacent text with their internal content.
2026-01-22 19:36:09 +01:00
Sam Atkins
b6207201d6 LibWeb/Layout: Replace existing ::backdrop layout nodes when necessary
We had two issues with ::backdrop which this commit fixes:

::backdrop is unique in that it's the previous sibling to its
originating element, instead of a child of it. This means when that
element's layout node is thrown away, the ::backdrop's is not.

A second issue is that if we do a partial layout rebuild, the
originating element's layout node replaces its previous one, but we
would still append a new layout node for ::backdrop to the root, so it
would appear in front of the originating element.

A related issue is that clear_pseudo_element_nodes() got called on the
element after its ::backdrop had been assigned, so it would immediately
lose track of it again.

To solve this, we now always remove the ::backdrop's layout node. If we
need to create a new one, we insert it before the element's layout node
if it has one, otherwise we append as before. This ensures we only ever
have up to one layout node for the ::backdrop, and it appears behind
its originating element.

To support this, create_pseudo_element_if_needed() has a couple of
changes:
- It returns the node that was created.
- The caller can ask it not to insert the node, so that the caller can
  do so (which we use so that we can insert it in a specific place)
2026-01-22 13:52:31 +00:00
Sam Atkins
b4a3520cc1 LibWeb/Layout: Remove duplicate AbstractElement
`pseudo_element_reference` is identical to `element_reference`. No
behaviour change.
2026-01-22 13:52:31 +00:00
Jelle Raaijmakers
d597bba5cf LibWeb: Align anonymous table-cell flex/grid wrappers to top
Vertical-align padding should not apply to anonymous table-cells that
wrap a flex/grid container, as the container handles its own alignment.
2026-01-21 23:44:23 +01:00
Jelle Raaijmakers
18b91206f8 LibWeb: Simplify paintable caching
No functional changes.
2026-01-21 11:30:06 +01:00
Aliaksandr Kalenik
d4feeb1cad LibWeb: Preserve paintable tree across relayouts
Reuse existing paintables during relayout to reduce GC allocation
pressure. Each paintable subclass implements reset_for_relayout()
to clear state before reuse.
2026-01-21 10:00:17 +01:00
Luke Wilde
babfd70ca7 LibGC: Enforce that a Cell type must declare the allocator to use
This ensures that we are explicitly declaring the allocator to use when
allocating a cell(-inheriting) type, instead of silently falling back
to size-based allocation.

Since this is done in allocate_cell, this will only be detected for
types that are actively being allocated. However, since that means
they're _not_ being allocated, that means it's safe to not declare
an allocator to use for those. For example, the base TypedArray<T>,
which is never directly allocated and only the defined specializations
are ever allocated.
2026-01-20 12:00:11 +01:00
Andreas Kling
dc79259970 LibWeb: Support inline elements as abspos containing blocks
CSS allows inline elements with `position: relative` (or other
containing-block-establishing properties) to serve as the containing
block for their absolutely positioned descendants. However, our layout
system stores containing blocks as `Box*`, which cannot represent
inline elements (they are `InlineNode`, not `Box`).

This patch adds a workaround: when computing containing blocks, we
also check if there's an inline element between the abspos element
and its Box containing block that should actually be the CSS
containing block. If found, we store it in a new member called
`m_inline_containing_block_if_applicable` and use it during abspos
layout to:

1. Compute the inline's fragment bounding box as the containing
   block rectangle (including padding, per CSS spec)
2. Resolve percentage-based insets against the inline's dimensions
3. Position the abspos element relative to the inline's location

Some details to be aware of:

- The inline containing block search happens in the function
  `recompute_containing_block()` by walking DOM ancestors (not layout
  tree ancestors, since the layout tree restructures blocks inside
  inlines as siblings)

- For pseudo-elements like `::after`, we start the search from the
  generating element itself, since it may be the inline containing
  block

- Fragment offsets are relative to their block container, so we
  translate the computed rect to the abspos element's containing
  block coordinate system by accumulating offsets up the ancestor
  chain

- When the abspos element uses static position (auto insets), we
  don't apply the inline rect translation since static position is
  already computed in the correct coordinate system

Long term, we want to refactor our "containing block" concept to
map more cleanly to the spec concept. That means turning it into
a rectangle instead of the box this rectangle was derived from.
That's an invasive change for another day though.
2026-01-19 17:34:46 +01:00
Andreas Kling
de47fe86ba LibWeb: Resolve percentage max-width when calculating intrinsic height
When calculating the width to use for intrinsic height determination of
flex items in a column layout, we were unconditionally ignoring
percentage min-width and max-width values.

This was overly conservative - when the containing block has a definite
width, percentages can be resolved. We now check if the available width
is definite and resolve percentages in that case.

This fixes the hero layout on https://slack.com/
2026-01-19 12:53:13 +01:00
Andreas Kling
eb75220eb6 LibWeb: Add missing type-isolating GC allocators for some types
- Layout::BlockContainer
- Painting::PaintableBox
- Painting::SVGGraphicsPaintable
- Platform::TimerSerenity
2026-01-18 10:10:04 +01:00
Andreas Kling
8078d53d4f LibWeb: Avoid intrinsic sizing of boxes that don't create new FC
When computing the width of a box with `width: auto` in an intrinsic
sizing context (min-content or max-content), we previously called
calculate_min/max_content_width() for every such box.

This was wasteful for boxes that don't establish their own formatting
context (e.g. plain divs in a BFC), since their intrinsic width is
simply the intrinsic width of their contents, which we're already
computing as part of the current layout pass.

By only computing intrinsic widths separately for boxes that actually
create a new formatting context (BFC, flex, grid, table, etc.), we
avoid creating redundant throwaway LayoutState objects and formatting
contexts.

For deeply nested structures with `width: max-content` on an ancestor,
this reduces the number of formatting contexts created from O(n) to
O(1), where n is the nesting depth.
2026-01-14 13:52:22 +01:00
Andreas Kling
9079e2b4a7 LibWeb: Return 0 for intrinsic sizes of childless boxes
Boxes with no layout children have no content, so their intrinsic
content size is zero by definition. This lets us skip creating a
throwaway layout state and running a formatting context just to
arrive at the same answer.
2026-01-14 11:46:23 +01:00
Michael Watt
9e35e06dc3 LibWeb: Create containing blocks for non-initial filter values
This fixes:
http://wpt.live/css/css-will-change/will-change-abspos-cb-002.html
http://wpt.live/css/css-will-change/will-change-abspos-cb-003.html
http://wpt.live/css/css-will-change/will-change-fixedpos-cb-001.html
http://wpt.live/css/css-will-change/will-change-fixedpos-cb-004.html
2026-01-12 11:52:50 +00:00
Jonathan Gamble
4579bb5bed LibWeb: Add resize property plumbing 2026-01-12 11:00:14 +00:00
Jonathan Gamble
555681bdb5 LibWeb: Split PaintableWithLines from PaintableBox
No functional changes. I just hope to improve code navigation.
2026-01-12 11:00:14 +00:00
Andreas Kling
3877026082 LibWeb: Pre-generate items in InlineLevelIterator
Instead of generating items lazily during iteration (and using a
lookahead queue for peeking), we now pre-generate all items upfront
in the constructor.

This allows next() and next_non_whitespace_sequence_width() to be
simple O(1) array accesses instead of doing generation work.

To handle tabs correctly (which need to know the accumulated width of
content before them on the line), we track m_accumulated_width_for_tabs
during pre-generation and reset it on forced breaks.
2026-01-11 11:10:19 +01:00
Andreas Kling
e654f9db8c LibWeb: Add ASCII fast path for bidi class lookups in text layout
For ASCII characters, the bidirectional class is trivially known:
letters (A-Z, a-z) are Left-to-Right, and everything else (digits,
spaces, punctuation) is neutral.

This avoids expensive Unicode::bidirectional_class() lookups via ICU
for the common case of ASCII-only text.
2026-01-11 11:10:19 +01:00
Andreas Kling
8cdfbfed49 LibUnicode+LibWeb: Add fast path grapheme segmenter for ASCII text
For ASCII text, every character is its own grapheme - there are no
combining characters or emoji sequences. This means grapheme boundary
detection is trivial: next_boundary(i) is simply i+1.

This commit adds AsciiGraphemeSegmenter, a simple Segmenter subclass
that performs O(1) boundary lookups without any ICU overhead.

TextNode::grapheme_segmenter() now checks if the text is ASCII and uses
this fast path, avoiding expensive ICU BreakIterator cloning and
boundary detection for the common case of ASCII-only text.
2026-01-11 11:10:19 +01:00
Andreas Kling
10737e22d1 LibWeb: Pre-generate text chunks in InlineLevelIterator
Instead of lazily generating text chunks via ChunkIterator with a peek
queue that used O(n) take_first() operations, we now pre-generate all
chunks upfront when entering a text node.

This makes chunk access O(1) via simple array indexing, which speeds up
both next() calls and forward lookups for bidi direction resolution.
2026-01-11 11:10:19 +01:00
Aliaksandr Kalenik
ccffc42d6a LibWeb: Add debug tracing for FormattingContext::run() calls
Add optional tracing that prints a tree visualization of formatting
context `run()` invocations. This is useful for debugging layout issues
where you need to understand the nesting and order of layout passes,
or why a box receives unexpected available space.

Example output:
```
├─ BFC <Viewport<#document>> run(definite(800) x definite(600))
│ ├─ BFC <BlockContainer<HTML>> run(definite(800) x indefinite)
│ │ ├─ IFC <BlockContainer(anonymous)> run(definite(800) x indefinite)
│ │ ├─ GFC <Box<DIV.grid>> run(definite(800) x indefinite)
│ │ │ ├─ BFC <BlockContainer<DIV.item>> run(definite(400) x indefinite)
```
2026-01-09 08:12:21 +01:00
Aliaksandr Kalenik
a203ed8878 LibWeb: Skip automatic minimum size for flex items with definite size
This optimization reduces expensive intrinsic sizing layout passes for
common patterns like multi-line flex containers with explicitly-sized
items.
2026-01-09 00:13:05 +01:00
Aliaksandr Kalenik
ef6829364a LibWeb: Skip intrinsic sizing for stretched grid items
For grid items with auto preferred size, stretch/normal alignment, and
no auto margins, the final size is simply the containing block size
minus margin box sizes. We can compute this directly without calling
`calculate_fit_content_width/height`, which triggers expensive intrinsic
sizing layout passes.
2026-01-08 19:37:14 +01:00
Andreas Kling
2ac363dcba LibGC: Only call finalize() on types that override finalize()
This dramatically cuts down on time spent in the GC's finalizer pass,
since most types don't override finalize().
2026-01-07 20:51:17 +01:00
Andreas Kling
a9cc425cde LibJS+LibWeb: Add missing GC marking visits
This adds visit_edges(Cell::Visitor&) methods to various helper structs
that contain GC pointers, and makes sure they are called from owning
GC-heap-allocated objects as needed.

These were found by our Clang plugin after expanding its capabilities.
The added rules will be enforced by CI going forward.
2026-01-07 12:48:58 +01:00
Sam Atkins
89def06fcc LibWeb/Layout: Implement fixed positioning containing blocks
Previously we only allowed the viewport itself to be the containing
block for fixed-position elements, but the specs give us a few other
situations. Many of these are the same as for absolute-positioned ones.
2026-01-06 15:54:57 +00:00
Sam Atkins
cc3e427498 LibWeb/Layout: Make transform-style: preserve-3d establish APCBs 2026-01-06 15:54:57 +00:00
Sam Atkins
cf2520d6a4 LibWeb/Node: Rename Node::can_contain_boxes_with_position_absolute()
This method matches the spec concept of "establishing an absolute
positioning containing block", so let's use that name.
2026-01-06 15:54:57 +00:00
Aliaksandr Kalenik
5336c53171 LibWeb: Stop intrinsic size cache invalidation at abspos boundaries
When content changes inside a layout node, we now reset intrinsic size
caches only up to the nearest absolutely positioned ancestor, rather
than all the way to the document root.

This optimization is safe because absolutely positioned elements don't
contribute to their ancestors' intrinsic sizes - they are skipped in
min/max content width calculations.

The needs_layout_update flag still propagates to all ancestors so the
document knows layout is needed. Only the cache reset is bounded.
2026-01-05 23:00:06 +01:00
Aliaksandr Kalenik
bdf2dbff98 LibWeb: Move min-height handling for independent FCs to parent context
Previously, GridFormattingContext handled its own min-height constraints
internally, which caused infinite recursion when min-height was an
intrinsic sizing keyword (e.g., min-height: max-content).

This change moves the responsibility to the parent formatting context
(BFC). When any box establishing an independent formatting context has
auto height but non-auto min-height:
1. BFC measures content height using a throwaway LayoutState
2. If content height < min-height, BFC passes min-height as definite
   available height to the child formatting context
3. Child FC runs once with correct constraints, unaware of min-height

Fixes https://github.com/LadybirdBrowser/ladybird/issues/4261 which
is corresponding to stack overflow on https://claude.ai/
2026-01-05 16:09:51 +01:00
Callum Law
79740b04b3 LibWeb: Don't generate layer for background-image none entry
Reduces the time spent in `background_layers()` from 1.5% to 0.04% when
loading https://en.wikipedia.org/wiki/2023_in_American_television
2026-01-05 11:35:26 +00:00
Callum Law
1708ce2e2b LibWeb: Propagate background-clip value for color layer separately
This is required for an optimization in a later commit
2026-01-05 11:35:26 +00:00
Aliaksandr Kalenik
ee141dd9f7 LibWeb: Fix grid placement when using unresolved named grid lines
When an item is placed with an unpositioned start and a positioned end
(e.g., `grid-row: auto / 1`), allow the start position to be negative.
This correctly creates implicit tracks before the explicit grid.
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
e424e8dac2 LibWeb: Handle min-{width/height} in minimum contribution [GFC] 2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
b2bc33a8e4 LibWeb: Fix 0fr track intrinsic sizing in grid layout
When distributing item contributions among flexible tracks with
intrinsic min sizing functions, we were skipping distribution entirely
when total_flex == 0. However, tracks like `0fr` (equivalent to
minmax(auto, 0fr)) should still receive the item's contribution
distributed equally among them.

Now we count intrinsic flexible tracks separately and distribute
equally when all flex factors are zero.
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
5efa268255 LibWeb: Fix fit-content() with zero limit and auto min-width in grid
This commit includes two interdependent changes that must be applied
together:
- Treat `fit-content()` tracks as having an intrinsic min sizing
  function when the limit resolves to zero.
- When clamping growth limit to fit-content limit, use `max(base_size,
  fit_content_limit)` instead of just `fit_content_limit` to preserve
  intrinsic sizing contributions.

These changes are coupled because the first change causes
`fit-content(0)` tracks to participate in intrinsic sizing, while the
second ensures the base size from that sizing is not discarded during
clamping.
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
5346602196 LibWeb: Fix flexible track intrinsic size calculation in grid layout
Rewrite
`increase_sizes_to_accommodate_spanning_items_crossing_flexible_tracks`
to follow spec steps more closely:
1. Process all items spanning flexible tracks together (not grouped by
   span size), taking the maximum contribution per track
2. Distribute item contributions proportionally to flex factors
3. Subtract non-flexible tracks existing sizes from item contribution
   before distributing to flexible tracks
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
ad6eb56a3b LibWeb: Don't serialize empty grid line names from implicit lines
Match serialization behavior of other browsers.
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
ce572e6af8 LibWeb: Fix grid placement when start=line-number and end=identifier
When resolving grid placement like `grid-column: 1 / a`, the start
position from the line number was being incorrectly overwritten when
processing the end identifier. Now we only set start from end if the
start placement doesn't already have a line number.

No tests are affected by the change itself, but combined with the
upcoming commits it's going to result in the progress for
`grid-flex-track-intrinsic-sizes-002.html`.
2026-01-03 16:41:31 +01:00
Aliaksandr Kalenik
f1253c7139 LibWeb: Fix grid placement when using unresolved named grid lines
This patch fixes an issue where grid items using `grid-area: <name>`
with no matching named grid area or lines would be incorrectly placed
at position 0.

According to https://www.w3.org/TR/css-grid-1/#line-placement:
1. When a `<custom-ident>` doesn't match any named line, placement
   should fall back to the first implicit grid line
2. When the same `<custom-ident>` is given for both `grid-*-start` and
   `grid-*-end` (which happens with `grid-area: name`), and both fall
   back to the same implicit line, the resulting span is 1

Previously, the fallback values were hardcoded to 0 and 1, which placed
items in track 0. The fix changes the fallback to use
`m_explicit_line_count` (the first implicit line index) for both start
and end identifiers. When both reference the same line (start == end),
the existing "both positioned" logic now handles this by setting span=1
and adjusting end accordingly.
2026-01-03 16:41:31 +01:00
Gingeh
04b4cd9e00 LibWeb: Treat near-zero aspect-ratios as degenerate 2025-12-30 12:45:31 +01:00