Commit graph

185 commits

Author SHA1 Message Date
Sam Atkins
658569b533 LibWeb/CSS: Use CSSRule's context to parse new child rules
Keep track of which CSSRule owns a CSSRuleList, and then use that to
produce a stack of RuleContexts for the CSS Parser to use.

There are certainly other places we should do this!
2025-04-23 10:55:45 +01:00
Sam Atkins
763b1b0ee2 LibWeb/CSS: Move RuleContext type out of Parser.h
This was previously internal to the Parser, but we'd like to pass it in
from outside.
2025-04-23 10:55:45 +01:00
Sam Atkins
9465492edf LibWeb/CSS: Combine the CSSRuleList constructors 2025-04-23 10:55:45 +01:00
Sam Atkins
fa8bbfa6a5 LibWeb/CSS: Align declaration block parsing with the spec
We have two different code paths that implement the "parse a CSS
declaration block" algorithm, for properties and descriptors. COmbining
them isn't straightforward, and doesn't seem especially useful.
2025-04-23 10:55:45 +01:00
Tim Ledbetter
8a20899382 LibWeb: Canonicalize color space when parsing color-mix() function
This amounts to replacing the "xyz" color space with "xyz-d65".
2025-04-23 11:26:27 +02:00
Tim Ledbetter
9cf04f40f6 LibWeb: Implement the color-mix() function
This takes 2 color values and returns the result of mixing them by a
given amount.
2025-04-22 12:19:51 +02:00
Shannon Booth
f48b760011 LibWeb/CSS: Avoid URL validity check parsing CSS URLs 2025-04-19 07:18:43 -04:00
Tim Ledbetter
ca200142e9 LibWeb: Don't construct stylesheet when modifying CSSStyleSheet rules
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macos-15, macOS, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macos-15, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, ubuntu-24.04, Linux, 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, `CSSStyleSheet.replace()` and `CSSStyleSheet.replaceSync()`
parsed the given CSS text into a temporary stylesheet object, from
which a list of rules was extracted. Doing this had the unintended
side-effect that a fetch request would be started if the given CSS text
referenced any external resources. This fetch request would cause a
crash, since the temporary stylesheet object didn't set the constructed
flag, or constructor document. We now parse the given CSS text as a
list of rules without constructing a temporary stylesheet.
2025-04-16 22:03:12 +01:00
Andrew Kaster
d1f6f5649e LibWeb: Make storage of CSS::CalculationNodes const-correct
Now we consistently use `RefPtr<CalculationNode const>` for all
CalculationNodes.
2025-04-16 10:41:44 -06:00
Andrew Kaster
6d11414957 LibWeb: Make storage of CSS::StyleValues const-correct
Now we consistently use `RefPtr<StyleValue const>` for all StyleValues.
2025-04-16 10:41:44 -06:00
Sam Atkins
9f333c424c LibWeb/CSS: Ensure a font source format() only contains one string/ident 2025-04-15 21:40:41 +02:00
Sam Atkins
52a4259ef2 LibWeb/CSS: Make @font-face src parsing more forgiving
As noted in the spec, an invalid (or unsupported) font source should not
make the `src` declaration invalid.
2025-04-15 21:40:41 +02:00
Sam Atkins
c224644bed LibWeb/CSS: Fetch ImageStyleValue images closer to spec
We now don't absolutize the URL during parsing, keeping it as a CSS::URL
object which we then pass to the "fetch an external image for a
stylesheet" algorithm. Our version of this algorithm is a bit ad-hoc,
in order to make use of SharedResourceRequest. To try and reduce
duplication, I've pulled all of fetch_a_style_resource() into a static
function, apart from the "actually do the fetch" step.
2025-04-15 09:54:35 +01:00
Sam Atkins
9284530a9f LibWeb/CSS: Return GC::Ref from style sheet parsing methods
These actually always return a value, despite the `CSSStyleSheet*`
return type. So, make that clearer by returning `GC::Ref<CSSStyleSheet>`
instead. This also means we can remove some ad-hoc error-checking code.
2025-04-15 09:40:38 +01:00
Sam Atkins
0f42d5ec3e LibWeb/CSS: Don't resolve @import URLs until they are used
The regression in the "conditional-CSSGroupingRule" test is we now fail
the "inserting an `@import`" subtests differently and the subtests
aren't independent. Specifically, we don't yet implement the checks in
`CSSRuleList::insert_a_css_rule()` that reject certain rules from being
inserted. Previously we didn't insert the `@import` rule because we
failed to parse its relative URL. Now we parse it correctly, we end up
inserting it.
2025-04-09 18:45:57 +01:00
Sam Atkins
1abc628cd8 LibWeb/CSS: Allow CSSImportRule's document to be null on construction
It's possible to parse an `@import` rule that isn't attached to a
document. We only actually need it to have one when fetching the linked
style sheet, and that should only happen when the CSSImportRule is
attached to a document. So, we can just accept a null pointer when
constructing it.

