Commit graph

148 commits

Author SHA1 Message Date
Sam Atkins
8dc4f3763d LibWeb: Only apply the unitless-length quirk to needed properties
Previously, we applied the unitless length quirk to all numbers in
quirks mode. Now, we correctly only do so for the set of properties
listed in the quirks-mode spec:
https://quirks.spec.whatwg.org/#quirky-length-value

However, we do not yet prevent this quirk inside CSS expressions (like
`calc()`) as the spec directs.
2021-09-12 16:30:38 +02:00
Sam Atkins
3fa4f55f86 LibWeb: Add current_property_id to CSS ParsingContext
After `parse_css_value(PropertyID, TokenStream)`, we only need to know
the current PropertyID when checking for property-specific quirks, which
will take place in only 2 places, which happen deep down. Making the
current PropertyID part of the context means that those places can check
it easily, without us having to pass it to every one of the parsing
functions, which otherwise do not care.
2021-09-12 16:30:38 +02:00
Sam Atkins
8ddce2faaf LibWeb: Don't assert if reconsuming on an empty TokenStream
This fixes #9978.

When a TokenStream is empty, reading its `current_token()` still returns
a token (for EOF) so it makes sense to allow users to
`reconsume_current_input_token()` that token, so they do not have to
handle that themselves. Instead of VERIFY()ing, we can just no-op when
reconsuming token 0.
2021-09-12 16:11:48 +02:00
Sam Atkins
e2c32a6c65 Everywhere: Use my shiny new serenityos.org email :^) 2021-09-03 12:22:36 +02:00
Andreas Kling
eaf88cc78a AK: Rename create<T> => make_ref_counted<T>
And also try_create<T> => try_make_ref_counted<T>.

A global "create" was a bit much. The new name matches make<T> better,
which we've used for making single-owner objects since forever.
2021-09-03 02:36:09 +02:00
Karol Kosek
ffa7da0ca5 LibWeb: Handle CSS "ch" length unit (mostly)
This isn't 100% spec complaint, as it should use glyph_height()
depending on what the value of the writing-mode is, but we haven't
implemented it yet, so I think it'll be good enough for now.

This can be tested in https://wpt.live/css/css-values/ch-unit-008.html
Other css-unit tests fail as:
- 001 shows an issue related to a renderer (looks to me like you can't
  pass a width and height property to a span -- adding `display: block`
  to it passes the test),
- 002-004 and 009-012 use mentioned writing-mode,
- 016-017 loads custom fonts, which we also don't support (yet).
2021-08-25 17:55:53 +02:00
Sam Atkins
b92a6d6542 LibWeb: Implement CSS unset builtin value
This is equivalent to `initial` or `inherit`, depending on if the
property is inherited by default.
2021-08-25 12:14:34 +02:00
Sam Atkins
6d39f4342d LibWeb: Use single shared instance of Inherit/InitialStyleValue
These are always the same, so we can avoid allocating them repeatedly
and just use a single instance of each. :^)
2021-08-25 12:14:34 +02:00
Sam Atkins
d2342caf42 LibWeb: Parse CSS builtins before other values
These are straightforward to parse, so doing them first saves
potentially expensive calculations inside parse_foo_value() functions.
2021-08-25 12:14:34 +02:00
Andreas Kling
f58e2350dc LibWeb: Parse the CSS opacity property with strtof() for now
With the new parser, we started interpreting the `opacity` property as a
string value, which made it turn into `auto` and so anything with
opacity ended up not visible (e.g the header on google.com)

This patch restores our old behavior for `opacity` by interpreting it
as a numeric value with optional decimals.
2021-08-20 15:43:01 +02:00
Sam Atkins
b7aba70a28 LibWeb: Fix check for too many 'normal' values in font declaration
I had the values backwards, oops!
2021-08-18 10:32:32 +02:00
Sam Atkins
46b93174fc LibWeb: Treat multi-value CSS properties as StyleValueList by default
Some properties, such as `margin`, take multiple values but are not
complicated enough to require special-case handling. These no longer
parsed after my previous StyleValue changes, so this fixes that.

In the future we may want to configure whether to allow this for each
property.
2021-08-14 15:09:19 +02:00
Sam Atkins
afc434c416 LibWeb: Remove ValueListStyleValue :^)
This was the whole point of this PR. No more dealing with
ComponentValues in StyleResolver!
2021-08-14 12:45:01 +02:00
Sam Atkins
678760bd87 LibWeb: Parse multiple font-family values
Apart from now gathering comma-separated font-family names into a
StyleValueList, this also corrects the logic for parsing a single
font-family. Previously, we did not handle unquoted font names at all,
and now they are handled including when they are several words separated
by whitespace.

Modified the logic for `font` to use `parse_font_family_value()`.
`FontStyleValue.font_families()` is now a StyleValueList instead of a
vector, so that it can be better handled in StyleResolver.

