Commit Graph

711 Commits

Author SHA1 Message Date
Callum Law
4faab1aa57 LibWeb: Replace FitContentSV with KeywordSV and FunctionSV
The `fit-content` keyword is stored as a `KeywordStyleValue` and
`fit-content()` function is stored as `FunctionStyleValue`
2026-04-24 07:34:54 +01:00
Callum Law
3bfebf862b LibWeb: Replace AddFunctionStyleValue with FunctionStyleValue 2026-04-24 07:34:54 +01:00
Callum Law
c846b289a9 LibWeb: Replace FontVariantAlternatesFunctionSV with FunctionSV 2026-04-24 07:34:54 +01:00
Andreas Kling
e4800b2498 LibWeb: Parse @keyframes name as logical string, not token text
The @keyframes parser was storing the keyframes name via
Token::to_string(), which keeps a string token in its quoted,
serialized form. That meant @keyframes "foo" was stored as
"\"foo\"" while animation-name: "foo" resolved to "foo",
and the two never matched.

Store the unquoted string or identifier value so the @keyframes name
and the animation-name reference compare on the same string.
2026-04-22 20:59:00 +02:00
Callum Law
eab7935686 LibWeb: Explicitly pass calculation context when parsing
Previously we would generate the calculation context based on the
current value parsing context. The main problem with this was that
contexts were defined per property by default and had to be overriden
per component value using "special" contexts, which was easy to forget.

We now generate the calculation context per component value in the
relevant `parse_foo_value` methods.

The new failures in `typed_arithmetic.html` are because we no longer
treat percentages as resolving to their property-level type when
computing what the resolved type of a calculation is i.e. when we are
parsing the `<number>` portion of `line-height` we treat percentages as
raw percentages, not lengths. This brings us in line with WebKit but no
longer with Chrome and WPT, I am not sure what the correct behavior is.
2026-04-22 14:24:12 +01:00
Callum Law
76250ba142 LibWeb: Validate literal numeric values at parse time
This brings a couple of advantages:
 - Previously we relied on the caller validating the parsed value was in
   bounds after the fact - this was usually fine but there are a couple
   of places that it was forgotten (see the tests added in this commit),
   requiring the bounds to be passed as arguments makes us consider the
   desired range more explicitly.
 - In a future commit we will use the passed bounds as the clamping
   bounds for computed values, removing the need for the existing
   `ValueParsingContext` based method we have at the moment.
 - Generating code is easier with this approach
2026-04-22 14:24:12 +01:00
Callum Law
ca1b433d27 LibWeb: Restrict CSS dimension-percentage mix parsing
We should only try to parse a dimension-percentage mix in
`parse_css_value_for_properties` if percentages resolve relative to that
dimension, not simply because percentages are allowed in general.

This doesn't currently cause any issues since we check that percentages
are resolved relative to the relevant dimension within the
`parse_foo_percentage_value` functions
2026-04-22 14:24:12 +01:00
Callum Law
76caf4d041 LibWeb: Deduplicate dimension literal parsing
This extracts the shared logic between <foo> and <foo-percentage>
parsing into helper methods
2026-04-22 14:24:12 +01:00
Callum Law
aa9f2ed83f LibWeb: Rename AcceptedTypeRange to NumericRange
This name wasn't very clear, also updates associated type and variable
names.
2026-04-22 14:24:12 +01:00
Callum Law
fc5dae8615 LibWeb: Clamp calculated values in FiltervalueListSV ::absolutized()
In a future commit we will use the allowed literal values as the bounds
for calculated values rather than the existing `ValueParsingContext`
based system. Since the computed bounds are different from the allowed
literal values here we need to handle clamping them manually.
2026-04-22 14:24:12 +01:00
Callum Law
83ed7e7419 LibWeb: Add spec comments for <filter-value-list> parsing 2026-04-22 14:24:12 +01:00
Callum Law
8a7332b7b9 LibWeb: Add OpacityValueStyleValue
This allows us to avoid the ugly hack in
`property_accepted_type_ranges()`.

This also updates the `ValueType` to be `opacity-value` rather than
`opacity` to match the spec.
2026-04-22 14:24:12 +01:00
Callum Law
d6dbabdf0e LibWeb: Clamp CSS numeric token values to float range
This matches the behavior of other browsers. We did the equivalent
change for <integer> in b86377b

