Commit graph

193 commits

Author SHA1 Message Date
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
Sam Atkins
988a8ed3d8 LibWeb: Parse CSS background-position property
This is done a bit differently from other properties: using a
TokenStream instead of just a Vector of ComponentValues. The reason for
this is, we can then use call the same function when parsing the
`background` shorthand. Otherwise, we would have to know in advance how
many values to pass down, which basically would involve duplicating the
`background-position` parsing code inside `background`.

The StyleValue is PositionStyleValue, since it represents a
`<position>`: https://www.w3.org/TR/css-values-4/#typedef-position
Unfortunately, background-position's parsing is a bit different from
`<position>`'s, (background-position allows 3-value syntax and
`<position>` doesn't) so we'll need to come back and write a different
parsing function for that later.
2021-11-10 14:38:49 +01:00
Timothy Flynn
eb0fb38cac LibWeb: Support parsing some data: URLs in CSS
For now, data URLs are explicitly limited to expected MIME types. For
example, image-related styles accept image MIME types.
2021-10-29 22:06:49 +02:00
Sam Atkins
52a1be5eae LibWeb: Allow whitespace when parsing "!important" in CSS
This accounts for cases like:
```css
.foo {
    color: blue ! important ;
}
```

That's rare now that minifying is so popular, but does appear on some
websites.

I've added spec comments to `consume_a_declaration()` while I was at it.
2021-10-28 16:05:48 +02:00
Sam Atkins
6f1debaab0 LibWeb: Allow whitespace inside CSS attribute selectors
Noticed this while checking some MediaWiki-based sites. It's not
obvious, but the spec does allow this, by not mentioning it in this list
of places whitespace is forbidden:
https://www.w3.org/TR/selectors-4/#white-space
2021-10-28 16:05:48 +02:00
Sam Atkins
3e9191936b LibWeb: Remove now-unnecessary String copy when parsing CSS colors
Color::from_string() now does a case-insensitive comparison of color
names, so we don't need this copy. :^)
2021-10-24 22:12:35 +02:00
Sam Atkins
6edd7a4d73 LibWeb: Implement hashless hex color quirk
https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk

The CSS parser should now be completely quirky! :^)

The code is a bit awkward, especially the prepending-0s step, but at
least it won't be running too often.
2021-10-23 18:53:17 +02:00
Sam Atkins
d1f489b847 LibWeb: Explicitly cast parameters to Color::from_hsl/hsla()
CLion was giving me the angry red underlines about this.
2021-10-23 18:53:17 +02:00
Sam Atkins
21b65de1ec LibGfx+LibWeb: Move "transparent" keyword into Color::from_string()
It seemed odd to have this one color handled separately, when
`Color::from_string()` implements all other CSS colors.
2021-10-23 18:53:17 +02:00
Sam Atkins
f645ed199e LibWeb: Make CSS ParsingContext's Document* be const
The only reason it wasn't const before (and why we had a const_cast
hack) was to support ImageStyleValue's constructor taking it, which no
longer applies. `hack_count--;` :^)
2021-10-23 11:42:24 +02:00
Sam Atkins
0f393771b7 LibWeb: Move image resource request out of ImageStyleValue constructor
This always felt awkward to me, and required a few other hacks to make
it work. Now, the request is only started when `load_bitmap()` is
called, which we do inside `NodeWithStyle::apply_style()`.
2021-10-23 11:42:24 +02:00
Sam Atkins
78e57096e2 LibWeb: Distinguish between integer and float in NumericStyleValue
We have this information when parsing, and some properties specifically
only allow integers, so it makes sense to keep that around.
2021-10-19 19:12:09 +02:00
Sam Atkins
8b57e56d66 LibWeb: Parse "none" value for box-shadow property
Previously, `box-shadow: none` would fail to parse, meaning that in this
example:

```css
p {
  box-shadow: 20px 10px 5px magenta;
}

p.foo {
  box-shadow: none;
}
```

... a `<p class="foo">` would still have a box-shadow, when it should
not have one. Now, we handle the `none` value. :^)
2021-10-15 21:18:50 +01:00