Commit Graph

2229 Commits

Author SHA1 Message Date
Callum Law
124b142ae4 LibWeb: Simplify z-index handling in ComputedProperites
We now implement clamping and up-on-half rounding for all <integer>
values generically so this is no longer needed
2026-03-26 12:30:01 +01:00
Callum Law
b86377b9dc LibWeb: Clamp CSS <integer> value to i32 at parse time
This matches the behavior of other browsers. Previously we implemented
this at used-value time for z-index specifically.
2026-03-26 12:30:01 +01:00
Callum Law
0ab06e119e LibWeb: Use i32 max for clamping 'infinite' calculated integers
This required us to change our range values from `float`s to `double`s
since `float` can't exactly represent i32 max
2026-03-26 12:30:01 +01:00
Callum Law
0e8956ee30 LibWeb: Round up on half when rounding to nearest integer in CSS 2026-03-26 12:30:01 +01:00
Callum Law
cdc264a62e LibWeb: Validate nested contents when parsing declaration value
Previously we automatically assumed that contents inside functions and
blocks were valid, now we actually check them.
2026-03-26 01:11:39 +00:00
Callum Law
93bd066639 LibWeb: Ensure <declaration-value>? descriptors are valid
Currently this only applies to the `@property` `syntax` descriptor.

As with custom properties in the previous commit we assumed that any
consumed values were valid but that's not the case.
2026-03-26 01:11:39 +00:00
Callum Law
46bebf44c5 LibWeb: Ensure custom property definition is valid <declaration-value>?
Previously we assumed that consumed declarations were always valid but
that isn't the case
2026-03-26 01:11:39 +00:00
Callum Law
6afe2ff27b LibWeb: Limit <ident> to <custom-ident> in @property/syntax
The definition of syntax in the "css-properties-values-api" spec (which
is used for the `@property/syntax` descriptor) is slightly different
from the definition of `<syntax>` in the "css-values" spec (which we
implement) in that it limits literal idents to exclusively
`<custom-ident>`s (i.e. not CSS-wide keywords or "default").

`<custom-ident>`s are also case-sensitive so that behavior is
implemented for syntax matching here as well
2026-03-26 01:11:39 +00:00
Callum Law
cfc2e64b4b LibWeb: Add is_valid_custom_ident function
We repeat this pattern in a couple of places so let's add a single
helper method
2026-03-26 01:11:39 +00:00
Callum Law
283f8e46a4 LibWeb: Only match alternative syntax if entire stream consumed
Previously we would consider an alternative syntax child to be a match
as long as parsing produced a value, even if there were trailing tokens
(which would later invalidate it within `parse_with_a_syntax`). This
meant that we wouldn't consider later alternatives which may actually
produce a valid match.
2026-03-26 01:11:39 +00:00
Callum Law
614a5cf33e LibWeb: Set context for parsing against <foo-percentage> syntax
This means that we correctly parse dimension percentage mixes (i.e.
`calc(10px + 10%)` is a valid `<length-percentage>`)
2026-03-26 01:11:39 +00:00
Callum Law
ed2909674f LibWeb: Add computationally independent check for custom properties
Registered custom properties only accept "computationally independent"
values for their initial value
2026-03-26 01:11:39 +00:00
Callum Law
3b3caa96d9 LibWeb: Don't pass around unnecessary element when parsing with syntax
This wasn't actually used anywhere and was just being recursively passed
around
2026-03-26 01:11:39 +00:00
Psychpsyo
bd91567863 Meta: Ensure that idl files link to draft specs 2026-03-25 16:02:04 +00:00
Luke Wilde
df32da5e86 LibWeb: Make every HTMLElement potentially form-associated
This can be the case for form-associated custom elements, where any
HTML element can be form-associated.
2026-03-25 13:18:15 +00:00
Callum Law
500ca417ce LibWeb: Map logical aliases at compute rather than cascade time
This requires us to front load computation of writing-mode and direction
before we encounter any logical aliases or their physical counterparts
so that we can create a mapping context.