We also finally remove the CSS::ParsingContext in
set_property_expanding_shorthands(). :^)
2021-08-14 12:45:01 +02:00
Sam Atkins
75450d2250 LibWeb: Handle StyleValueList for background-image
Also removed the string-parsing code I'd accidentally missed removing
before. Oops!
2021-08-14 12:45:01 +02:00
Sam Atkins
6f9263de04 LibWeb: Implement and use BackgroundRepeatStyleValue
This wraps an x and y background-repeat value. Potentially, we could use
this in place of the background-repeat-x and background-repeat-y
pseudo-properties, but for now StyleResolver splits it into those
properties, like it did before.
2021-08-14 12:45:01 +02:00
Sam Atkins
e6c0cb5a7f LibWeb: Implement and use OverflowStyleValue
Also added a test page for the `overflow` properties. They apparently
don't work, but at least they do parse.
2021-08-14 12:45:01 +02:00
Sam Atkins
168865dbdc LibWeb: Implement and use BorderRadiusStyleValue
This parses the elliptical border-radius values, though currently we
only support circular ones. So, to_length() is overloaded to return the
horizontal-radius. This means the code in Layout/Node.cpp does not need
to change for now.
2021-08-14 12:45:01 +02:00
Sam Atkins
cb3e097663 LibWeb: Implement and use BorderStyleValue 2021-08-14 12:45:01 +02:00
Sam Atkins
c27f99fc1d LibWeb: Implement and use FlexFlowStyleValue 2021-08-14 12:45:01 +02:00
Sam Atkins
2644d2c221 LibWeb: Implement and use FlexStyleValue
This is not just moving the code from StyleResolver to Parser. The logic
has changed to allow for the `flex-basis` to come before or after the
`flex-grow/shrink` values, as well as handle the special one-value
cases.

Also added test cases to flex.html to check the parsing. It does parse
correctly, but elements with `flex-basis: auto` do not calculate their
width correctly.
2021-08-14 12:45:01 +02:00
Sam Atkins
44a082391b LibWeb: Implement and use TextDecorationStyleValue
Modified text-decoration.html to better test that the values can be in
any order, and that it adopts the color from the `color` property if no
decoration color is specified. Right now, it always does because we do
not support a different decoration color. Later, we need to support the
`currentcolor` special CSS value for this purpose.
2021-08-14 12:45:01 +02:00
Sam Atkins
0e15561df0 LibWeb: Implement and use ListStyleStyleValue
Yes, the name is silly, but it's a StyleValue for list-style, so...
yeah. :^)

Since `list-style-type` and `list-style-image` can both have `none` as a
value, and can appear in any order, we have to handle it separately, and
then assign either or both of those to `none` depending on how many
`none`s there are, and whether those sub-properties already have values.

Added some extra test cases to lists.html to cover list-style-image and
list-style-position parts of the list-style shorthand, and the `none`
values.
2021-08-14 12:45:01 +02:00
Sam Atkins
dcbfb61816 LibWeb: Implement and use BackgroundStyleValue
This one represents one secton of a `background` property, since it can
have multiple background values separated by commas. Eventually, we will
represent that as a List of BackgroundStyleValues.

Also modified some background-foo properties in StyleResolver so that
the is_background_x() functions could be removed.

I realized that our handling of var() in shorthand properties is wrong,
so have been removing the is_builtin_or_dynamic() calls from the parsing
code for shorthands. This broke our var() test page, so I have replaced
the use of 'background' with 'background-color' there.
2021-08-14 12:45:01 +02:00
Sam Atkins
59501f1940 LibWeb: Implement and use FontStyleValue
After working with the code for a while, it makes more sense to put all
the parsing in Parser, instead of some of it living in StyleResolver.
That means our current ValueListStyleValue needs to be replaced with
specific StyleValue types for the properties that are shorthands or
otherwise combine several values together.

Here we implement FontStyleProperty, which represents a `font` CSS
property.

Also adjusted the fonts.html test page so that font-weights are featured
in test cases without things we do not yet support.
2021-08-14 12:45:01 +02:00
Sam Atkins
ed962be0fa LibWeb: Make 'auto' be both a Length and Identifier in CSS
I had accidentally parsed it in `parse_builtin_or_dynamic_value()`
instead of `parse_length()` before, which was confusing, so now it's
parsed along with other Lengths.

Whether it should be a Length is up for debate, and has been tripping me
up a few times, but a lot of code expects it to be one. For now, an
'auto' Length value (or any other value which overloads `is_auto()`)
also claims to be a `ValueID::Auto` identifier.
2021-08-14 12:45:01 +02:00
Sam Atkins
c8c2a8df56 LibWeb: Make '0' always be both a number and a length in CSS
A '0' token can be interpreted both as a Number, and as a Length. This
is problematic as in our CSS parser, we often call parse_css_value()
first, to figure out what something is, and then assign it. So we do not
know in advance whether we want a Length or not. Previously, it always
got parsed as a Length, and then every place that expected a
NumericStyleValue had to also check for a Length(0), which is easy to
forget to do.

