Commit graph

286 commits

Author SHA1 Message Date
Callum Law
dca80ad5eb LibWeb: Account for animated values when computing font
Computing the font for an element in `compute_font` is premature since
we are yet to apply animated properties - instead we should compute the
value on the fly (with a cache to avoid unnecessary work) to ensure we
are respecting the latest values
2025-12-05 10:03:15 +00:00
Callum Law
6c236d04d8 LibWeb: Separate font computation logic from StyleComputer
Font computation and loading is distinct enough from style computation
that it makes more sense to have this in it's own class.

This will be useful later when we move the font loading process to
`ComputedProperties` in order to respect animated values.
2025-12-05 10:03:15 +00:00
Callum Law
dfa47d9ed6 LibWeb: Simplify handling of font-variation-settings
Since we resolve any relative lengths at compute time there's no need
for the value to be passed around as a `NumberOrCalculated` and we can
just resolve it within `ComputedProperties::font_variation_settings`.

The only place this is used it is used with value_or so there's no need
to return it is an `Optional`.

This is only used for loading fonts (which occurs during style
computation) so there's no need to store it in `ComputedValues`
2025-12-05 10:03:15 +00:00
Sam Atkins
d717dd64b3 LibWeb: Make Animation's owning element an AbstractElement
From the spec:
> The owning element of a transition refers to the element or
  pseudo-element to which the transition-property property was applied
  that generated the animation.

https://drafts.csswg.org/css-transitions-2/#owning-element

Previously we only stored the element.
2025-12-03 13:29:51 +01:00
Sam Atkins
b61c857c64 LibWeb/CSS: Use AbstractElement animation helpers 2025-12-03 13:29:51 +01:00
Callum Law
e935dfbf5b LibWeb: Simplify compute_style_value_list 2025-12-02 12:47:10 +00:00
Callum Law
4034ce995a LibWeb: Account for transition-{delay,duration} always being a list
This fixes the optimization implemented in 46acdbd that was no longer
working after e937f5d
2025-12-02 11:45:46 +00:00
Sam Atkins
d9abfdf2ab LibWeb/CSS: Allow non-StyleValueLists in animation properties
Reverts 51f694c6af and
b52beb5105.

The animation-* properties are in an awkward place currently, where
they *should* be lists, and a lot of our code acts as if they are, but
actually we parse them as single values. The above commits caused a few
WPT tests to crash - see link below. I've imported one of them to
prevent future regressions.
https://wpt.fyi/results/css/css-typed-om/the-stylepropertymap/properties?diff&filter=ADC&run_id=6293362870321152&run_id=5123272984494080
2025-12-02 09:48:32 +00:00
Callum Law
51f694c6af LibWeb: Simplify compute_style_value_list 2025-12-01 10:16:41 +00:00
Callum Law
11a9b5a67b LibWeb: Simplify handling of font-family
We know `parse_comma_separated_value_list` always returns a
`StyleValueList`
2025-12-01 10:16:41 +00:00
Callum Law
4c4fa5c9cd LibWeb: Simplify expansion of background-position shorthand
We know that this is always a `StyleValueList`
2025-12-01 10:16:41 +00:00
Callum Law
1af0364cfc LibWeb: Store last_css_animation_play_state on anim instead of effect
Since it is the animation rather than the effect which has a play state
it makes more sense to store it here
2025-12-01 10:16:41 +00:00
Callum Law
63538c8a75 LibWeb: Prefer transitioned property values over important
CSS transitions have a higher precedence in the cascade than important
properties
2025-11-28 16:15:49 +00:00
Timothy Flynn
3dce6766a3 LibWeb: Extract some CORS and MIME Fetch helpers to their own files
An upcoming commit will migrate the contents of Headers.h/cpp to LibHTTP
for use outside of LibWeb. These CORS and MIME helpers depend on other
LibWeb facilities, however, so they cannot be moved.
2025-11-27 14:57:29 +01:00
Psychpsyo
693dd7b6f6 LibWeb: Avoid unnecessary sorting work when getting animations
This way, the list is not re-sorted on every recursive call.
2025-11-26 22:19:23 +01:00
Callum Law
46acdbd10a LibWeb: Avoid registering unnecessary transitions
Reduces time spent in `StyleComputer::compute_properties` when loading
https://en.wikipedia.org/wiki/2023_in_American_television from ~37% to
~13%
2025-11-23 09:43:24 +01:00
Callum Law
67a3d0f1aa LibWeb: Consider cancelled (idle) transitions as "completed" 2025-11-23 09:43:24 +01:00
Callum Law
b2b889e1da LibWeb: Ensure registered transitions are reflective of properties
Previously we would only update these if:
a) We had a cascaded value for `transition-property`
b) The source of that cascaded value had changed since we last
   registered transitions

This meant that there were a lot of changes we didn't apply:
 - Changes exclusively to properties other than `transition-property`
   (e.g. `transition-duration`, `transition-behavior`, etc)
 - Removing the `transition-property` property
 - Updating the `transition-property` property in a way that didn't
   change it's source (e.g. setting it within inline-style)