Doing this at compute rather than cascade time achieves a few things:
 1) Brings us into line with the spec
 2) Avoids the double cascade that was previously required to compute
    mapping contexts
 3) We now compute values of logical aliases, while
    `style_value_for_computed_property` maps logical aliases to their
    physical counterparts, this didn't account for all cases (i.e. if
    there was no layout node, Typed OM, etc).
 4) Removes a hurdle to moving other upstream processes (i.e. arbitrary
    substitution function resolution, custom property computation) to
    compute time as the spec requires.
2026-03-25 12:53:22 +00:00
Callum Law
127d10c048 LibWeb: Record cascaded properties relative order
This will be required to perform logical alias mapping at compute time
since we need to know the relative order of logical and physical
property pairs
2026-03-25 12:53:22 +00:00
Callum Law
d0a868dcfb LibWeb: Add method to map from physical to logical property
This will be required in later commit.

The implementation could do with some optimization
2026-03-25 12:53:22 +00:00
Callum Law
d284ff96fe LibWeb: Nest logical property group physical properties
In a later commit we will also add logical properties in here
2026-03-25 12:53:22 +00:00
Callum Law
9bd4dff1aa LibWeb: Remove outdated AD-HOC comment in parse_media_feature
The spec has been updated so this is no longer ad-hoc
2026-03-25 08:18:35 +00:00
Tim Ledbetter
77b9fcf7f9 LibWeb+LibWebView: Select generic font families based on requested style
Pass the requested font weight and slope to generic_font_name() and use
them to select the most suitable font family. Families that have the
exact requested weight are strongly preferred, followed by families with
more style variety.

This ensures that when CSS requests bold text with a generic font family
like `serif`, we select a family that actually supports bold weights
rather than one that only has regular weight.

Co-authored-by: Jelle Raaijmakers <jelle@ladybird.org>
2026-03-24 15:08:24 +01:00
Callum Law
02db1b942b LibWeb: Improve handling of calculated values in <ratio>
We no longer try to resolve calculated values at parse time which means
we support relative lengths.

We now clamp negative values rather than rejecting them at parse time.

Parsing has been inlined into `parse_ratio_value` and `parse_ratio` has
been removed since `parse_ratio_value` was the only caller
2026-03-24 14:00:01 +00:00
Callum Law
f347be8206 LibWeb: Store underlying RatioStyleValue values as StyleValues
Previously we stored a fully formed `Ratio` - this restricted us from
supporting calculated values which will be implemented in a later commit
2026-03-24 14:00:01 +00:00
Callum Law
9f8ffc3ad0 LibWeb: Move RatioStyleValue methods into implementation file
We will be expanding this class in a later commit so it makes sense to
have an implementation file
2026-03-24 14:00:01 +00:00
Callum Law
201b609450 LibWeb: Store MediaFeatureValue underlying value as StyleValue
This simplifies handling in some cases and gets us a step closer to
removing the `OrCalculated` classes
2026-03-24 14:00:01 +00:00
Callum Law
860ec7aea3 LibWeb: Store type of MediaFeatureValue explicitly
In a later commit we will be storing the value as a `StyleValue` and
thus won't be able to easily get it's type
2026-03-24 14:00:01 +00:00
Callum Law
d55e0a858a LibWeb: Add reusable Resolution::from_style_value method 2026-03-24 14:00:01 +00:00
Callum Law
b07ef9435e LibWeb: Remove fallback behavior from color_or_fallback
All of the properties using this method only accept color values so the
fallback behavior wasn't required
2026-03-24 13:56:01 +01:00
Callum Law
e2f2f813b6 LibWeb: Handle accent-color auto value explicitly
`accent-color` is the only user of the fallback functionality of
`color_or_fallback`, by handling this explicitly we can remove that
fallback functionality in a later commit.

