Commit Graph

635 Commits

Author SHA1 Message Date
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
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
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
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
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
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
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
mikiubo
b5e90e0350 LibWeb: Remove outdated FIXME about path() in basic-shape parser
Remove a FIXME stating that path() was not implemented in
basic-shape parsing, as it is now supported.
2026-03-21 02:44:49 +00:00
Callum Law
6f226f3d2e LibWeb: Add missing AD-HOC comment to parse_family_name_value 2026-03-20 14:06:39 +00:00
Callum Law
915fc4602b LibWeb: Implement CSS inherit() function
The remaining failing imported tests are due to wider issues which are
covered by FIXMEs (both existing and added in this commit)
2026-03-19 10:25:37 +01:00
Callum Law
2300ba41fb LibWeb: Use generic <color-interpolation-method> parsing for color-mix
This also fixes an issue where we would allow arbitrary idents as color
spaces
2026-03-18 13:21:57 +00:00
Callum Law
9db607b1a7 LibWeb: Use generic <color-interpolation-method> parsing for gradients
See previous commit for details

We now support parsing of `display-p3-linear` (although it just falls
back to using Oklab since Skia doesn't support it)
2026-03-18 13:21:57 +00:00
Callum Law
d8c38a294c LibWeb: Add a generic <color-interpolation-method> parsing method
Previously we had two implementations for parsing
`<color-interpolation-method>`, one for gradients and one for
`color-mix()` - this commit adds another which will unify the existing
ones in following commits.

This implementation has a couple of advantages over the existing ones:
 - It is simpler in that it uses global CSS enums and their helper
   functions
 - It is spec compliant (unlike the `color-mix()` one which allows
   arbitrary idents)
 - It parses as a `StyleValue` which will be required once we support
   `<custom-color-space>` since that can be an `ident()` which isn't
   resolvable at parse time
2026-03-18 13:21:57 +00:00
Callum Law
c47f226225 LibWeb: Support CSS if() function
We don't yet support style queries
2026-03-09 14:36:18 +00:00
Callum Law
7f3b8bb587 LibWeb: Make parsing of <supports-condition> reusable
This will also be used for parsing `if()` (for which we want to return a
`BooleanExpression`) as well as `@supports`
2026-03-09 14:36:18 +00:00
Callum Law
ce43e81933 LibWeb: Lift parentheses parsing out of <media-feature>
Having this as part of `<media-feature>` seemed to be a spec bug, see
https://github.com/w3c/csswg-drafts/pull/13575
2026-03-09 14:36:18 +00:00
Callum Law
87b0b9ad31 LibWeb: Support other ASF argument types
While all parsed argument grammars can be represented as
`Vector<Vector<ComponentValue>>`, we can save some redundant work by
storing them in their argument-grammar-parsed format.

Note that for all currently implemented ASFs this is actually
`Vector<Vector<ComponentValue>>` and thus this change will only be
relevant for ASFs we haven't implemented yet
2026-03-09 14:36:18 +00:00
Callum Law
11d5898915 LibWeb: Make ASF grammar parsing method more reusable
These changes make it possible to use this function to parse
`<if-args-branch>` as part of `<if-args>`
2026-03-09 14:36:18 +00:00
Callum Law
81cb968beb LibWeb: Support symbols() function in <counter-style> 2026-02-27 16:25:53 +00:00
Tim Ledbetter
804287847a LibWeb: Add SVG paint fallback color support to CSS parsing 2026-02-27 17:14:50 +01:00
Callum Law
b683568556 LibWeb: Add missing predefined counter style keywords
This means that we properly lowercase these keywords when used as
counter style names
2026-02-27 12:10:44 +00:00
Sam Atkins
f657a4b71b LibWeb/CSS: Parse @font-face { font-weight } with two values
WPT has coverage for matching but not parsing, so the test is homemade.
2026-02-24 10:05:09 +00:00
Callum Law
2008c6be5a LibWeb: Add predefined @counter-styles to UA stylesheet
There are some predefined counter styles (such as the longhand east
asian ones) which are too complex to be defined here and will need to be
implemented ad-hoc, this remains as a FIXME for now.
2026-02-23 11:21:09 +00:00
Callum Law
3e9cdb2cf4 LibWeb: Store whether sheet being parsed is a UA stylesheet
UA stylesheets allow some things that regular stylesheets don't, for
instance allowing use of "non-overridable" `@counter-style` names.
2026-02-23 11:21:09 +00:00
Adam Colvin
d2f10c76fd LibWeb: Reject invalid alt text values in CSS content property parsing
The CSS content property's alt text (after `/`) was incorrectly
accepting any content value. Per the CSS Content Module Level 3 spec,
alt text only accepts <string>, <counter>, and <attr()> values. This
change adds type validation in the alt text parsing branch to reject
URLs, quote keywords, images, and other non-alt-text value types.

This fixes 64 subtests in the content-invalid WPT test.
2026-02-22 13:55:22 -05:00
Aliaksandr Kalenik
795222fab3 LibWeb: Validate grid-template-areas rectangles at parse time
Move grid area rectangle computation and validation from layout to the
CSS parser. Named grid areas that don't form filled-in rectangles now
correctly invalidate the declaration per spec.
2026-02-21 21:46:34 +01:00
Callum Law
e6669482e6 LibWeb: Parse font-variant-alternates functions 2026-02-20 22:01:44 +00:00
Callum Law
630117e111 LibWeb: Reuse logic for parsing font-variant-alternates
This means that we only need to add support for parsing of alternates
functions in one place
2026-02-20 22:01:44 +00:00
Callum Law
849e55b7ae LibWeb: Simplify parsing of font-variant-numeric 2026-02-20 22:01:44 +00:00
Callum Law
04fd7e00e9 LibWeb: Disallow disjointed numeric component of font-variant
The grammar groups this component together meaning that all
sub-components must occur together i.e.
`ordinal slashed-zero small-caps` is valid but
`ordinal small-caps slashed-zero` is not.

We also reuse the logic for parsing from the longhand
`font-variant-numeric` property for simplicity.
2026-02-20 22:01:44 +00:00
Callum Law
f511b95b81 LibWeb: Simplify parsing of font-variant-ligatures 2026-02-20 22:01:44 +00:00
Callum Law
d97098ec80 LibWeb: Disallow disjointed ligatures component of font-variant
The grammar groups this component together meaning that all
sub-components must occur together i.e.
`common-ligatures no-contextual small-caps` is valid but
`common-ligatures small-caps no-contextual` is not.

We also reuse the logic for parsing from the longhand
`font-variant-ligatures` property for simplicity.
2026-02-20 22:01:44 +00:00
Callum Law
d6b94951cf LibWeb: Simplify parsing of font-variant-east-asian 2026-02-20 22:01:44 +00:00
Callum Law
75dd7b767f LibWeb: Disallow disjointed east asian component of font-variant
The grammar groups this component together meaning that all
sub-components must occur together i.e. `jis78 full-width small-caps` is
valid but `jis78 small-caps full-width` is not.

We also reuse the logic for parsing from the longhand
`font-variant-east-asian` property for simplicity.
2026-02-20 22:01:44 +00:00
Callum Law
a287df4620 LibWeb: Improve formatting of font-variant grammar comment
Fix indentation and wrapping to clearly show grouped components
2026-02-20 22:01:44 +00:00
Callum Law
f0434655f9 LibWeb: Reduce recompilation from editing Enums.json
Reduces the recompilation caused by editing `Enums.json` from ~1528 to
~327
2026-02-19 11:27:06 +00:00
Callum Law
6d73a3e85f LibWeb: Parse @font-feature-values at-rule
We don't yet support the `font-display` descriptor as part of this
2026-02-17 12:25:27 +00:00
Callum Law
784911fb6d LibWeb: Implement CSSFontFeatureValuesRule 2026-02-17 12:25:27 +00:00
Callum Law
9f7f623455 LibWeb: Store FilterOperation::DropShadow sub-values as StyleValues
This simplifies handling and brings it into line with other `StyleValue`
types
2026-02-16 12:09:23 +00:00
Callum Law
98e62cf86a LibWeb: Store FilterOperation::Blur::radius as StyleValue
This simplifies handling and brings it into line with other `StyleValue`
types
2026-02-16 12:09:23 +00:00