We relied on that Document to get the Realm, so pass that in as a
separate parameter.
2025-04-09 18:45:57 +01:00
Sam Atkins
bc02e3e9a9 LibWeb/CSS: Pass location to parse_a_stylesheet() 2025-04-09 18:45:57 +01:00
Sam Atkins
7216c6b050 LibWeb/CSS: Parse <url> as a new CSS::URL type
Our previous approach to `<url>` had a couple of issues:
- We'd complete the URL during parsing, when we should actually keep it
  as the original string until it's used.
- There's nowhere for us to store `<url-modifier>`s on a `URL::URL`.

So, `CSS::URL` is a solution to this. It holds the original URL string,
and later will also hold any modifiers. This commit parses all `<url>`s
as `CSS::URL`, but then converts it into a `URL::URL`, so no user code
is changed. These will be modified in subsequent commits.

For `@namespace`, we were never supposed to complete the URL at all, so
this makes that more correct already. However, in practice all
`@namespace`s are absolute URLs already, so this should have no
observable effects.
2025-04-09 18:45:57 +01:00
Sam Atkins
c82f4b46a2 LibWeb/CSS: Qualify uses of LibURL
To prepare for introducing a CSS::URL type, we need to qualify any use
of LibURL as `::URL::foo` instead of `URL::foo` so the compiler doesn't
get confused.

Many of these uses will be replaced, but I don't want to mix this in
with what will likely already be a large change.
2025-04-09 18:45:57 +01:00
Tim Ledbetter
02d34dd021 LibWeb: Rename CSSColor to ColorFunctionStyleValue
This gives a better idea of what the class represents.
2025-04-09 12:11:33 +01:00
Sam Atkins
a80408fea6 LibWeb/CSS: Store @property initial value as a style value
For now, it's the "raw" UnresolvedStyleValue. Later, we'll need to try
to parse it into a "real" style value using the property's syntax.
2025-04-08 09:59:41 +01:00
Sam Atkins
9292b769ef LibWeb/CSS: Use descriptor parsing for @property 2025-04-08 09:59:41 +01:00
Tim Ledbetter
408f9f3dde LibWeb: Disallow "default" as a <family-name> identifier
This commit disallows "default" as a font-family name, when the name is
not quoted because unquoted names are treated as custom-idents, for
which the name "default" is not allowed.
2025-04-07 12:14:29 +01:00
Sam Atkins
5aeaeb3097 LibWeb/CSS: Remove redundant font-face src parsing code
We now always parse this as a descriptor.
2025-04-07 10:00:21 +01:00
Sam Atkins
a7f7c2a821 LibWeb/CSS: Teach OpenTypeTaggedStyleValue to serialize without "1" 2025-04-07 10:00:21 +01:00
Tim Ledbetter
e73438e82c LibWeb: Clamp grayscale, invert, filter and opacity filter values to 1 2025-04-04 17:12:47 +01:00
Tim Ledbetter
61f76c7ec5 LibWeb: Set color filter value to 1 if omitted 2025-04-04 17:12:47 +01:00
Sam Atkins
e43bb1410c LibWeb/CSS: Reject non-grouping-rules as descendants of style rules
For example, `@font-face` is not only invalid inside a style rule, it's
also invalid inside a child of a style rule. This fixes a test
regression that we previously passed by accident.
2025-04-04 10:40:32 +01:00
Sam Atkins
f87b454fa9 LibWeb/CSS: Parse @font-face descriptors as style values
CSSFontFaceRule now stores its values as a CSSFontFaceDescriptors, with
a ParsedFontFace produced on request. This is exposed via the `style`
attribute, so we pass a lot of tests that try to read values from
that.