Also includes a couple of improvements for `accent-color` specifically:
 - We don't set it in `ComputedValues` twice.
 - `ComputedProperties::accent_color` returns a non-optional value
   (since we always have one)
 - `ComputedProperties::accent_color` takes a `ColorResolutionContext`
   instead of generating one itself from a `LayoutNode`, this will allow
   us to reuse shared resolution contexts in the future
2026-03-24 13:56:01 +01:00
Charlie Tonneslan
e564eff402 LibWeb/CSS: Fix typo overriden -> overridden in comments 2026-03-24 11:08:23 +01:00
Rob Ryan
281c2ce545 LibWeb: Seed ancestor context for computing style with no layout node 2026-03-24 05:02:48 +01:00
Andreas Kling
a5db4c874e LibWeb: Skip declaration parsing in block contents when not applicable
When parsing block contents, the CSS parser speculatively tries to parse
each item as a declaration first. If that fails, it restores the token
position and tries again as a qualified rule. This means every qualified
rule inside an at-rule block (e.g. @layer, @media) gets parsed twice:
once as a failed declaration (which consumes all tokens via
consume_the_remnants_of_a_bad_declaration), and then again successfully
as a rule.

Add a lookahead that checks for the `ident whitespace* ':'` pattern
before attempting declaration parsing. Since declarations must start
with this pattern per spec, we can skip the attempt entirely when it
doesn't match and go straight to qualified rule parsing.

This is a massive win on large Tailwind CSS stylesheets (like the one
used by chatgpt.com) where thousands of rules inside @layer blocks were
being double-parsed. On a 1.2MB Tailwind v4 stylesheet, parse time goes
from ~2000ms to ~95ms (21x speedup).
2026-03-23 09:28:23 +01:00
Andreas Kling
2e6e597c94 LibWeb: Skip font sources with unsupported formats early
Filter out unsupported @font-face sources (e.g. .eot files, or sources
with an explicit unsupported format() hint) when populating the
FontFace's URL list, rather than fetching and attempting to decode them.

This matches the behavior of Blink and WebKit, which both reject .eot
URLs by filename when no format() hint is present, to avoid conflicts
with old WinIE-style @font-face rules.

Previously, we would fetch .eot files over the network, fail to decode
them, and then reject the font load promise, leading to a flood of
"NetworkError: Failed to load font" messages in the console.
2026-03-23 09:03:57 +01:00
Andreas Kling
76298a7f0c LibWeb: Only switch FontFaceSet to loaded when a loading font is removed
FontFaceSet::delete_() was calling switch_to_loaded() whenever the
loading fonts list was empty after removing a font, even if the font
was never in the loading list to begin with. Per spec, we should only
switch to loaded if the font was the last item in the loading list.
2026-03-23 09:03:57 +01:00
Davi Gomes
6d77c9edd1 Libraries: Move #pragma once above include headers
The #pragma once was placed after the #include directives instead of
immediately after the copyright comment, inconsistent with every other
header file
2026-03-22 14:05:44 +01:00
Andreas Kling
3cda08b6bf LibWeb: Numerically interpolate rotation angles for same-axis rotations
Per the CSS Transforms spec, when interpolating rotate3d() functions
with equal normalized direction vectors (or when one angle is zero),
the rotation angle should be interpolated numerically rather than
using quaternion slerp.

Previously we always used quaternion slerp, which cannot represent
rotations beyond 360 degrees. This meant that animating from
rotateY(0deg) to rotateY(3600deg) produced no visual animation, since
both quaternions are identical (3600 mod 360 = 0).

Now we detect when axes match and interpolate the angle directly,
correctly preserving multi-turn rotations. This fixes 168 WPT tests.
2026-03-21 23:16:32 -05:00
Andreas Kling
42bf301acd LibWeb: Apply animation-timing-function per keyframe interval
Per the CSS Animations spec, the animation-timing-function property
describes how the animation progresses between each pair of keyframes,
not as an overall effect-level timing function.