We continue to store these as doubles for the extra precision.
2026-04-22 14:24:12 +01:00
Callum Law
dbe0cb4b1d LibWeb: Use number_value() for Number token debug string
This brings us in line with the other numeric types (percentage and
dimension) and allows us to test the clamping behavior that will be
added in a future commit.
2026-04-22 14:24:12 +01:00
Callum Law
cbf6e4454d LibWeb: Remove Token::number()
In a future commit we will be applying clamping to numeric (i.e.
number, percentage, dimension) values which will be done in the
appropriate `Token` getters so accessing the underlying number value
would be a potential footgun
2026-04-22 14:24:12 +01:00
Tim Ledbetter
882bd57b83 LibWeb: Drop unnecessary ColorFunctionStyleValue constructor 2026-04-22 11:52:56 +01:00
Tim Ledbetter
8d4f8a2d7f LibWeb: Route lch()/oklch() through unified ColorFunctionStyleValue 2026-04-22 11:52:56 +01:00
Tim Ledbetter
43ba21a45b LibWeb: Route lab()/oklab() through unified ColorFunctionStyleValue 2026-04-22 11:52:56 +01:00
Tim Ledbetter
e358cf1f0d LibWeb: Route hwb() through unified ColorFunctionStyleValue 2026-04-22 11:52:56 +01:00
Tim Ledbetter
ebe12a8766 LibWeb: Route hsl()/hsla() through unified ColorFunctionStyleValue 2026-04-22 11:52:56 +01:00
Tim Ledbetter
9ea880dcf0 LibWeb: Route rgb()/rgba() through unified ColorFunctionStyleValue 2026-04-22 11:52:56 +01:00
Tim Ledbetter
e4c29811be LibWeb: Generalize ColorFunctionStyleValue to cover every color space
Introduce a descriptor table keyed by `ColorType` that encodes
per-space metadata: channel kind, percent reference value, clamp
bounds, function name and serialization behavior.

This descriptor table is then used so that a single class can back
every CSS color type. Resolution, serialization, and absolutization
are driven by the descriptor.
2026-04-22 11:52:56 +01:00
Sam Atkins
5d965d6d37 LibWeb/CSS: Simplify parse_as_pseudo_element_selector()
Now that we have a method that parses a single pseudo-element selector,
use that. This actually fixes a bug too.
2026-04-18 08:56:25 +02:00
Sam Atkins
2b47098301 LibWeb/CSS: Un-deprecate TokenStream::peek_token()
This is sometimes the best tool for the job. To try and make it less of
a footgun that peek_token() is identical to next_token(), make the
offset required, and note the shared behaviour. Also move them
together, and make the internal code match.
2026-04-16 14:52:22 +01:00
Sam Atkins
1f8a8870c3 LibWeb/CSS: Replace trivial peek_token() users with next_token()
`next_token()` does the same thing as `peek_token()` or `peek_token(0)`,
but is more idiomatic.
2026-04-16 14:52:22 +01:00
Sam Atkins
f938829619 LibWeb/CSS: Remove a couple of deprecated TokenStream methods 2026-04-16 14:52:22 +01:00
Sam Atkins
e7e0a48144 LibWeb/CSS: Stop reconsuming tokens in an+b parsing
The main change is that we now wait to consume the token until we know
we want it, instead of consuming it and then later putting it back.

While I was here, I spec-commented some parts and updated the URL.
2026-04-16 14:52:22 +01:00
Sam Atkins
d7c2524311 LibWeb: Stop reconsuming tokens to parse shorthands
Reconsuming the current token relies on parse_css_value_for_properties()
only consuming a single token, which may be true now but isn't safe to
assume. Wrapping that in a Transaction and reverting it if we need to
run manual parsing, is a much safer pattern.

Ideally we'll get parse_css_value_for_properties() to run the
per-property bespoke parsing code eventually.
2026-04-16 14:52:22 +01:00
Sam Atkins
b661c17117 LibWeb/CSS: Stop reconsuming tokens in parse_simple_selector()
No behaviour change, just a refactor to stop using a deprecated API.
2026-04-16 14:52:22 +01:00
Sam Atkins
7eabbfaa4b LibWeb/CSS: Use Transaction for checking tokens' substitution presence
Equivalent to using mark() and restore_a_mark() from before, except it
runs even if we return a SyntaxError.

Also removes the deprecated reconsume_current_input_token() call - this
never actually did anything useful anyway.
2026-04-16 14:52:22 +01:00
Sam Atkins
1239ebad29 LibWeb/CSS: Use transaction for attribute selector match-type parsing
No behaviour change, just a refactor to stop using a deprecated API -
reconsuming tokens.
2026-04-16 14:52:22 +01:00
Sam Atkins
3681452386 LibWeb/CSS: Stop reconsuming tokens in convert_to_keyframes_rule()
No behaviour change, just a refactor to stop using a deprecated API.
2026-04-16 14:52:22 +01:00
Sam Atkins
3342d880e8 LibWeb/CSS: Use a transaction for simple comma-separated list parsing
No behaviour change, just a refactor to stop using a deprecated API.
2026-04-16 14:52:22 +01:00
Sam Atkins
08dbbf24c2 LibWeb/CSS: Tokenize custom property values when substituting var()
Co-authored-by: Shannon Booth <shannon@serenityos.org>
2026-04-15 16:42:15 +02:00
Sam Atkins
5385d90e3c LibWeb/CSS: Output more details in Token::to_debug_string()
Include all the relevant fields for the type of Token, so that the
upcoming Tokenizer tests can verify that the produced Tokens are
correct.
2026-04-15 10:23:09 +01:00
Sam Atkins
d54305eb06 LibWeb/CSS: Ensure at-keyword name is consumed before input_since()
This relied on input_since() being called after
consume_an_ident_sequence(), which is not at all guaranteed. GCC
evidently evaluates the arguments in the opposite order.

