Commit graph

943 commits

Author SHA1 Message Date
Callum Law
4ee8110449 LibWeb: Handle flood-opacity in line with other opacity properties
We now do the proper thing in terms of:
 - Allowing percentages
 - Returning the computed value in getComputedStyle
 - Handling values out of the [0,1] range

Gains us 13 WPT passes in the imported tests.
2025-07-18 11:04:55 -04:00
Jelle Raaijmakers
8dd259b8d8 LibWeb: Stub CSSCounterStyleRule
Just the interface. Adds 6 new WPT subtest passes.
2025-07-18 11:51:41 +01:00
norbiros
7ad01d28a8 LibWeb/CSS: Add basic registered properties with initial values
Add global registry for registered properties and partial support
for `@property` rule. Enables registering properties with initial
values. Also adds basic retrieval via `var()`.

Note: This is not a complete `@property` implementation.
2025-07-18 11:12:39 +01:00
Sam Atkins
0ff61e5e7b LibWeb/CSS: Implement path() basic shape 2025-07-17 13:59:23 -04:00
Callum Law
3d7c5115d8 LibWeb: Move Transformation::to_matrix to new CSV resolve methods
This gains us 2 WPT passes as we now correctly disallow relative lengths
in more places in the `DOMMatrix` constructor.
2025-07-17 08:31:52 +02:00
Sam Atkins
27a666f3b2 LibWeb/CSS: Implement type(<syntax>) in attr()
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Build Dev Container Image / build (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
This lets the `attr()` interpret the attribute's contents as an
arbitrary type instead of just as a string or number.
2025-07-16 14:47:45 +01:00
Sam Atkins
0a5e8c2865 LibWeb/CSS: Implement "parse with a <syntax>"
Uses the SyntaxNode tree to parse a list of ComponentValues into some
kind of StyleValue.
2025-07-16 14:47:45 +01:00
Sam Atkins
ded2207762 LibWeb/CSS: Parse the CSS <syntax> type into a tree
`<syntax>` is a limited subset of the "value definition syntax" used in
CSS specs. It's used for `@property`'s `syntax` descriptor, and for the
`type()` function in `attr()`.
2025-07-16 14:47:45 +01:00
Sam Atkins
72a7a18502 LibWeb/CSS: Use parse_value() to shrink parse_css_value_for_properties() 2025-07-16 14:47:45 +01:00
Sam Atkins
c7d4c4fbff LibWeb/CSS: Add a method to parse a value based on a ValueType 2025-07-16 14:47:45 +01:00
Sam Atkins
08bf9d39de LibWeb/CSS: Add String->ValueType conversion function
And also move this out of PropertyID.h because it's a separate thing.

I considered generating this but there's really not much to it.
2025-07-16 14:47:45 +01:00
Sam Atkins
7e2fa2650d LibWeb/CSS: Sort the entries in Enums.json
I semi-unintentionally add a checker for this in the next commit.
2025-07-16 14:47:45 +01:00
Sam Atkins
d18b0c07ca LibWeb/CSS: Make UnresolvedStyleValue figure out if it contains ASFs
UnresolvedStyleValue::create() has one user where we know if there are
any arbitrary substitution functions in the list of CVs, and two users
where we don't know and just hope there aren't any. I'm about to add
another user that also doesn't know, and so it seems worth just making
UnresolvedStyleValue::create() do that work instead.

We keep the parameter, now Optional<>, so that we save some redundant
work in that one place where we do already know.
2025-07-16 14:47:45 +01:00
Sam Atkins
5aba457009 LibWeb/CSS: Add tokenize() method to style values, to get a list of CVs
A couple of arbitrary substitution functions require us to get or
produce some style value, and then substitute its ComponentValues into
the original ComponentValue list. So this commit gives CSSStyleValue a
tokenize() method that does so.

Apart from a couple of unusual cases like the guaranteed-invalid value,
style values can all be converted into ComponentValues by serializing
them as a string, and then parsing that as a list of component values.
That feels unnecessarily inefficient in most cases though, so I've
implemented faster overrides for a lot of the basic style value
classes, but left that serialize-and-reparse method as the fallback.
2025-07-16 14:47:45 +01:00
Callum Law
33cf3d7782 LibWeb: Serialize RGB, HWB and HSL colors with unresolved components
Gains us 44 WPT passes.
2025-07-16 13:05:33 +01:00
Callum Law
6a9c8d7767 LibWeb: Don't resolve colors with unresolved components
`CSSColorValue`s which have unresolved `calc` components should be able
to be resolved. Previously we would always resolve them but with
incorrect values.

This is useful as we will now be able to now whether we should serialize
colors in their normalized form or not.

Slight regression in that we now serialize (RGB, HSL and HWB) colors
with components that rely on compute-time information as an empty
string, but that will be fixed in the next commit.
2025-07-16 13:05:33 +01:00
Callum Law
e66332c07a LibWeb: Add new CalculatedStyleValue::resolve_* methods
These new methods are built on top of the spec's
`simplify_a_calculation_tree` algorithm where the old methods were
ad-hoc.

These methods are not used anywhere yet as callers will need to be
migrated over from the deprecated methods one-by-one to account for
differences in behaviour.

No functionality changes.
2025-07-16 13:05:33 +01:00
Callum Law
afa95c2815 LibWeb: Mark CalculatedStyleValue::resolve_* methods as deprecated
The existing resolve methods are not to spec and we are working to
replace them with new ones based on the `simplify_a_calculation_tree`
method.

These are marked as deprecated rather than replaced outright as work
will need to be done on the caller side to be made compatible with the
new methods, for instance the new methods can fail to resolve (e.g.
if we are missing required context), where the existing methods will
always resolve (albeit sometimes with an incorrect value).

No functionality changes.
2025-07-16 13:05:33 +01:00
Callum Law
1d3e539c09 LibWeb: Account for logical property groups in set_a_declaration
When setting a declaration for a property in a logical property group,
it should appear after all other declarations which belong to the same
property group but have different mapping logic (are/aren't a logical
alias).

Gains us 1 WPT pass.
2025-07-16 11:16:49 +01:00
Callum Law
47ddc2ea87 LibWeb: Respect logical property groups when serializing shorthands
We should not serialize a group of properties `longhands` as a single
shorthand if there is any property declared between the first and
last property in `longhands` which is not part of `longhands` but
belongs to the same logical property group, and has different mapping
logic to any of property in `longhands`
2025-07-16 11:16:49 +01:00
Callum Law
a1c9b86ad3 LibWeb: Account for non-shorthand sub-properties when serializing border
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
When parsing values in `process_a_keyframes_argument` we don't expand
properties using `StyleComputer::for_each_property_expanding_shorthands`
unlike most other places - this means that if we parse a `border` we end
up with the `border`'s sub-properties (`border-width`, `border-style`,
`border-color`) still in their unexpanded forms (`CSSKeywordValue`,
`LengthStyleValue`, `StyleValueList`, etc) rather than
`ShorthandStyleValue`s which causes a crash when serializing the
`border` value in `KeyframeEffect::get_keyframes`.