Unfortunately this does mean that we now register transitions for all
properties on most elements since "all" is the initial value for
"transition-property" which isn't great for performance, but that can be
looked at in later commits.
2025-11-23 09:43:24 +01:00
Callum Law
025274cd86 LibWeb: Don't deassociate animations when deregistering transitions
Also renames the `clear_transitions` function to clarify this doesn't
affect the associated transition animations.

This fixes an issue where transitions weren't being cancelled when the
relevant transition-property entry was no longer present
2025-11-23 09:43:24 +01:00
Hendiadyoin1
bcd01da91d AK: Use Deducing this for OptionalBase
This is taken from and akin to
https://github.com/SerenityOS/serenity/pull/25894
2025-11-20 16:27:07 +01:00
Andreas Kling
66263f142b LibWeb: Add StyleScope to keep style caches per Document/ShadowRoot
Before this change, we've been maintaining various StyleComputer caches
at the document level.

This made sense for old-school documents without shadow trees, since
all the style information was document-wide anyway. However, documents
with many shadow trees ended up suffering since any time you mutated
a style sheet inside a shadow tree, *all* style caches for the entire
document would get invalidated.

This was particularly expensive on Reddit, which has tons of shadow
trees with their own style elements. Every time we'd create one of their
custom elements, we'd invalidate the document-level "rule cache" and
have to rebuild it, taking about ~60ms each time (ouch).

This commit introduces a new object called StyleScope.

Every Document and ShadowRoot has its own StyleScope. Rule caches etc
are moved from StyleComputer to StyleScope.

Rule cache invalidation now happens at StyleScope level. As an example,
rule cache rebuilds now take ~1ms on Reddit instead of ~60ms.

This is largely a mechanical change, moving things around, but there's
one key detail to be aware of: due to the :host selector, which works
across the shadow DOM boundary and reaches from inside a shadow tree out
into the light tree, there are various places where we have to check
both the shadow tree's StyleScope *and* the document-level StyleScope
in order to get all rules that may apply.
2025-11-14 22:05:33 +01:00
Timothy Flynn
d8ec204be8 LibWeb: Do not return an exception from fetching CSS resources
The exception returned here is never used or logged. Let's just return
a null request, to make it clearer that these are not exceptions shown
to the user (thus not observable).
2025-11-12 18:30:16 +01:00
Tim Ledbetter
0ec97eabab LibWeb: Extract device_pixels_per_css_pixel getter from loop
This reduces time spent in StyleComputer::compute_property_values() by
~10% when loading https://html.spec.whatwg.org/
2025-11-12 11:21:17 +01:00
Tim Ledbetter
df23b42b37 LibWeb: Don't store custom name on StyleProperty
This reduces time spent in `~StyleProperty()` from 2.18% to 0.67% on
https://cloudflare.com.
2025-11-12 11:19:37 +01:00
Callum Law
55afa9d37e LibWeb: Avoid unnecessary work when element display property changes
`play_or_cancel_animations_after_display_property_change` is called
whenever an element is inserted or removed, or it's display property
changes, but it is only required to run if we actually have animations
to play or cancel.

Reduces time spent in the aforementioned function from ~2% to ~0.03%
when loading https://en.wikipedia.org/wiki/2023_in_American_television
2025-11-11 08:35:39 +00:00
Callum Law
cfc22a4075 LibWeb: Dont try to transition custom properties
Fixes a crash introduced in dd9d6d2
2025-11-11 07:53:41 +00:00
Tim Ledbetter
61af399b50 LibWeb: Remove unnecessary lowercase name property from Attr 2025-11-10 22:55:38 +01:00
Callum Law
dd9d6d22ee LibWeb: Iterate over fewer properties in start_needed_transitions
We don't need to iterate every property in start_needed_transitions,
only those that appear in transition-property or have an existing
transition

Reduces the time spent in start_needed_transitions from ~5% to ~0.03%
when loading https://en.wikipedia.org/wiki/2023_in_American_television
2025-11-10 12:11:36 +01:00
Timothy Flynn
ac246caa0c LibWeb: Remove now-unused Resource and ResourceClient
And deal with the fallout of transitive includes.
2025-11-05 18:27:36 +01:00
Tim Ledbetter
8d81421526 LibWeb: Cache font matching algorithm results
The majority of time in `compute_font()` was spent in
`font_matching_algorithm()` repeatedly computing the same values. We
now cache these values to avoid unnecessary work.
2025-11-05 09:12:02 +01:00
norbiros
d8e80718d7 LibWeb/CSS: Absolutize values while computing font variation settings
Makes the `compute_font_variation_settings` function match other
`compute_font_*` by absolutizing values passed into them. It resolves a
crash related to using `siblings-count` inside `font-variation-settings`