This caused AtKeyword tokens to have their original source text be `@`
instead of the full `@whatever`, which apparently has no consequences
in our current code, but does mess up the debug output introduced in
the following commit.
2026-04-15 10:23:09 +01:00
Callum Law
76f0416b75 LibWeb: Resolve keywords in basic shape position at compute time
Previously we resolved these at parse time since that's what WPT
expected, but WPT was updated in web-platform-tests/wpt#59022 and
web-platform-tests/wpt#59176
2026-04-15 08:04:02 +02:00
Callum Law
1e36ccad0d LibWeb: Remove unused PercentageOr classes
These are only used at used value time (since we store as `StyleValue`s
before) by which time we resolve all percentages except for layout
relative length percentages.
2026-04-13 19:24:43 +02:00
Callum Law
d25f86da66 LibWeb: Remove unused CSS::Parser::Dimension 2026-04-13 19:24:43 +02:00
Sam Atkins
62527c1917 LibWeb/CSS: Separate pseudo-class and pseudo-element parsing code
Apart from making the code a bit easier to follow, we'll need to
distinguish between the two in order to insert a pseudo-element
combinator in front of pseudo-elements (including the single-colon
legacy ones).

next_is_pseudo_element() is its own function because it'll be needed in
a couple of other places, again to support pseudo-element combinators.
2026-04-10 15:00:58 +01:00
Sam Atkins
77308d8a5e LibWeb/CSS: VERIFY() that we don't call is_delim() incorrectly
This has bitten me too many times and it is non-intuitive to debug. So
let's make it very clear that something is wrong!
2026-04-10 15:00:58 +01:00
Sam Atkins
a997bb9546 LibWeb/CSS: Remove Optional from parse_compound_selector() return type
We always either return a CompoundSelector or an Error here. This
simplifies the user code.
2026-04-10 15:00:58 +01:00
Callum Law
919dba87b9 LibWeb: Support calculated <flex> values
This spec note reads to me as explicitly disallowing combining
`<length>` and `<flex>` (i.e. `calc(1px + 1fr)`). This behavior is
already implemented (as it is for all other combination of units i.e.
`<length>` and `<time>`).
2026-04-09 21:41:49 +01:00
Callum Law
281028944e LibWeb: Store GridTrackSize::m_value as a StyleValue
This simplifies handling (particularly around absolutization and
interpolation) and allows us to support calculated flex values (which
will come in a later commit).
2026-04-09 21:41:49 +01:00
Callum Law
aa7c30b7a9 LibWeb: Store fit-content() argument as StyleValue
This brings us in line with other `StyleValue` classes and will make it
easier to implement absolutization in a later commit
2026-04-09 21:41:49 +01:00
Sam Atkins
f993362425 LibWeb/CSS: Reject trailing tokens inside :host() pseudo-class 2026-04-08 15:53:02 +01:00
Sam Atkins
8cd4a19bb0 LibWeb/CSS: Parse combinators and compound-selectors separately
This allows places that want to parse a lone `<compound-selector>` to do
so without also implicitly parsing a combinator before it.

Also add error reporting in a bunch of places that previously failed
silently.
2026-04-08 15:53:02 +01:00
Callum Law
4fcb82143b LibWeb: Disallow tree counting functions in most canvas context setters
This matches the behavior of Chrome - tree counting functions are
allowed within on-screen (i.e. not OffscreenCanvas) font values, but
nowhere else.
2026-04-08 14:31:43 +01:00
Callum Law
1fdcea2b7b LibWeb: Disallow random() in canvas context value setters
This introduces two new top-level `ValueParsingContext`s,
`OnScreenCanvasContextFontValue` and `CanvasContextGenericValue`, while
these are handled the same for now, there is a distinction is whether or
not they allow tree counting functions (which will come in a later
commit)
2026-04-08 14:31:43 +01:00
Andreas Kling
e15b1a33cb LibWeb: Parse sizes media conditions per HTML
The HTML sizes algorithm does not use full media queries. It evaluates
a restricted media-condition grammar and then parses the selected
source size value as a length.

Teach the parser to follow that split more closely: treat sizes
conditions as two-valued booleans, validate MQ5 <general-enclosed>
contents more strictly, accept calc(0) for media feature values, and
reject only source-size math results that are negative or non-finite.

The imported sizes parsing tests then progress from 140/171 to
171/171 in all four cases.
2026-04-05 22:01:18 +02:00