Commit Graph

65 Commits

Author SHA1 Message Date
Martin Robinson
45972e07ab script: Rewrite the layout DOM wrappers (#44114)
This change reworks the layout DOM wrappers so that they are simpler and
easier to reason about. The main changes here:

**Combine layout wrappers into one interface:**

 - `LayoutNode`/`ThreadSafeLayoutNode` is combined into `LayoutNode`:
   The idea here is that `LayoutNode` is always thread-safe when used in
   layout as long as no `unsafe` calls are used. These interfaces
   only expose what is necessary for layout.
 - `LayoutElement`/`ThreadSafeLayoutElement` is combined into
   `LayoutElement`: See above.

**Expose two new interfaces to be used *only* with `stylo` and
`selectors`:**

`DangerousStyleNode` and `DangerousStyleElement`. `stylo`
and `selectors` have a different way of ensuring safety that is
incompatible with Servo's layout (access all of the DOM tree anywhere,
but ensure that writing only happens from a single-thread). These types
only implement things like `TElement`, `TNode` and are not intended to
be used by layout at all.

All traits and implementations are moved to files that are named after
the struct or trait inside them, in order to better understand what one
is looking at.

The main goals here are:

 - Make it easier to reason about the safe use of the DOM APIs.
 - Remove the interdependencies between the `stylo` and `selectors`
   interface implementations and the layout interface. This helps
   with the first point as well and makes it simpler to know where
   a method is implemented.
 - Reduce the amount of code.
 - Make it possible to eliminate `TrustedNodeAddress` in the future.
 - Document and bring the method naming up to modern Rust conventions.

This is a lot of code changes, but is very well tested by the WPT tests.
Unfortunately, it is difficult to make a change like this iteratively.
In addition, this new design comes with new documentation at
servo/book#225.

Testing: This should not change behavior so should be covered by
existing
WPT tests.