We have one test regression, which we passed by mistake before: The test
wanted to ensure we don't allow `@font-face` nested inside other rules.
We passed it just because we discarded any `@font-face` without a
`font-family`. What we're supposed to do is 1) keep at-rules with
missing required descriptors and just not use them, and 2) reject
certain ones when nested.

We may want to cache the ParsedFontFace in the future, but I didn't here
because 1) it's called rarely, and 2) that would mean knowing to
invalidate it when the CSSFontFaceDescriptors changes, which isn't
obvious to me right now.
2025-04-04 10:40:32 +01:00
Sam Atkins
fd45c53c11 LibWeb: Parse descriptors as style values, using the JSON data
The goal here is to do something a bit smarter with the parsing here
than we do for properties. Instead of the JSON saying "here are the
values, and here are the keywords, and we can have up to 3", here we
place the syntax in the JSON directly (though currently broken up as
one string per option) and then we attempt to parse each one in
sequence. It's something we'll need eventually for `@property` among
other things.

...However, in this first pass, I've gone with the simplest option of
hard-coding the types instead of figuring them out properly. So there's
a PositivePercentage type and a UnicodeRangeTokens type, instead of
properly implementing the grammar for those in a generic way.
2025-04-04 10:40:32 +01:00
Sam Atkins
60c536bdd5 LibWeb/CSS: Add FontSourceStyleValue
This will be used by the `@font { src: ... }` descriptor once we parse
descriptors as style values.
2025-04-04 10:40:32 +01:00
Sam Atkins
79093291b5 LibWeb/CSS: Un-template parse_comma_separated_value_list()
This doesn't need to be a template. Changing it means we can use it from
any FooParsing.cpp file, and also move it ValueParsing.cpp where it
belongs.
2025-04-04 10:40:32 +01:00
Sam Atkins
86b57a5205 LibWeb/CSS: Use font_format_is_supported() when parsing font sources 2025-04-02 15:53:50 +01:00
Tim Ledbetter
ac19b0cda8 LibWeb/CSS: Support media queries in import at-rules 2025-04-02 13:53:03 +01:00
Glenn Skrzypczak
9973b01848 LibWeb/CSS: Improved implementation of background-blend-mode
This is a improved version of a73cd88f0c
The old commit was reverted in 552dd18696

The new version only paints an element into a new layer if background
blend modes other than normal are used. The rasterization performance
of most websites should therefore not suffer.

Co-Authored-By: Alexander Kalenik <kalenik.aliaksandr@gmail.com>
2025-04-01 13:38:00 +02:00
Aliaksandr Kalenik
552dd18696 Revert "LibWeb/CSS: Implement 'background-blend-mode'"
This reverts commit a73cd88f0c.

Emitting SaveLayer for each paintable made rasterization a lot slower
on every website because now Skia has to allocate enormous amounts of
temporary surfaces. Let's revert it for now and figure how to implement
it with less aggressive SaveLayer usage.
2025-03-28 16:48:03 +00:00
Glenn Skrzypczak
a73cd88f0c LibWeb/CSS: Implement 'background-blend-mode'
This implements the 'background-blend-mode' CSS property.
2025-03-28 09:41:06 +00:00
Sam Atkins
09b8f73e11 LibWeb/CSS: Implement UnicodeRangeStyleValue
This is preparation for storing at-rule descriptors as style values.
2025-03-28 09:15:02 +00:00
Sam Atkins
ef3a2bf907 LibWeb/CSS: Move functions for detecting var()/attr() into Token types
This makes them accessible outside of PropertyParsing.cpp (which will be
useful if/when descriptors can include them). I've also renamed them to
use the correct term: "arbitrary substitution function".
2025-03-28 09:15:02 +00:00
Andreas Kling
ba030f0363 LibWeb: Add an engine-internal CSS realm for internal style parsing
This is used for default UA style right now, and we'll expand its use in
the near future.