Previously we set it as the effect-level timing function on the
AnimationEffect, which caused easing to be applied to the global
animation progress. This made animations with multiple keyframes
"pause" at the start and end of the full animation cycle instead of
easing smoothly between each pair of keyframes.

Now we:
- Store per-keyframe easing in ResolvedKeyFrame from @keyframes rules
- Store the default easing on CSSAnimation instead of on the effect
- Apply per-keyframe easing to the interval progress during
  interpolation, falling back to the CSS animation's default easing
- Also store per-keyframe easing from JS-created KeyframeEffects to
  avoid incorrectly applying CSS default easing to replaced effects
2026-03-21 23:16:32 -05:00
Andreas Kling
6d6e15f012 LibWeb: Look up @keyframes rules from shadow root stylesheets
When processing CSS animations, we were only looking up @keyframes
rules from the document-level rule cache, passing nullptr for the
shadow root parameter. This meant that @keyframes defined inside
shadow DOM <style> elements were never found, and animations
referencing them would silently have no keyframes.

Fix this by first checking the element's containing shadow root for
@keyframes rules, then falling back to the document-level rules.
2026-03-21 23:16:32 -05:00
Andreas Kling
36e05884f4 LibWeb: Fall back to discrete interpolation for non-invertible matrices
Per the CSS Transforms spec, when one of the matrices for interpolation
is non-invertible (i.e. cannot be decomposed), the animation must fall
back to discrete interpolation.

Previously we would silently drop the transform, producing "none". Now
we correctly snap between the from/to values at the 50% progress mark,
matching the behavior required by the spec and other browsers.

Also propagate to_matrix() errors instead of silently using a partial
result, and use the previously-unused AllowDiscrete parameter.
2026-03-21 23:16:32 -05:00
Andreas Kling
63748758ea LibWeb: Compute float to none for absolutely positioned elements
Per CSS2 section 9.7, if position has the value absolute or fixed,
the computed value of float is none. We were already blockifying the
display for such elements, but not resetting the computed value of
float. This made the wrong value observable via getComputedStyle().
2026-03-21 21:42:44 -05:00
Andreas Kling
223ca14abc LibWeb: Support :host::part() selectors within shadow DOM stylesheets
The :host::part() pattern allows a shadow DOM's own stylesheet to
style its internal elements that have been exposed via the part
attribute. Previously, ::part() rules were only collected from
ancestor shadow roots (for external part styling), but never from the
element's own containing shadow root.

Fix this by also collecting ::part() rules from the element's own
shadow root in collect_matching_rules(). Additionally, fix the
selector engine's ::part() compound matching to preserve the
shadow_host when the rule originates from the part element's own
shadow root, allowing :host to match correctly in the same compound
selector.

This fixes 2 previously failing WPT tests:
- css/css-shadow-parts/host-part-002.html
- css/css-shadow-parts/host-part-nesting.html
2026-03-21 21:42:44 -05:00
Andreas Kling
d1acb6e157 LibWeb: Fix :host() descendant matching for nested shadow hosts
When an element inside a shadow DOM is itself a shadow host, and a
rule from the parent shadow DOM uses a selector like
`:host([attr]) .child`, the combinator traversal needs to reach the
outer shadow host to match `:host([attr])`.

Previously, when the styled element was a shadow host and the rule
came from outside its own shadow root, we set shadow_host_to_use to
nullptr. This caused traverse_up() to use parent() which cannot
cross the shadow boundary, preventing the selector from reaching the
outer :host.

Fix this by using the rule's shadow root's host as the traversal
boundary instead of nullptr. This allows the descendant combinator
to traverse from the element up through the enclosing shadow DOM to
reach the outer shadow host for :host() matching.
2026-03-21 21:42:44 -05:00
Andreas Kling
6eeafd3d7a LibWeb: Support ::slotted() matching through nested slot chains
The CSS Scoping spec says ::slotted() represents elements assigned
"after flattening" to a slot. When a slot element is itself slotted
into another slot (nested slots), the flattened tree resolves the
chain so that the inner content appears in the outermost slot.