In particular, this was causing issues with the `flex` property parsing.

To solve this, we now always parse 0 as a NumericStyleValue, and NSVs of
0 pretend to be a Length(0px) when asked. In two places, we were casting
to a LengthStyleValue* based on is_length(), which no longer works, so
those have been adjusted to use `StyleValue::to_length()` instead. They
also now check for `is_numeric()` first, to avoid the extra conversion
to a Length and back.

Possibly this opens up new issues elsewhere. In my testing it seems
fine, but until we can get CSS test suites running, it's hard to know
for certain.
2021-08-14 12:45:01 +02:00
Ali Mohammad Pur
15f95220ae AK+Everywhere: Delete Variant's default constructor
This was exposed to the user by mistake, and even accumulated a bunch of
users that didn't blow up out of sheer luck.
2021-08-13 17:31:39 +04:30
Tobias Christiansen
2a77abf85e Everywhere: Use tobyase@serenityos.org for my copyright headers 2021-08-09 21:43:05 +02:00
Sam Atkins
4065eb169c LibWeb: Implement CSS parsing convenience functions
These mostly match the API in `DeprecatedCSSParser.h`. The exception is
that `parse_selector()` returns a `SelectorList` instead of just one
`Selector`. The only uses of that are in
`ParentNode::query_selector[_all]()` which should be matching against a
list, according to the spec.

`parse_html_length()` is an odd case. It's used for `width="200"` cases
in HTML, so is not really CSS related, but does produce a StyleValue.
The values allowed in `width/height` in HTML vary per element, but they
are a lot more restricted than in CSS, so it's slightly inappropriate to
use the CSS parser for them, even though it's convenient.

We also ignore a few functions:

- `parse_line_width()`
- `parse_line_style()`
- `parse_color()`

These are all only used in `StyleResolver`, when it is given a property
value as a String. That won't happen once the old parser is removed.
2021-08-02 19:01:25 +04:30
Sam Atkins
eadcdd21e3 LibWeb: Clarify naming and publicity of CSS Parser methods
`parse_as_foo()` implies that the Parser's internal data is used,
whereas `parse_a_foo()` implies that the passed-in data is used.

Also, made all the `parse_a_foo()` methods private, as they are only
required within the Parser, and this makes the API clearer to outsiders.

The `parse_a(s)_foo()` naming is a little awkward, but it comes from
section 5.3 of the spec, so seemed worth keeping:
https://www.w3.org/TR/css-syntax-3/#parser-entry-points
2021-08-02 19:01:25 +04:30
Sam Atkins
f4b3ab19c0 LibWeb: Only dump parsed CSS stylesheet if logging is enabled 2021-08-02 19:01:25 +04:30
Sam Atkins
4c4482c7f6 LibWeb: Ignore unquoted data urls in CSS
Previously we were only ignoring quoted ones (eg `url("data:...")`), so
now we ignore unquoted ones (eg `url(data:...)`) too.
2021-07-31 00:18:11 +02:00
Sam Atkins
23d869138f LibWeb: Parse calc() values in new CSS Parser
This is a port of the code from DeprecatedCSSParser, to work with CSS
Tokens and TokenStream. I've modified it as little as possible.
2021-07-31 00:18:11 +02:00
Sam Atkins
21c59efbb9 LibWeb: Treat CSS calc() values as "builtin_or_dynamic" 2021-07-31 00:18:11 +02:00
Sam Atkins
d6668dbc56 LibWeb: Allow peeking more than 1 token ahead in CSS Parser 2021-07-31 00:18:11 +02:00
Sam Atkins
919eb7c0aa LibWeb: Parse box-shadow property in new CSS Parser
Previous multi-value properties use a ValueListStyleValue, which then
gets parsed into its sub-properties in the StyleResolver. However, that
is not ideal, especially as it exposes StyleResolver to the inner
workings of the Parser and Tokenizer, which it should not need to know
about.

The way `box-shadow` was implemented as a StyleValue subclass means that
the parsing can happen inside the Parser instead, which seems like a
better solution. Converting the other complicated cases (background,
font, list-style) is on my todo list for later.
2021-07-31 00:18:11 +02:00
Sam Atkins
697bffa3bd LibWeb: Convert CSS parse_{color,length}() lambdas into methods
This lets us get the Length and Color values directly, without having to
create a StyleValue object and then throw it away again, when parsing
the box-shadow property in the next commit.
2021-07-31 00:18:11 +02:00
Sam Atkins
1b72766e4e LibWeb: Fix issues with CSS attribute selector handling
This is three small, related changes:

1. Element::has_attribute() now returns true if the attribute exists but
has no value. (eg, `<div foo />` -> `has_attribute("foo")`)

2. SelectorEngine::matches_attribute() now makes sure there is a first
segment before comparing it, fixing a crash.

3. CSS::Parser now converts attribute names in attribute selectors to
lowercase, to match the expectations of the rest of the system.
Converting to lowercase is not always correct, depending on language,
but since we only currently support HTML, and that expects them to be
case-insensitive, it is fine for now.
2021-07-31 00:18:11 +02:00
Sam Atkins
6034fc0ee6 LibWeb: Make CSS 'An+B' parsing spec-compliant
Parsing this pattern from CSS tokens turns out to be slightly crazy, but
thankfully well documented in the spec.

The spec lists the cases in order of simple -> complicated, but this
would cause problems in code, since `<n-dimension> <signed-.integer>`
would never by reached, as `<n-dimension>` comes before. Instead, I
have grouped them by their first token.

Also renamed the NthChildPattern class to ANPlusBPattern, to match spec
terminology.
2021-07-31 00:18:11 +02:00
Sam Atkins
8d0ff98eff LibWeb: Parse CSS selectors according to the spec
The spec does not directly tell us how to parse selectors, so there are
likely some bugs here, but I've used the spec language where possible.
This is very much based on the previous selector parsing code.

Any parse error inside a selector makes the entire SelectorList
invalid, so nothing is returned.
2021-07-31 00:18:11 +02:00
Sam Atkins
6ea5d03f43 LibWeb: Bring Selector terminology in line with the CSS spec
- CompoundSelector -> *deleted*
- ComplexSelector -> CompoundSelector
- Relation -> Combinator

Our Selector is really a ComplexSelector, but only the Parser and
SelectorEngine need to know that, so keeping it named Selector makes it
more understandable for users.

Our CompoundSelector is really a CompoundSelectorAndCombinator.
Combining the two makes sense in our codebase, but the accurate name is
so long that I think it makes the code less readable.

Renamed some Combinators to also match the spec terminology:

- AdjacentSibling -> NextSibling
- GeneralSibling -> SubsequentSibling

The previous names are somewhat ambiguous, so hopefully this is clearer.
2021-07-31 00:18:11 +02:00
Sam Atkins
ca436afeb5 LibWeb: Stop parsing integer CSS values as Lengths
This was a hack copied over from the old parser, but it was causing
problems with flex-grow, and probably other properties that accept
numbers. Removing it does not seem to break anything, so lets' remove
it! :^)
2021-07-31 00:18:11 +02:00
Sam Atkins
e54531244f LibWeb: Define proper debug symbols for CSS Parser and Tokenizer
You can now turn debug logging for them on using `CSS_PARSER_DEBUG` and
`CSS_TOKENIZER_DEBUG`.
2021-07-31 00:18:11 +02:00
Sam Atkins
af045cee22 LibWeb: Use references to CSS tokens instead of copying by value
A couple of places required a logic change to make this work, but mostly
it's a simple case of adding &.
2021-07-31 00:18:11 +02:00
Sam Atkins
7439fbd896 LibWeb: Get CSS @import rules working in new parser
Also added css-import.html, which tests the 3 syntax variations on
`@import` statements. Note that the optional media-query parameter to
`@import` is not handled yet.
2021-07-31 00:18:11 +02:00
stelar7
8264511d32 LibWeb: Dont try to parse "data" urls as links 2021-07-23 14:18:17 +02:00
Sam Atkins
6e08b200d4 LibWeb: Implement ImageStyleValue parsing
Later we will want to make a distinction between URL and Image values,
but this works for now.
2021-07-22 23:09:01 +02:00
Sam Atkins
a44d7670ab LibWeb: Resolve CSS list-style from value list
This resolves the three sub-properties, appearing in any order:

- list-style-image
- list-style-position
- list-style-type

Added `list-style-position` values to support this, though they are not
yet used in rendering.
2021-07-22 23:09:01 +02:00
Sam Atkins
7abfb18656 LibWeb: Implement CSS color parsing from Tokens
This was broken when we switched away from using StringStyleValues.
While I was at it, I have implemented hsl/a() and the percentage
syntax for rgb/a().

As a bonus, added `colors.html` as a test page for the various CSS
color syntaxes, since nothing was testing rgb() or rgba() before.

Much of the parsing code in LibGFX/Color.h seems to be centered
around CSS color values, but this is not used by the new Parser.
(And can't be used, because it requires a String value and we have
a list of Tokens of some kind instead.) Maybe that should be removed
from there when the new CSS parser is operational.
2021-07-22 23:09:01 +02:00