Signed-off-by: Martin Robinson <mrobinson@fastmail.fm>
2026-04-12 04:22:06 +00:00
Abbas Olanrewaju Sarafa
02a350d864 layout: Rename confusing ``SequentialLayoutState::collapse_margins`` (#43978)
Renamed ```SequentialLayoutState::collapse_margins``` to
```commit_margin``` in ```float.rs```, ```mod.rs``` &
```inline/mod.rs```

Testing: No testing required - just renaming. Compiles successfully.
Fixes: #43941

Signed-off-by: Sabb <sarafaabbas@gmail.com>
2026-04-06 19:10:28 +00:00
Sharan Poojari
57b86edb89 layout: Fix line breaking opportunities for Chinese and Japanese (#43744)
Enable `CJK-aware` line breaking in inline layout so Chinese/Japanese
text gets proper wrap opportunities instead of overflowing containers.

Testing: some WPT are now passing

---------

Signed-off-by: SharanRP <z8903830@gmail.com>
2026-04-05 23:15:54 +00:00
Martin Robinson
322dfd0115 layout: Combine some common text segment data into FontAndScriptInfo (#43653)
This starts splitting out commonly shared data into a new data structure
that will make it easier to compare old shaping results to new ones in
the future. This eliminates some memory usage during inline layout, but
adds 8 bytes per segment (usually one per inline box). We hope to
recover this memory by storing one `GlyphStore` per `TextRunSegment`,
which
should recover 16 bytes.

Testing: This should not change behavior so is covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Luke Warlow <lwarlow@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-03-30 13:15:23 +00:00
Oriol Brufau
80c57f80d4 layout: Fix justify-self: auto on block-in-inline (#43625)
We were resolving `justify-self: auto` according to the `justify-items`
of the containing block. However, the CSSWG resolved to instead use the
parent box; and if the parent is an inline box, then `justify-items`
doesn't apply, so `justify-self: auto` behaves as `normal`.

Testing: Adding WPT test
Fixes: #43624

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-03-25 06:05:03 +00:00
Oriol Brufau
62b2eefc7c layout: Allow block-levels inside an inline directly, without wrapper (#43586)
When encountering a block-level inside an inline box, we would wrap it
(together with other block-level siblings, if any) inside an anonymous
block box.

That was complicating the logic for no real benefit, so just get rid of
these anonymous wrappers, and allow block-levels as direct children of
inlines.

This aligns Servo with WebKit, which did the same refactoring:
https://commits.webkit.org/304357@main

Blink still generates these wrappers, but its design doc acknowledges
that "the anonymous box is not strictly required".

Testing: No existing test fails. But note that, while minimal, this may
have some observable implications. For example, as an accidental
side-effect of #41492, Servo aligned with the CSSWG resolution in
https://github.com/w3c/csswg-drafts/issues/11462, but now we will revert
to the previous behavior. I will address it in a follow-up, and add a
test.

Fixes: #41636

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-03-24 14:31:42 +00:00
Vinayak Sharma
d47711e849 layout: Fix static position of abspos nested within a block-in-inline (#43084)
Fixes incorrect static positioning of absolutely positioned elements
nested within a block that has `margin-top` inside an inline element.
Added two lines of code to `AnonymousBlockBox::layout_into_line_items`
in components/layout/flow/inline/mod.rs:
- We snapshot the current positioning_context_length before laying out
the block.
- We call `adjust_static_position_of_hoisted_fragments` on the
positioning_context after the block fragment is placed by the state.

Testing: 
Tested the fix using the manual reproduction HTML provided in the issue,
Also ran the existing WPT tests in `tests/wpt/tests/css/css-position/`
to ensure there are no regressions.


Fixes: #42890

---------

Signed-off-by: vinayak sharma <vinayaks0111@gmail.com>
Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-03-23 17:27:44 +00:00
minghuaw
3a50ff1c10 layout: Add initial support for first-letter pseudo element (#43027)
This and the accompanying stylo PR
(https://github.com/servo/stylo/pull/320) is a partial implementation of
the `first-letter` pseudo element.

Testing: Existing WPT tests. There is one WPT test
(fcp-typographic-pseudo.html) that servo currently fails would timeout
with this PR because opacity of `TextFragment` is taken from the
`InlineFormattingContext` instead of the pseudo element.
Fixes: #43008 
Part of: #15413

---------

Signed-off-by: Minghua Wu <michael.wu1107@gmail.com>
Signed-off-by: minghuaw <michael.wu1107@gmail.com>
Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Xiaocheng Hu <xiaochengh.work@gmail.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-03-11 15:30:15 +00:00
Freya Arbjerg
4ec46aaadc layout: Support text carets on empty lines in <textarea> (#42218)
This PR modifies the text layout in such a way that text carets can
render on empty lines (other than the first if the textarea is empty).
Zero-length segments are added to support caret rendering. The last
line, if it is without content, does not currently render a caret
correctly

This corresponds to cases 0, 1, and 3 in #41338

Testing: WPT subset `tests/wpt/tests/css/css-text/` ran as expected, one
internal WPT test was added
Fixes: Partially addresses https://github.com/servo/servo/issues/41338

---------

Signed-off-by: Freya Arbjerg <git@arbjerg.dev>
Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2026-03-09 07:06:27 +00:00
Oriol Brufau
42d228dcc7 Upgrade Stylo to 2026-03-01 (#43045)
This continues  #42361

Changelog:
- Upstream:
7cd2a178d3...74ddab4091
- Servo fixups:
a5eabbdfc2...ecf17b5425

Stylo tracking issue: https://github.com/servo/stylo/issues/321

In particular, this includes some performance optimizations, improves
`attr()`, and adds support for `none` keywords in `clamp()`.

Testing: Some WPT tests pass.

---------

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-03-06 23:39:55 +00:00
Martin Robinson
027910e544 layout/fonts: Mitigate races related to quirks mode and fallback fonts (#42960)
This change includes two related fixes for flakiness revealed by #42847.

1. When caching fonts, try harder to not re-cache a font. This could
   happen due to a race condition with the read-write lock on the font
   cache. This change improves memory usage in some cases as it prevents
   a font from being loaded twice.
2. Incorporate both the strut size and the block size contribution of a
   previously unused font in an inline container. Before the code was
   incorporating one or the other. In addition, *only* incorporate the
   block contribution of a previously unused font if its metrics
   meaningfully differ from the container's default font. This was the
   case triggered by failing to have the fix in part one of the change.

Testing: This fixes flakiness in an internal WPT-style test.
Fixes: #42908.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Luke Warlow <lwarlow@igalia.com>
2026-03-03 21:10:05 +00:00
Oriol Brufau
e0bd48f16a layout: Fix abspos position when it would be preceded by phantom line (#42586)
The static position of an abspos is the hypothetical position that it
would have had in the normal flow. If the abspos is inside inline layout
and it had a block-level original display, then the static position is
computed as if the abspos was a block-level that breaks the inline.

Usually this places the static position after the current line. However,
if the abspos is not preceded by any text, padding, border, margin,
preserved newline or atomic inline, then the static position should be
as if the abspos was preceded by a 0px tall phantom line. Previously we
weren't taking that into account.

Note that browsers aren't completely interoperable in corner cases.

Testing: Adding 3 new tests. Blink fails the 2nd one and Gecko fails the
3rd one. WebKit and Servo pass them all.
Fixes: #41990

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-02-26 18:20:46 +00:00
Oriol Brufau
60c5977291 layout: Correctly repair OutsideMarker::list_item_style (#42825)
`OutsideMarker::repair_style()` was setting `list_item_style` to the
style of the marker, not the style of the list item.

This patch sets it to `node.parent_style(context)` instead, and it fixes
`ServoThreadSafeLayoutNode::parent_style()` to take the pseudo-element
chain into account. This requires modifying a bunch of logic to pass the
`SharedStyleContext` around.

Testing: Adding a new test

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-02-25 13:25:45 +00:00
Oriol Brufau
2a3f23c78d Upgrade Stylo to 2026-02-02 (#42361)
This continues #41916

Changelog:
- Upstream:
360787fff5...7cd2a178d3
- Servo fixups:
e4d50e905e...d9831d464b

Stylo tracking issue: https://github.com/servo/stylo/issues/305

In particular, this adds support for `alignment-baseline` and
`baseline-shift`, and turns `vertical-align` into a shorthand of them.
This also introduces `vertical-align: center`.

Testing: Various tests improve. Some internal tests are updated because
they were wrong. And some fail because we don't support presentation
attributes on SVG elements.

---------

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-02-09 11:16:42 +00:00
Rocketjumper
e950e5c2ca layout: Suppress soft wrap opportunities between NU/AL/AI/ID characters to respect word-break: keep-all (#42088)
This change modifies `LineBreaker` to properly handle `word-break:
keep-all` by adjusting the arguments passed to
`icu_segmenter::LineSegmenter`.

- [CSS
specification](https://drafts.csswg.org/css-text/#valdef-word-break-keep-all)
- Stylo PR : servo/stylo#289

Testing: Created a WPT test for this change, specifically:
`tests/wpt/tests/css/css-text/word-break/word-break-keep-all-011.html`.
Fixes: #42047

---------

Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
2026-01-28 08:11:33 +00:00
Martin Robinson
2a7d2780b3 fonts: Store shaping output per-character rather than per-code point (#42105)
This change is a reworking of the shaping code and simplification of the
`GlyphRun` data structure.

The shaper was written between 2012 and 2014 against an early version of
Rust. It was originally written to have a single glyph entry per UTF-8
code point. This is useful when you always need to iterate through
glyphs based on UTF-8 code points. Unfortunately, this required a
tri-level data structure to hold detailed glyphs and meant that CJK
characters took over 3x the memory usage of ASCII characters. In
addition, iterating through glyphs (the most common and basic operation
on shaped text) required doing a lookup involving a binary search for
detailed glyphs (ones that had large advances or mapped to more or less
than a single UTF-8 code point).

The new design of the `GlyphStore` is instead based on `chars` in the
input string. These are tracked during layout so that the resulting
glyph output can be interpreted relatively to its original character
offset in the containing IFC. We are already dealing with IFC text on a
per-character basis for a variety of reasons (such as text
transformation and whitespace collapse). In addition, we will now able
to
implement mapping between the character offsets before and after layout
transformations of the original DOM string.

Now the penalty of more complex glyph iteration is only paid when
transforming glyph offsets to character offsets. Currently this is only
done for selections and clicking in text boxes, both of which are much
less common than layout.

This change does not properly handle selections in RTL text, though
rendering and basic selection and visual movement works (though buggy).

It does not seem like this affects the performance of shaping based on
measurement using the text shaping performance counters. This likely
means that the performance of shaping is dominated on our machines by
HarfBuzz. We noticed no performance degradation in Speedometer when run
on a M3 Mac.

Followup work:
 - Properly handle selection in RTL text.
 - Support mapping from original DOM character offsets to offsets in
   layout after text transformation and whitespace collapse. This is now
   possible.

Testing: This causes some tests to pass and a few to fail. This is
likely
due to the fact that we are handling glyphs more consistently while
shaping. Of the new failures:
- `letter-spacing-bengali-yaphala-001.html`,
`letter-spacing-cursive-001.html`, `font-feature-settings-tibetan.html`
where passing before probably because we were not applying letter
spacing to detailed glyphs. These scripts should not have letter spacing
applied to them, because they are cursive -- which we never implemented
properly. It will be handled in a a followup.
- `shaping-arabic-diacritics-001.html`: This was a false pass. The tests
verifies that Arabic diacritics are applied to NBSP. This wasn't
happening before nor after this change, but the results matched anyway.
Now they don't, but before and after are equally broken.
 - 
Fixes: #216
Part of #35540.

---------

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-01-26 12:38:04 +00:00
batu_hoang
bd97204816 layout: Support style inheritance for display: contents and <slot> elements (#41855)
In DOM traversal, `display:contents `elements don't generate boxes, but
their styles still need to apply to children.
`ModernContainerBuilder` now keeps track of `display:contents` ancestors
during traversal then apply their style to text runs.

In `InlineFormattingContextBuilder::push_text` only use same style
parent if both selected and current inline style are identical.

Testing: 

> PASS [expected FAIL]
/css/css-display/display-contents-dynamic-before-after-001.html
> PASS [expected FAIL]
/css/css-display/display-contents-dynamic-inline-flex-001-inline.html
> PASS [expected FAIL]
/css/css-display/display-contents-dynamic-inline-flex-001-none.html
> FAIL [expected PASS] /css/css-display/display-contents-fieldset.html
> PASS [expected FAIL]
/css/css-display/display-contents-first-line-002.html
> PASS [expected FAIL] /css/css-display/display-contents-inline-001.html
> PASS [expected FAIL]
/css/css-display/display-contents-inline-flex-001.html
> PASS [expected FAIL]
/css/css-display/display-contents-line-height.html

Fixes: https://github.com/servo/servo/issues/41797

cc: @xiaochengh

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2026-01-22 03:04:28 +00:00
Martin Robinson
4a9c95ac8e layout: Handle selection during display list construction (#41963)
Instead of handling selection in text input during box tree / fragment
tree construction, fully handle it during display list construction.
This means that when the selection changes in script, it updates
automatically in the `FragmentTree` and only a new display list is
necessary. This avoids a layout while changing the selection in text
fields.

Testing: This fixes a few rendering issues, but these are very hard
to isolate and test for. It causes one test to start failing, but this
is
because a cursor that wasn't rendered properly now starts showing
up.
Fixes: #41920.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-01-21 08:04:15 +00:00
Oriol Brufau
d0dcdb5a1a layout: Reduce visibility in components/layout/flow/inline/mod.rs (#42015)
Various methods, structs and fields were unnecessarily public. Make them
private.

Testing: Not needed, no behavior change.

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-20 00:09:05 +00:00
Oriol Brufau
bc167b0ff1 layout: Simplify process_soft_wrap_opportunity() (#42008)
Some of its logic can just be replaced with a call to the method
`unbreakable_segment_fits_on_line()`.

Testing: Not needed, no behavior change

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-19 13:16:17 +00:00
Oriol Brufau
e58bdc03f9 layout: Track parentage in the box tree (#41884)
This patch adds a weak reference to the parent box in `LayoutBoxBase`,
so that boxes can refer to their parent. This will help during future
changes for incremental layout.

Testing: Not needed, no behavior change

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2026-01-14 14:40:12 +00:00
Martin Robinson
d5dcdd6e06 script: Add support for setting the edit point with the mouse in <input> and <textarea> (#41906)
This change updates and implements the old `query_text_index` layout
query to properly look for the glyph index of a point within a node's
`Fragment`s. This should work properly with the shadow DOM of both
`<input>` and `<textarea>` elements. In particular, multiple lines are
supported.

Caveats:
 - `<input>` and `<textarea>` that are transformed are currently not
   supported. This will happen in a followup.
 - For multi-line inputs, we should be finding the text offset of the
   nearest line that is within the block range of the click. This will
   happen in a followup.

Testing: This change adds two Servo-specific WPT-style tests. These are
Servo-specific because the behavior of clicking in text fields isn't
fully specified.

Fixes: #35432
Fixes: #10083

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2026-01-14 13:23:43 +00:00
Oriol Brufau
0aae90ffe8 layout: Let floats know that margins can collapse thru phantom lines (#41812)
Phantom line boxes allow margins to collapse through them. But in the
sequential layout state we were assuming that was not the case, so we
were placing floats incorrectly.

Testing: Adding 2 tests
Fixes: #41794
Fixes: #41734

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-13 19:25:53 +00:00
Oriol Brufau
bb74a756a7 layout: Don't wrap InlineItem with ArcRefCell in LayoutBox (#41877)
Changes `LayoutBox::InlineLevel()` to have a raw `InlineItem` instead of
an `ArcRefCell<InlineItem>`. `InlineItem` is an enum where all the
options already use `ArcRefCell`, so the outer `ArcRefCell` wasn't
really necessary.

Testing: Not needed, no behavior change

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2026-01-13 16:29:31 +00:00
Martin Robinson
30fa32714e Remove Servo's custom Range implementation (#41880)
Servo `Range` type probably predates the `Range` type in the standard
library. The fact that there are two `Range` types in Servo is often
very confusing. The internal type is only used for byte indices in glyph
runs. This change removes the internal generic `Range` in favor of a
small wrapper around a native Rust `Range`.

It's likely that there are more improvements that could be done here,
such as reusing `Utf8CodePointIndex`, but this code is going to change a
lot soon and these ranges might soon be replaced with actual glyph
ranges. We are going to be looking to remove `TextByteRange` entirely.
This is just an intermediate step.

Testing: This should not change behavior and is thus covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2026-01-13 15:31:47 +00:00
Martin Robinson
e44cfc9ea5 layout: Store FontMetrics in an Arc (#41876)
This decreases the size of `TextFragment` by 66 bits and also allows
sharing empty metrics for text runs without fonts. Many `TextFragments`
should share the same font so these 66 bits should reflect reality even
though the `FontMetrics` is now stored on the heap.

Testing: This should not change behavior so is covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
2026-01-13 12:48:55 +00:00
Oriol Brufau
c9613a25ec layout: Fix intrinsic size of block-in-inline not marked as depending on block constraints (#41655)
When a block-level that depends on block constraints is inside an inline
formatting context, then the intrinsic size of the entire IFC needs to
be marked as depending on block constraints too.

Testing: Adding new test
Fixes: #41630

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-03 21:22:11 +00:00
Oriol Brufau
147d5656df layout: Handle block-in-inline dependency on block constraints (#41624)
When a block-level that depends on block constraints is inside an inline
formatting context, then the entire layout of the IFC needs to be marked
as depending on block constraints too.

Testing: Adding new test
Fixes: #41623

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-02 17:49:45 +00:00
Oriol Brufau
accda387b4 layout: Stop splitting inline boxes when they contain block-levels (#41492)
We now follow the same approach as Blink: sequences of block-levels in
an inline formatting context get wrapped in an anonymous block, which is
treated as a line item. Inline ancestors are no longer split.

This means that inline elements will now generate a single inline box,
and the box tree makes more sense in general. This will help for
incremental layout.

This also means that block-level elements will now be properly affected
by effects like opacity of filters set on inline ancestors.

Recently, WebKit has done some similar work, but without the anonymous
blocks. We might also consider removing them in the future.

Google's explainer:
https://docs.google.com/document/d/15kgdIHhb9EVNup6Ir5NWwJxpzY5GH0ED7Ld3iMW3HlA/

Testing: Several tests are now passing
Fixes: #39813

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2026-01-01 17:41:37 +00:00
Oriol Brufau
cbe6b03c5b layout: Take floats into account for the intrinsic sizes of an inline FC (#41513)
The computation of the min-content and max-content sizes of an inline
formatting context was ignoring floated contents.

Now we will take them into account.

Testing: One test is now passing. Note there isn't much test coverage
because there is no spec, and browsers aren't fully interoperable. More
tests should be added as part of
https://github.com/w3c/csswg-drafts/issues/9120
Fixes: #3923

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-12-25 22:40:24 +00:00
Freya Arbjerg
c2e7484ef7 layout: Fix caret never rendering following a newline (#41330)
Fixes caret rendering when the caret is placed a the start of a line

```
Hello,

{here}World!
```

Also adds a utility function to the `range` crate.

Note that the rendering of the caret on many non-empty lines is still
broken due to the presence of several other bugs

Testing: I ran unit tests. Add a new reftest
Fixes: Part of #40839

---------

Signed-off-by: Freya Arbjerg <git@arbjerg.dev>
2025-12-18 22:31:25 +00:00
minghuaw
7e665f6bb9 layout: Remove centralized FontKeyAndMetrics list from the InlineFormattingContext (#41208)
As previously described in #41150 ,
`InlineFormattingContext::font_metrics` contains info that are easily
accessible via `FontRef`, and the `FontInstanceKey` is already cached in
the `FontContext`. Instead of keeping an index and obtaining these info
using the index, one should be able to obtain the same info via a
`FontRef`.

Testing: This should not change the expected behavior and the current
unit tests and WPT testcases should be sufficient.
Fixes: #41150

---------

Signed-off-by: minghuaw <michael.wu1107@gmail.com>
2025-12-15 16:52:43 +00:00
Martin Robinson
e9f372ca41 layout: Add a BaseFragment that contains shared properties of all Fragment types (#41282)
This reverts commit f5ecb34577 and also
merges more shared properties into `BaseFragment`. The goal here is to
eventually also store a reference to the parent fragment in this
`BaseFragment` as well.

Testing: This should not change behavior in an observable way, so is
covered by WPT tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2025-12-15 15:17:13 +00:00
Mukilan Thiyagarajan
07cf9a9175 compositor: Rename RenderingGroupId to PainterId (#40307)
And let the Compositor create the `PainterId` when it creates a new
`Painter`. Also make inline formatting code take `LayoutContext` rather
than threading it via the `InlineFormattingContextBuilder`.


Testing: Should preserve existing behavior, so covered by existing
tests.

Signed-off-by: Mukilan Thiyagarajan <mukilan@igalia.com>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
2025-10-31 11:49:24 +00:00
Simon Wülker
c14795b33a Replace "map(..).unwrap_or(false)" with "is_some_and(..)" everywhere (#40244)
Testing: This change is a simple cleanup and should mostly be covered by
existing tests.

Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
2025-10-28 21:19:04 +00:00
Oriol Brufau
7e39dfdcee layout: Cleanup clear_fragment_layout_cache() (#40040)
`LayoutBox::clear_fragment_layout_cache()` was just redirecting to the
`clear_fragment_layout_cache()` method of inner structures, until
reaching `LayoutBoxBase` where the work is actually done.

So this patch gets the `LayoutBoxBase` first, and then calls the method
on it. To do so, `LayoutBox::with_base_fold()` is added as a non-mutable
version of the existing `LayoutBox::with_base_mut_fold()`. But since we
the return value doesn't matter, `LayoutBox::with_each_base()` is also
added as a tiny helper around `LayoutBox::with_base_fold()`.

Testing: Not needed, no behavior change

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-10-25 15:55:13 +00:00
spuradage
113472e171 chore: remove repetitive word in comment (#39948)
remove repetitive word in comment

Signed-off-by: spuradage <spuradage@outlook.com>
2025-10-17 08:48:23 +00:00
Rocketjumper
bc823950b0 layout: Let min-content size break before each unbreakable segment run (#39830)
We were only allowing a soft wrap opportunity before the first run in a
`TextRunSegment`, but not before the other runs. For example, this meant
that the min-content size of `中文中文` would be 4ic instead of just 1ic.

The change brings the logic in `ContentSizesComputation::process_item()`
closer to `TextRunSegment::layout_into_line_items()`.

Testing: Adding new tests. There is also a new failure because we don't
support `word-break: auto-phrase`.
Fixes: #39728

---------

Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2025-10-16 10:00:53 +00:00
Oriol Brufau
6e72455b5d layout: Make inline boxes in phantom line boxes be 0px tall (#39834)
The height of an inline box is typically decided by font metrics, but if
it's on a phantom line box, then it should be zero.

This is hard to observe, because the contents of a phantom line box are
typically invisible, but it's noticeable on these cases:
 - If the inline has a box shadow or an outline (not on all browsers).
 - If the inline establishes a containing block for abspos descendants.
 - When getting the size using JS APIs.

Testing: Some existing tests pass, also adding a new one
Fixes: #39648

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-10-13 15:47:20 +00:00
Oriol Brufau
f4ad81de58 layout: Unify line height resolution (#39688)
Both `flow/inline/line.rs` and `flow/inline/mod.rs` had a function to
resolve the `line-height` value. They were pretty much the same, except
that the former wasn't handling single-line text inputs. But I don't
think it matters, since the text inside such input can't be styled with
`vertical-align: top` or `bottom`.

Thus this patch unifies the logic. And to simplify the callers, now this
function accepts a `&InlineContainerStateFlags` instead of a bool for
identifying single-line text inputs.

Testing: There shouldn't be any behavior change.
This is part of #39664

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-10-06 20:20:27 +00:00
Narfinger
e64f021550 Allow WebViews and fonts to have a RenderingGroupId. (#39140)
Motivation: The font cache currently has to store a cache of Keys which
need to be given by the webrender instance.
Having a cache for every WebViewId in the future when we have every
webview have the different webrender::DocumentId might be too wasteful
to store this key cache per DocumentId. This proposes to include in the
WebViewId another id, the RenderingGroupId. This id can be easily
changed
to be equivalent to the DocumentId when we support multiple DocumentIds
for a unique Webrender instance.
Additionally this will keep it easier to integrate the currently out of
tree patches for multiple rendering contexts with different webrenders.


Change:
We introduce the RenderingGroupId in the WebViewId and allow a method to
extract it. The font key cache uses this cache
and forwards it to the Compositor when requesting new changes. The
compositor currently ignores this id.
Additionally, the WebView can return the RenderingGroupId. The WebViewId
also has an appropiate constructor for specifying a RenderingGroupId.
Because there currently will be only one RenderingGroupId the
performance will be minimal.


Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>

Testing: This should be covered by WPT tests and normal browsing
behavior works fine.

---------

Signed-off-by: Narfinger <Narfinger@users.noreply.github.com>
2025-09-29 10:01:56 +00:00
Oriol Brufau
18b3e5fe21 layout: Make lines non-phantom if they have inline padding/border/margin (#39058)
According to https://drafts.csswg.org/css-inline/#invisible-line-boxes,
if a line box contains non-zero inline-axis margins, padding or borders,
then it can't be phantom.

Therefore, this patch makes adds a `has_inline_pbm` flag to the line.
Note that we can't use the `has_content` flag, because that would add a
soft wrap opportunity between the padding/border/margin and the first
content of the line.

The patch also renames `InlineFormattingContext::had_inflow_content` to
`has_line_boxes`, which is what we care about for collapsing margins
through.

Testing: Adding new tests
Fixes: #39057

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-16 22:18:46 +00:00
Oriol Brufau
4451ce0ef1 layout: Fix propagation of overflow to viewport (#39173)
This patch refactors the logic for propagating overflow to the viewport,
fixing various issues:
- Now we won't propagate from the root element if it has no box. Note
the fix isn't observable in Servo because we lack scrollbars.
- If the first `<body>` element has no box, we won't keep searching for
other `<body>` elements. This deviates from the spec, but aligns us with
other browsers.
- We won't propagate from the `<body>` if it has no box. We were already
handling `display: none` but not `display: contents`. This deviates from
the spec, but aligns us with other browsers.

Also, when we flag the root or `<body>` as having propagated `overflow`
to the viewport, we retrieve the `LayoutBoxBase`. Therefore, now we get
the computed style from the `LayoutBoxBase` in a single operation,
instead of first retrieving the style from the DOM element and then
getting the `LayoutBoxBase` from the box.

Testing: Adding more tests. We were only failing one of them, but it's
hard to test the fixes given that we don't show scrollbars. The tests
that were already passing are useful too, e.g. Firefox fails one of
them.

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-16 20:53:46 +00:00
Oriol Brufau
c11670b067 layout: Take text-indent into account in min/max-content inline sizes (#39230)
The min-content and max-content inline sizes of an inline formatting
contentext need to take `text-indent` into account. Note it can be set
to a negative amount, so the `ContentSizesComputation` logic needs some
tweaks to handle it well.

Testing: Fixes various WPT tests

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-13 06:20:39 +00:00
Oriol Brufau
9e9bd80bba layout: Add style to ConstraintSpace and IndefiniteContainingBlock (#39229)
They only had the writing mode, now they will have the entire computed
style.
This is needed for #39230.

Testing: Not needed, no behavior change

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-11 21:53:14 +00:00
Oriol Brufau
433a6bf47b layout: Set baseline even if line box has no fragment (#39235)
`InlineFormattingContextLayout::finish_current_line_and_reset()` has an
early return in case the line has no fragment. However, if the line only
has a forced line break, then we still need to set the baseline.

Testing: Adding new test.

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-10 05:17:25 +00:00
Oriol Brufau
5b9a23ebdb layout: Add method to retrieve only the first LayoutBoxBase of a box (#39165)
A LayoutBox typically has one LayoutBoxBase, or none in the case of
`LayoutBox::DisplayContents`. However, `LayoutBox::InlineLevel` can
contain multiple inline items, each one with its base. But since things
like the style or the fragment flags should be the same for all of them,
getting the first base is sometimes enough.

Testing: not needed, no change in behavior.

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-05 16:19:58 +00:00
Oriol Brufau
6e92e1d7d5 layout: Avoid setting baseline for phantom line boxes (#39055)
Phantom line boxes should be treated as non-existing for most purposes,
so don't let them affect the baseline of their block container.

Testing: An existing test passes, and also adding a new one which
doesn't rely on `<button>`

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
2025-09-01 02:52:17 +00:00
Shubham Gupta
d8ff9c7a08 layout: Use overflow: visible if overflow was propagated to viewport (#38598)
The `overflow-*` values of either the root element or the `<body>` get
propagated to the viewport. However, we were missing this part:
> The element from which the value is propagated must then have a used
`overflow` value of `visible`.

See https://drafts.csswg.org/css-overflow/#overflow-propagation

Testing:
 - `css/cssom-view/scrolling-quirks-vs-nonquirks.html`
 - `css/css-overflow/overflow-body-propagation-007.html`
 - `css/css-overflow/overflow-body-propagation-008.html`
 - `css/css-overflow/overflow-body-propagation-009.html`
 - `css/css-overflow/scrollable-overflow-with-nested-elements-001.html` 
 - `css/css-overflow/scrollable-overflow-with-nested-elements-002.html` 
 - `css/css-overflow/scrollable-overflow-with-nested-elements-003.html`
 - `css/css-overflow/scrollable-overflow-with-nested-elements-004.html`
 - `css/css-overflow/scrollbar-gutter-scroll-into-view.html`

Failures:
 - `css/css-overflow/overflow-body-propagation-010.html`
   Failing because of missing support for `contain: paint`.
- `css/css-overflow/scrollable-overflow-with-nested-elements-005.html`
Failing because of wrong `data-expected-height`, but correct
`data-expected-scroll-height` which is core of this PR.
`data-expected-height` can be dealt separately.


Fixes: #38248

---------

Signed-off-by: Shubham Gupta <shubham13297@gmail.com>
Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2025-08-20 14:21:51 +00:00
Martin Robinson
ee7c1d9109 layout: Use ServoThreadSafeLayoutNode in more places (#38626)
Use `ServoThreadSafeLayoutNode` in more places in layout rather than
`ServoLayoutNode`. The former is meant to be used during layout, but
layout 2020 was written against the latter. In general, this reduces the
amount of conversion to the thread-safe version in many places in
layout.

In addition, an unused iterator from the `script` crate
`ServoThreadSafeLayoutNodeChildrenIterator` is replaced with the child
iterator from `layout`. The `layout` version must be directly in
`script` now as it uses the dangerous variants of `next_sibling` and
`first_child`, which allow encapsulating the unsafe bits into one
module.

This will ultimately be useful for storing the layout data of
pseudo-element children of pseudo-elements properly.

Testing: This should not change any behavior and thus is covered by
existing tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
Co-authored-by: Oriol Brufau <obrufau@igalia.com>
2025-08-13 14:55:19 +00:00