Previously, we only collected ::slotted() rules from the directly
assigned slot's shadow root. This meant that styles defined in an
outer shadow DOM's ::slotted() rules would not apply to elements
that were transitively slotted through intermediate slots.

Fix this by walking up the slot assignment chain when collecting
and matching ::slotted() rules, so that rules from every shadow
root in the chain are considered.
2026-03-21 21:42:44 -05:00
Andreas Kling
0c58a6f322 LibWeb: Use slot's shadow host when matching ::slotted() selectors
When matching selectors like `:host ::slotted(div)`, the selector
engine needs to traverse up from the slot element to reach the shadow
host for `:host` matching. Previously, we passed shadow_host_to_use
which was derived from the *slotted element's* DOM position. For
elements in the light DOM (not inside any shadow root), this was null,
causing traverse_up() to use parent() instead of
parent_or_shadow_host_element(). This meant the traversal could never
cross the shadow boundary from inside the shadow tree to reach the
host element.

Fix this by deriving the shadow host from the slot's containing shadow
root, which is the correct scope for combinator traversal within
::slotted() rule matching.
2026-03-21 21:42:44 -05:00
Andreas Kling
717f18909b LibWeb: Handle trailing whitespace in display value parsing
When a var() fallback value contained trailing whitespace (e.g.
`var(--foo, flex )`), parse_display_value() miscounted the tokens.
The remaining_token_count() check included whitespace tokens, routing
single-keyword values like "flex" to the multi-component path. The
multi-component parser then failed when encountering the trailing
whitespace token.

Fix this by counting only non-whitespace tokens for the single vs
multi-component routing decision, and by skipping whitespace in the
multi-component parsing loop.
2026-03-21 21:42:44 -05:00
Andreas Kling
bc3bd28378 LibWeb: Use Newton-Raphson for cubic-bezier easing evaluation
Replace the previous caching/binary-search approach with
Newton-Raphson iteration and bisection fallback. This is the
same algorithm used by WebKit, Chromium, and Firefox.

The old code had a broken binary search comparator that could never
return 0 (the second condition was always true when the first was
false), leading to out-of-bounds vector accesses and crashes.

Fixes #3628.
2026-03-21 18:21:31 -05:00
Andreas Kling
eb789e790e Everywhere: Use AK::SaturatingMath and remove Checked saturating APIs
Port all callers of Checked<T>::saturating_add/sub/mul to the new
standalone functions in AK/SaturatingMath.h, and remove the old
APIs from Checked.
2026-03-21 18:20:09 -05:00
Andreas Kling
be56df8d4f LibWeb: Handle guaranteed-invalid values in animation keyframes
When a CSS animation keyframe uses var() referencing a nonexistent or
invalid custom property, variable substitution produces a
guaranteed-invalid value. The animation keyframe processing code did not
handle this case, allowing the value to reach compute_opacity() (and
similar functions) which would hit VERIFY_NOT_REACHED().

Fix this by skipping guaranteed-invalid values in
compute_keyframe_values, matching how the regular cascading code treats
them.

This fixes a crash on chess.com.
2026-03-21 08:41:13 -05:00
Jelle Raaijmakers
7fed3f9801 LibWeb: Start fetching CSS image resources before document load event
Both Chromium and Gecko delay the document's load event for CSS image
resource requests (background-image, mask-image, etc). We now start
fetching CSS image resources as soon as their stylesheet is associated
with a document, rather than deferring until layout. This is done by
collecting ImageStyleValues during stylesheet parsing and initiating
their fetches when the stylesheet is added to the document.

Fixes #3448
2026-03-21 10:29:54 +01:00
Rob Ryan
d9f909c911 LibWeb: Add label cursor default CSS to match Chromium and Gecko 2026-03-21 07:40:25 +00:00