The proper fix here is to parse `border`'s sub-properties directly to
`ShorthandStyleValue`s instead of relying on
`StyleComputer::for_each_property_expanding_shorthand` to do this
conversion for us but this may be a while off.

This commit also imports the previously crashing tests.
2025-07-16 06:49:38 +01:00
Sam Atkins
632ce9523b LibWeb/CSS: Add :unchecked pseudo-class
This just got added to the Selectors spec:

b78c97c19d

It's thus missing from the HTML spec and WPT, but I figured it was
simple enough to add.
2025-07-15 21:27:44 +02:00
Sam Atkins
b973c8d275 LibWeb/CSS: Remove the :target-within pseudo-class
This has been removed from the spec:

03b340c34f
2025-07-15 21:27:44 +02:00
Callum Law
93f957051a LibWeb: Handle serialization of invalid border in all contexts
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Previously as we handled this in `get_property_internal` there were some
contexts that we missed, for instance `CSSStyleProperties::serialized`.
2025-07-15 14:26:02 +01:00
Callum Law
927cd969b2 LibWeb: Generically serialize "positional-value-list-shorthand"s
We were previously handling this ad-hoc via logic in
`get_property_internal` but this didn't cover all contexts (for
instance `CSSStyleProperties::serialized`.

Gains us 9 more WPT tests as we now cover properties which weren't
included in the previous ad-hoc approach.
2025-07-15 14:26:02 +01:00
Callum Law
9ed85ddd63 LibWeb: Mark relevant properties as "positional-value-list-shorthands"
Some shorthand properties work differently to normal in that mapping of
provided values to longhands isn't necessarily 1-to-1 and depends on the
number of values provided, for example `margin`, `border-width`, `gap`,
etc.