Co-authored-by: Sam Atkins <sam@ladybird.org>
2025-11-04 21:44:32 +01:00
norbiros
3829a85fde LibWeb: Add basic variable font support
Integrates the new `FontVariationSettings` from LibGfx into LibWeb to
enable initial variable font functionality. Currently, only the `wght`
(weight) axis is fully supported and tested. This update also introduces
support for the CSS `font-variation-settings` property.
2025-11-04 21:44:32 +01:00
Marcos Del Sol Vives
abcc5bc16d LibWeb: Support multiple emoji/symbol fonts 2025-11-03 09:56:25 +00:00
Callum Law
ef8dcb2a43 LibWeb: Remove unused code in compute_properties 2025-11-02 23:54:00 +01:00
Callum Law
a95cde3660 LibWeb: Separate CSSAnimation::animationName from Animation::id 2025-11-02 23:54:00 +01:00
Callum Law
2447b8a759 LibWeb: Ignore non-animatable properties in keyframes
Gains us 21 WPT tests
2025-11-02 23:54:00 +01:00
Callum Law
ed0b741a26 LibWeb: Remove special handling of all property for animations
There were a couple places that we had special handling for the `all`
property but since d31a58a was merged we can treat it the same as any
other shorthand
2025-11-02 23:54:00 +01:00
Lorenz A
f54793315c LibWeb: Adjust buttons computed display style 2025-11-01 13:02:44 +00:00
Callum Law
c8f345356e LibWeb: Maintain property importance into ComputedProperties
Previously we wouldn't propagate this from CascadedProperties to
ComputedProperties
2025-10-27 09:51:50 +00:00
Callum Law
823dd11b67 LibWeb: Add ComputedProperties::set_property_without_modifying_flags
There are a few places in style computation where we want to update the
value of a property without modifying the inherited or important flags.

Previously we would look up these flags and pass them to the normal
`set_property` method but this is unnecessary and was easy to forget to
do.
2025-10-27 09:51:50 +00:00
Callum Law
84762021b8 LibWeb: Support triggering multiple animations per animation property
We also now use the computed (rather than cascaded) values when
triggering animations.
2025-10-27 09:48:25 +00:00
Callum Law
18477b0d84 LibWeb: Promote animation-composition values to enum
This brings us in line with the other `animation-*` enum properties
(`animation-play-state` and `animation-fill-mode`)
2025-10-27 09:48:25 +00:00
Callum Law
8417d74328 LibWeb: Parse transition property as a coordinating list shorthand
We don't need all this specific logic for parsing the `transition`
property - we also now maintain `none` as such until use time which
gains us a couple extra tests
2025-10-23 10:09:11 +01:00
Callum Law
fc5cdd69a0 LibWeb: Extract transition coordination logic to reusable method
This will be useful for other coordinating list property groups (e.g.
background and animation)
2025-10-23 10:09:11 +01:00
Callum Law
e78cb71eb3 LibWeb: Dont skip invalid properties when coordinating transition list
Unrecognized property names should still be kept in the list to preserve
matching of indices (e.g. that the Nth property should associate with
the Nth duration)
2025-10-23 10:09:11 +01:00
Callum Law
f49cf75d44 LibWeb: Don't pass unnecessary PropertyComputationDependencies struct
Since we now have access to the `AbstractElement` through the
`ComputationContext` we can just set the flag that this element relies
on tree counting functions directly, no need to pass this struct around.
2025-10-22 00:01:30 +02:00
Callum Law
5b9a36b172 LibWeb: Pass AbstractElement in ComputationContext
Passing the `AbstractElement` rather than the
`TreeCountingFunctionResolutionContext` allows us to only compute the
resolution context when necessary (i.e. when we actually need to resolve
a tree counting function)
2025-10-22 00:01:30 +02:00
Andreas Kling
d17f666a8c LibWeb: Better CSS inheritance for nodes that represent a pseudo-element
When we compute style for elements inside a UA-internal shadow tree that
represent a pseudo-element (e.g ::placeholder), we actually run the
StyleComputer machinery for (host element :: pseudo-element).

While that lets us match the correct selectors, it was incorrectly
applying CSS inheritance, since we'd also then inherit from whatever was
above the host element in the tree.

This patch fixes the issue by introducing an inheritance override in
AbstractElement and then using that to force inheritance from whatever
is actually directly above in the DOM for these elements instead of
jumping all the way up past the host.

This fixes an issue where `text-align: center` on input type=text
elements would render the main text centered but placeholder text was
still left-aligned.
2025-10-21 16:42:00 +02:00
Callum Law
2404f95e03 LibWeb: Invalidate style for tree counting functions when required
We mark any element that relies on tree counting functions as needing a
style update when a sibling is inserted/removed.
2025-10-20 16:12:08 +01:00
Callum Law
9cd23e3ae5 LibWeb: Compute and propagate tree-counting function resolution context
Tree counting functions should be resolved at style computation time -
to do this we will need to know the element's sibling count and index.

This commit computes that information and propagates it to the various
`StyleValue::to_computed_value` methods.
2025-10-20 16:12:08 +01:00