Commit graph

205 commits

Author SHA1 Message Date
Sam Atkins
8e82544dce LibWeb: Stop treating undefined lengths as valid
When converting this code to use Optional, I accidentally left in the
initialization, so it *always* had a value, and always created a Length
from it. Oops.
2022-02-16 21:51:15 +01:00
Sam Atkins
a99d02e14d LibWeb: Add an enum for !important 2022-02-12 16:13:27 +00:00
Guilherme Gonçalves
a456759d7a LibWeb: Ignore malformed at-rules in CSS parser
Fixes #12405.
2022-02-12 11:24:17 +01:00
Sam Atkins
b51f428165 LibWeb: Parse multiple box-shadows :^)
Again, we don't yet render these (we render nothing) but this gets rid
of a decent amount of CSS spam on Discord.
2022-02-08 17:45:51 +01:00
Sam Atkins
e5b0369dfd LibWeb: Parse spread-distance and inset parts of box-shadow
We do not actually use these when rendering the shadow yet.
2022-02-08 17:45:51 +01:00
Sam Atkins
c547bed13b LibWeb: Reorganize box-shadow parsing code
The pattern we've adopted for other multi-value properties is to run in
a loop like this, since that makes it easier to cater for values
appearing in different orders.
2022-02-08 17:45:51 +01:00
Sam Atkins
8aaab19e6b LibWeb: Allow percentages for border-radius :^)
During the LengthPercentage split, I converted the individual-corner
`border-foo-bar-radius` properties to LengthPercentage but forgot
`border-radius` itself! Oops. Discord's CSS was doing `border-radius:
50%` a lot, so this cuts down on CSS parser spam.
2022-02-07 21:55:12 +01:00
Sam Atkins
dc7e73a0b7 LibWeb: Don't dump all stylesheets with CSS_PARSER_DEBUG enabled
Browser has a handy debug menu option to dump all stylesheets, so we
don't need to spam the console with this. (All the spam massively slows
down page loads.)
2022-02-06 22:13:07 +01:00
Andreas Kling
ad2180ba6c LibWeb: Put CSS parser debug spam behind CSS_PARSER_DEBUG 2022-02-05 22:50:39 +01:00
Sam Atkins
cbdbe0c5a2 LibWeb: Implement CalculatedStyleValue::to_string() 2022-02-04 13:52:02 +01:00
Sam Atkins
714832e705 LibWeb: Distinguish between Integer and Number calc() values 2022-02-04 13:52:02 +01:00
Sam Atkins
b54cd17c1e LibWeb: Allow percentage tokens again when parsing calc()
I unintentionally broke this in my LengthPercentage PR, but it was not
convenient to fix until now.
2022-02-04 13:52:02 +01:00
Sam Atkins
b69f6097de LibWeb: Resolve type of calc() expressions at parse-time
See https://www.w3.org/TR/css-values-3/#calc-type-checking

If the sub-expressions' types are incompatible, we discard the calc() as
invalid.

Had to do some minor rearranging/renaming of the Calc structs to make
the `resolve_foo_type()` templates work too.
2022-02-04 13:52:02 +01:00
Sam Atkins
b818d952d1 LibWeb: Combine the two sets of calc() operator enums 2022-02-04 13:52:02 +01:00
Sam Atkins
5826fe094f LibWeb: Allow comma- or space-separated StyleValueLists
This lets us produce valid CSS in its to_string() method, instead of
always adding commas as before. :^)

Also, finally added a Formatter for StyleValues.
2022-02-03 00:45:49 +01:00
Sam Atkins
cff44831a8 LibWeb: Remove Length::Type::Percentage :^)
All `<percentage>`s in the CSS grammar are now represented by the
`Percentage` class, and `<length-percentage>` by `LengthPercentage`.
2022-01-20 00:04:10 +01:00
Sam Atkins
784ba2ec42 LibWeb: Convert background-position to LengthPercentage
Not much needed changing this time, hurrah! :^)
2022-01-20 00:04:10 +01:00
Sam Atkins
f602249ae9 LibWeb: Convert background-size from Length to LengthPercentage
Checking these for `auto` is awkward, but separating that will come
later. :^)
2022-01-20 00:04:10 +01:00
Sam Atkins
f75e796909 LibWeb: Convert border-radii from Length to LengthPercentage :^)
The visit_lengths() code is a bit awkward but we'll clean that up later.
2022-01-20 00:04:10 +01:00
Sam Atkins
ea0f6b42f0 LibWeb: Add PercentageStyleValue, and parse it
This is in a slightly weird state, where Percentages are sometimes
Lengths and sometimes not, which I will be cleaning up in subsequent
commits, in an attempt not to change all of LibWeb in one go. :^)
2022-01-20 00:04:10 +01:00
Sam Atkins
416033a660 LibWeb: Support range syntax for media queries
This means you can now do queries like:

```css
@media (400px <= width < 800px) { }
```

Chromium and Firefox which I tested with both don't support this yet, so
that's cool. :^)
2022-01-02 15:43:51 +01:00
Sam Atkins
c3bf9e5b79 LibWeb: Rewrite media-query parsing to match spec grammar
Past me decided that the grammar was overly verbose and I could do it
better myself. Which seemed fine until the spec changed and I didn't
know how to integrate the changes. Lesson learned! :^)

Rather than have a function for every single part of the grammar, I have
written some as lambdas, and combned `<media-condition>` and
`<media-condition-without-or>` into one function. But otherwise it's
close to the spec, with comments listing the part of the grammar being
parsed, so hopefully it will be easier to make future adjustments!

This does not add any new functionality.
2022-01-02 15:43:51 +01:00
Sam Atkins
b6fe7cc324 LibWeb: Make MediaCondition a top-level type and add factory methods 2022-01-02 15:43:51 +01:00
Sam Atkins
ae4f0000c8 LibWeb: Make MediaFeature a top-level class and add factory methods
Web::CSS::MediaQuery::MediaFeature::Type was getting a bit ridiculous!
Also, this moves the detection of "min-" and "max-" media-features into
the MediaFeature itself, since this is an implementation detail, not
part of the spec.
2022-01-02 15:43:51 +01:00
Sam Atkins
6299d68e45 LibWeb: Introduce MediaFeatureValue type for use in media queries
Previously, we were using StyleValues for this, which was a bit of a
hack and was brittle, breaking when I modified how custom properties
were parsed. This is better and also lets us limit the kinds of value
that can be used here, to match the spec.
2022-01-02 15:43:51 +01:00
Sam Atkins
0a8e289f37 LibWeb: Update <general-enclosed> definition to match spec change
See https://github.com/w3c/csswg-drafts/pull/6799
2022-01-02 15:43:51 +01:00
Sam Atkins
c9062b4ed5 LibWeb: Remove now-unused CustomStyleValue 2021-12-09 21:30:31 +01:00
Sam Atkins
23dc0dac88 LibWeb: Parse and resolve UnresolvedStyleValues
If a property is custom or contains a `var()` reference, it cannot be
parsed into a proper StyleValue immediately, so we store it as an
UnresolvedStyleValue until the property is compute. Then, at compute
time, we resolve them by expanding out any `var()` references, and
parsing the result.

The implementation here is very naive, and involves copying the
UnresolvedStyleValue's tree of StyleComponentValueRules while copying
the contents of any `var()`s it finds along the way. This is quite an
expensive operation to do every time that the style is computed.
2021-12-09 21:30:31 +01:00
Sam Atkins
dea4f83037 LibWeb: Use a string instead of an internal Parser class in Supports
Now that we can serialize CSS tokens, we can just hold a string and then
re-parse it when the Supports is evaluated. This feels a little weird,
but it only happens once so it's not going to slow it down much, and it
keep the API cleaner.
2021-11-24 22:57:46 +01:00
Sam Atkins
7d5c626276 LibWeb: Add <general-enclosed> support to Media Queries 2021-11-24 22:57:46 +01:00
Sam Atkins
99e18f40fb LibWeb: Use new GeneralEnclosed class in Supports 2021-11-24 22:57:46 +01:00
Sam Atkins
e760263728 LibWeb: Parse CSS <general-enclosed> 2021-11-24 22:57:46 +01:00
Sam Atkins
9286aa77bc LibWeb: Break friendship between CSS Token and Parser :^(
The Parser no longer needs to mess with Token's internals, since we have
getter functions that are safer.
2021-11-19 22:35:05 +01:00
Sam Atkins
d37f62fd54 LibWeb: Verify that the Tokenizer doesn't produce Dimensions from %
If `12.34%` ever produces a Dimension token instead of a Percentage,
then something has gone wrong and we want to know about it!
2021-11-19 22:35:05 +01:00
Sam Atkins
522faa1554 LibWeb: Use number value from CSS Tokens
This lets us get rid of `try_parse_float()`, and simplifies the various
places we were calling it before.
2021-11-19 22:35:05 +01:00
Sam Atkins
738e682ae0 LibWeb: Make @import rules functional again :^)
The changes in commit d462a6720a meant
that `CSSLoader::load_next_import_if_needed()` was no longer being
called, so no `@import` rules were loading.

The spec does not seem to mention when that loading should take place,
but WebKit does this at parse time, as each rule is added to the style
sheet. If it works for them, it's probably not that bad. :^)

So, that's what we now do here. The `CSSImportRule` creates a fetch
request when it is constructed, so each one is responsible for its own
contents.
2021-11-18 21:11:19 +01:00
Sam Atkins
16d9ae0f88 LibWeb: Stop consuming too many tokens when parsing background-position
This was causing inputs like `0% 0%` to think only one value was
provided.
2021-11-17 22:20:01 +01:00
Sam Atkins
72d080b4ba LibWeb: Parse multiple backgrounds :^)
Like the `background-foo` longhand properties (except
`background-color`), `background` allows multiple layers to be defined,
separated by commas. A layer does not necessarily contain something to
actually draw!

This parses as a `BackgroundStyleValue`, holding a `StyleValueList` for
each property. This is mostly to make expansion into longhands simpler -
if we had a list of `BackgroundStyleValue`s instead, one per layer, then
we would have to break it up per-property anyway when computing styles.
2021-11-17 22:20:01 +01:00
Sam Atkins
8fd4678e79 LibWeb: Parse comma-separated lists for most background properties
We now can parse lists of values for these properties:
- `background-attachment`
- `background-clip`
- `background-image`
- `background-origin`
- `background-position`
- `background-repeat`
- `background-size`

This uses two new Parser methods:
`parse_simple_comma_separated_value_list()` for the simple case when
each value is parsed from a single token; and
`parse_comma_separated_value_list()` which takes a lambda for when
parsing each value is more involved.

This also means that any unconsumed tokens at the end will make the
parsing fail as it should, where previously we just ignored them.
2021-11-17 22:20:01 +01:00
Sam Atkins
0da014befd LibWeb: Remove CSS Parser method overloads with no TokenStream parameter
These are just clutter. Only one was ever used, in one place, and is
easily replaced by just passing `m_token_stream` to it.
2021-11-17 22:20:01 +01:00
Sam Atkins
7936f79825 LibWeb: Remove ParsingContext parameter from private CSS Parser methods
This was only needed when they were all static.
2021-11-17 22:20:01 +01:00
Andreas Kling
8b1108e485 Everywhere: Pass AK::StringView by value 2021-11-11 01:27:46 +01:00
Sam Atkins
63aa399873 LibWeb: Allow none value for transform property
This is the initial value for `transform`. We already handle the value
later, we just were not parsing it.
2021-11-10 21:58:14 +01:00
Sam Atkins
96936d04d6 LibWeb: Parse background-size as part of background shorthand 2021-11-10 14:38:49 +01:00
Sam Atkins
67214e0b61 LibWeb: Parse background-size property 2021-11-10 14:38:49 +01:00
Sam Atkins
891dd46a17 LibWeb: Store Repeat values directly in BackgroundRepeatStyleValue
...as opposed to storing StyleValues, which we have to later check are
IdentifierStyleValues, which store identifiers that we can convert to
Repeat values later. It's fewer allocations, and we can't end up with
invalid values by mistake. :^)
2021-11-10 14:38:49 +01:00
Sam Atkins
5d0acb63ae LibWeb: Use BackgroundRepeatStyleValue in background shorthand
This is step 1 in removing the two `background-repeat-x/y`
pseudo-properties. Since adding the concept of compound StyleValues, we
don't need `background-repeat` to be split in two any more.
2021-11-10 14:38:49 +01:00
Sam Atkins
532f1e859d LibWeb: Parse background-clip and background-origin
Including as part of the `background` shorthand. :^)
2021-11-10 14:38:49 +01:00
Sam Atkins
018a4aa85c LibWeb: Parse background-attachment as part of background property 2021-11-10 14:38:49 +01:00
Sam Atkins
116a5fe5d0 LibWeb: Add background-position to background property
This required modifying the background-parsing code to use a
TokenStream, but that turned out to be pretty simple.
2021-11-10 14:38:49 +01:00