These properties have distinct behaviors in how they are parsed and
serialized, having them marked allows us to implement theses behaviors
in a generic way.

No functionality changes.
2025-07-15 14:26:02 +01:00
Callum Law
48153ecf45 LibWeb: Remove duplicate logic for white-space special keywords
This is already handled in `ShorthandStyleValue::to_string`.

No functionality change.
2025-07-15 14:26:02 +01:00
Callum Law
09a5c04e5c LibWeb: Handle serialization of invalid font-variant in all contexts
Previously as we handled this in `get_property_internal` there were some
contexts that we missed, for instance `CSSStyleProperties::serialized`.
2025-07-15 14:26:02 +01:00
Shannon Booth
9054ff29f0 LibWeb/CSS: Parse the ::slotted pseudo-element 2025-07-15 13:54:17 +01:00
Shannon Booth
0151a088ad LibWeb/CSS: Fix missing ':' in debug logging for pt-name-selector 2025-07-15 13:54:17 +01:00
Sam Atkins
b3abbeab89 LibWeb/CSS: Support inherit custom properties on :root
Make sure we have a parent element before trying to look at it!

I've also pulled out a stub function for getting a custom property's
initial value, so that there's only one place to change once we support
`@property` more.
2025-07-15 10:36:08 +02:00
Jelle Raaijmakers
9f7447f546 LibWeb: Prioritize inheriting text-align for <th>
Because we defined `th { text-align: center }` in our UA stylesheet, it
received a higher precedence than inherited (inline) styles. Firefox
deals with this by defining a custom `text-align` value that prioritizes
any inherited value before defaulting to `text-align: center`.

We now do this as well :^)
2025-07-15 10:05:48 +02:00
Aliaksandr Kalenik
9bb5e70f5a LibWeb: Skip tree traversal if invalidation set doesn't have properties
This allows us to skip unnecessary tree traversal to mark style for
recalculation when invalidation set only has `InvalidateSelf`.
2025-07-14 09:32:05 +02:00
Callum Law
1a0d4487a8 LibWeb: Implement matches attribute on CSSSupportsRule and CSSMediaRule
Gains us 4 WPT tests.
2025-07-11 10:57:19 +02:00
Tim Ledbetter
88efb362c4 LibWeb: Remove UA styles for h1 in article, aside, nav and section 2025-07-11 08:24:32 +02:00
Tim Ledbetter
c4cf1ccede LibWeb: Don't crash when parsing ASF with non comma-separated arguments 2025-07-09 19:47:12 +01:00
Lucien Fiorini
635adc8aa7 LibWeb/SVG: Implement resolution for a subset of SVG filters 2025-07-09 18:07:12 +01:00
Lucien Fiorini
d3684a36b0 LibWeb/SVG: Add FEFloodElement 2025-07-09 18:07:12 +01:00
Callum Law
a8fc15c6b3 LibWeb: Don't fail on non-fill keyword parsing <border-image-slice>
Previously if we encountered a keyword other than `fill` when parsing
`<border-image-slice` we would return a nullptr.

This could cause issues when we parse `<border-image-slice>` as part of
parsing `border-image`, for example `border-image: 100% none` would fail
as we would try parse `none` as part of the `<border-image-slice>`
instead of `<border-image-source>`.

This change makes it so that we don't consume the token and leave it to
be parsed as part of the next section of the grammar.
2025-07-09 16:59:22 +01:00
Callum Law
cfafb3bf36 LibWeb: Remove unused code in for_each_property_expanding_shorthands
This case is already handled by this point as `value` is a shorthand.

No functionality changes.
2025-07-09 16:59:22 +01:00
Sam Atkins
ce79bc793c LibWeb/CSS: Protect against the billion-laughs attack
The attack unfortunately still slows us down, but this prevents us from
OOMing. Currently, we don't save the value of `var(--foo)` after
computing it once, so in this example, we end up computing `--prop1` 4
times to compute `--prop3`, but then we start again from scratch when
computing `--prop4`:

```css
  --prop1: lol;
  --prop2: var(--prop1) var(--prop1);
  --prop3: var(--prop2) var(--prop2);
  --prop4: var(--prop3) var(--prop3);
}
```

This should be solvable later if we update the computed values as we go.
2025-07-09 16:44:20 +01:00
Sam Atkins
b6032b0fcd LibWeb/CSS: Reimplement var()/attr() as arbitrary substitution functions
"Arbitrary substitution functions" are a family of functions that
includes var() and attr(). All of them resolve to an arbitrary set of
component values that are not known at parse-time, so they have to be
substituted at computed-value time.

Besides it being nice to follow the spec closely, this means we'll be
able to implement the others (such as `if()` and `inherit()`) more
easily.

The main omission here is the new "spread syntax", which can be
implemented in the future.
2025-07-09 16:44:20 +01:00
Sam Atkins
b417d13a7b LibWeb/CSS: Add method to parse <declaration-value>
This has an extra parameter to allow stopping at the first comma token,
which we need for var() and attr()'s "argument grammar".

Co-authored-by: Tim Ledbetter <tim.ledbetter@ladybird.org>
2025-07-09 16:44:20 +01:00
Sam Atkins
26acd897bf LibWeb: Produce computed values for custom properties
Custom properties are required to produce a computed value just like
regular properties. The computed value is defined in the spec as
"specified value with variables substituted, or the guaranteed-invalid
value", though in reality all arbitrary substitution functions should be
substituted, not just `var()`.

To support this, we parse the CSS-wide keywords normally in custom
properties, instead of ignoring them. We don't yet handle all of them
properly, and because that will require us to cascade them like regular
properties. This is just enough to prevent regressions when implementing
ASFs.

Our output in this new test is not quite correct, because of the awkward
way we handle whitespace in property values - so it has 3 spaces in the
middle instead of 1, until that's fixed.

It's possible this computed-value production should go in
cascade_custom_properties(), but I had issues with that. Hopefully once
we start cascading custom properties properly, it'll be clearer how
this should all work.
2025-07-09 16:44:20 +01:00
Sam Atkins
9079be850b LibWeb/CSS: Include guaranteed-invalid value in ComponentValue
Treating these like any other ComponentValue means not having to convert
between different types of Vector, and that we will be able to use
TokenStream to parse the "argument grammars" of arbitrary substitution
functions.
2025-07-09 16:44:20 +01:00
Sam Atkins
b5ed910f1f LibWeb: Rename "var_or_attr" to "arbitrary_substitution_function"
This is the spec term, and will apply to many more things than var() and
attr().
2025-07-09 16:44:20 +01:00
Sam Atkins
8f01297182 LibWeb: Remove now-invalid attr() type support
Previously the type argument in attr() could be the name of a CSS type
on its own. This has changed, and now only `raw-string`
(previously `string`) or the name of a dimension unit is allowed. Other
types and more complex grammar use the `type()` function, which we
don't yet support.

I've updated the syntax comment, but not the algorithm itself, which
will be reimplemented in a later commit.
2025-07-09 16:44:20 +01:00
Sam Atkins
a6ff088984 LibWeb/CSS: Stop converting at-rule names to lowercase
This basically reverts a6efdb1068.

The test added there still passes without the ad-hoc behaviour, so let's
remove it.
2025-07-09 15:04:57 +01:00
Sam Atkins
d5bee680b0 LibWeb/CSS: Construct all CSS Tokens in a consistent way
Add `create_foo()` static methods for the missing Token::Types, and use
them in the Tokenizer. This means we slightly deviate from the spec now:
it says "create foo token... set its bar to 32", but we now just wait
and construct the Token fully-formed. But those cases are short so it
should still be clear what we're doing.

This makes it possible to construct all kinds of Token elsewhere, such
as for testing purposes.
2025-07-09 15:04:57 +01:00
Andreas Kling
b3fd939628 LibWeb: Make sure we run selectors for mixed-case tag names
Before this change, we would never apply CSS rules where the selector
had a mixed-case tag name. This happened because our rule caches would
key them on the lowercased tag name, but we didn't lowercase the tag
name when fetching things from the cache.

This uncovered the fact that the SVG2 spec has a bunch of style applied
to non-rendered elements in a way that doesn't match other browsers.
Instead of blindly following the spec, we now match other browsers.
2025-07-09 14:36:08 +02:00