Note that this required teaching the CSS parser to handle url()
functions when there's no document URL to resolve them against. If we
don't handle that, namespace rules in UA style don't parse correctly.
2025-03-25 23:57:00 +00:00
Jelle Raaijmakers
545d151948 LibWeb: Make transform: scale(calc(..)) work
The `transform` property supports transform functions that sometimes
need their `calc(percentage)` values to be converted to a number instead
of a length. Currently this only applies to the `scale*` family of
functions, which are marked as such in `TransformFunctions.json`.

We were not consistently applying the `NumberPercentage` type to these
functions though, and in addition, any `NumberPercentage` value would
not consider calculated values.
2025-03-25 19:53:36 +00:00
Aliaksandr Kalenik
090ac66af1 LibWeb: Repeat grid track sizing with minmax where both are not definite
...should be treated as invalid value.
2025-03-25 09:53:04 +00:00
Sam Atkins
88e11eea2d LibWeb: Implement functional pseudo-element parsing
"Functional" as in "it's a function token" and not "it works", because
the behaviour for these is unimplemented. :^)

This is modeled after the pseudo-class parsing, but with some changes
based on things I don't like about that implementation. I've
implemented the `<pt-name-selector>` parameter used by view-transitions
for now, but nothing else.
2025-03-25 07:54:13 +00:00
Sam Atkins
5cf04a33ad LibWeb/CSS: Add method for parsing <custom-ident> directly
We specifically want to parse one inside a selector, where we only care
about the string itself and don't want a whole style value.
2025-03-25 07:54:13 +00:00
Sam Atkins
285fbc8f1c LibWeb/CSS: Parse local() font sources more correctly
There were several issues with the previous parsing code, like ignoring
trailing tokens, not handling whitespace, and not requiring the value
to be a `<family-name>`. So, fix all that.

Also correct the serialization code, which didn't call
`serialize_a_string()` previously.
2025-03-25 07:53:59 +00:00
Sam Atkins
93a2c9946f LibWeb: Use FlyString for local font sources
The next commit will make it so we always have a FlyString, so this lets
us keep using it instead of turning it into a String.
2025-03-25 07:53:59 +00:00
Sam Atkins
f8536fc48a LibWeb/CSS: Split out <family-name> parsing
This type is used individually elsewhere in the spec. This also lets us
separate out the `<generic-family>` type.
2025-03-25 07:53:59 +00:00
Sam Atkins
193adee164 LibWeb/CSS: Implement aliases for pseudo-elements
We previously supported a few -webkit vendor-prefixed pseudo-elements.
This patch adds those back, along with -moz equivalents, by aliasing
them to standard ones. They behave identically, except for serializing
with their original name, just like for unrecognized -webkit
pseudo-elements.

It's likely to be a while before the forms spec settles and authors
start using the new pseudo-elements, so until then, we can still make
use of styles they've written for the non-standard ones.
2025-03-24 09:49:50 +00:00
Sam Atkins
ffa1dba96a LibWeb: Generate pseudo-element code from JSON
Initially, this generates the enum and to/from-string functions. The
JSON itself contains more data than that, but it's unused for now.
2025-03-24 09:49:50 +00:00
Sam Atkins
0ed2e71801 LibWeb/CSS: Move and rename PseudoElement types to prep for code gen
The upcoming generated types will match those for pseudo-classes: A
PseudoElementSelector type, that then holds a PseudoElement enum
defining what it is. That enum will be at the top level in the Web::CSS
namespace.

In order to keep the diffs clearer, this commit renames and moves the
types, and then a following one will replace the handwritten enum with
a generated one.
2025-03-24 09:49:50 +00:00