Commit graph

140 commits

Author SHA1 Message Date
Sam Atkins
57a25139a5 LibWeb: Implement @supports rule :^)
The main thing missing is that we don't serialize the supports clause,
but for actually using a `@supports (something: cool) {}` rule in CSS,
it works!
2021-10-08 23:02:57 +02:00
Sam Atkins
b1f8a73a05 LibWeb: Parse CSS Supports
... according to
https://www.w3.org/TR/css-conditional-3/#typedef-supports-condition

This works very similarly to `@media`, but is different enough to
require its own parsing. (Though, the draft of Conditional-4 currently
mentions combining the two into a `@when` rule.)

Made some small changes to parsing code to make this work. Notably,
making `consume_a_declaration()` fail gracefully instead of
`VERIFY()`ing.
2021-10-08 23:02:57 +02:00
Sam Atkins
87a30418bf LibWeb: Add CSS 'Supports' class
The name is a little awkward, but this corresponds to the condition of a
`@supports` rule or the `CSS.supports("")` function.

A supports query only gets evaluated once, since its condition cannot
change during runtime. (We either support something or we don't, and the
spec specifically mentions that user preferences that disable features
do not affect the result here.) We keep a representation of it around
though, so that it can be serialized if needed. This is a little awkward
since we hold onto a `StyleDeclarationRule` which should be an internal
Parser class. This means making some Parser functions more public.

Potentially we could evaluate the Supports inside the Parser, and have
it only store a String representation of itself. But this works for now.
:^)
2021-10-08 23:02:57 +02:00
Ben Wiederhake
0db6ca4065 LibWeb: Resolve cyclic dependency between StyleSheet and ImportRule
Previously: CSSImportRule::loaded_style_sheet() (and others) depend on
the definition of class CSSStyleSheet. Meanwhile,
CSSStyleSheet::template for_each_effective_style_rule (and others)
depend on the definition of class CSSImportRule.

This hasn't caused any problems so far because CSSStyleSheet.h happened
to be always included after CSSImportRule.h (in part due to alphabetical
ordering).

However, a compilation unit that (for example) only contains
    #include <Userland/Libraries/LibWeb/CSSImportRule.h>
would fail to compile.

This patch resolves this issue by pushing the inline definition of
Web::CSS::CSSStyleSheet::for_each_effective_style_rule and
for_first_not_loaded_import_rule into a different file, and adding the
missing headers.
2021-10-06 23:52:40 +01:00
Sam Atkins
bd648d082c LibWeb: Parse media queries! :^)
While not complete by any means, we are now compatible with the [level 3
spec](https://www.w3.org/TR/css3-mediaqueries/#syntax) and some parts of
[level 4.](https://www.w3.org/TR/mediaqueries-4#mq-syntax)

Compatibility with level 4+ requires:
- Implementing the range syntax: `(800px <= width <= 1200px)`
- Parsing `<general-enclosed>`, which represents syntax that is not yet
used but they may use in the future.
2021-10-01 20:03:03 +02:00
Sam Atkins
2ed0f880ee LibWeb: Add TokenStream::rewind_to_position()
Parsing media queries sometimes requires significant back-tracking, so
`reconsume_current_input_token()` was not good enough.
`rewind_to_position()` lets you reconsume an erbitrary number of tokens
to return to an earlier point in the stream, which you previously saved
from `TokenStream::position()`.
2021-10-01 20:03:03 +02:00
Sam Atkins
c7cd489689 LibWeb: Parse @media rules into CSSMediaRule objects
This is not yet actually useful, since we only have a stub for parsing
the query part, but now I have a nice way to test that things are
working. :^)
2021-10-01 20:03:03 +02:00
Sam Atkins
9c5430b9ee LibWeb: Make consume_a_qualified_rule() understand block tokens
This now matches the spec, and fixes the situation where if it was given
a TokenStream of StyleComponentValueRules, it would drop any Blocks on
the floor instead of adding them to the result StyleRule.
2021-10-01 20:03:03 +02:00
Sam Atkins
5bbbdb81dc LibWeb: Sketch out media-query parsing
This does everything except actually parse the individual media queries.
2021-10-01 20:03:03 +02:00
Nico Weber
6c9bc18a79 Userland: Fix typos 2021-10-01 01:18:52 +01:00
Andreas Kling
198bb322ef LibWeb: Fix null dereference when assigning an ImageStyleValue via JS
When parsing a CSS value in the context of a CSSStyleDeclaration
camelCase property setter, we don't necessarily have a Document to
provide the CSS parser for context.

So the parser can't go assuming that there's always a Document in the
ParsingContext. And ImageStyleValue can't go assuming that there's
always a Document either. This will require some more work to get things
right, I'm just patching up the null dereference for now.
2021-09-30 02:18:30 +02:00
Andreas Kling
994e33b0f7 LibWeb: Implement most of CSSStyleRule.insertRule() 2021-09-29 21:21:57 +02:00
Sam Atkins
058d44dcae LibWeb: Replace last couple of StyleValue casts with as_foo() 2021-09-24 15:01:43 +02:00
Sam Atkins
f574f538d2 LibWeb: Use property_accepts_value() for parsing text-decoration 2021-09-23 17:47:40 +02:00
Sam Atkins
4bc9b9eaaa LibWeb: Use property_accepts_value() for overflow parsing 2021-09-23 17:47:40 +02:00
Sam Atkins
b08094bccc LibWeb: Use property_accepts_value() for list style parsing 2021-09-23 17:47:40 +02:00
Sam Atkins
dcf70ab821 LibWeb: Use property_accepts_value() for font parsing 2021-09-23 17:47:40 +02:00
Sam Atkins
e262596ee1 LibWeb: Use property_accepts_value() for parsing flexbox properties 2021-09-23 17:47:40 +02:00
Sam Atkins
37e69fb286 LibWeb: Use property_accepts_value() when parsing borders 2021-09-23 17:47:40 +02:00
Sam Atkins
5213760e4b LibWeb: Use property_accepts_value() for background parsing
We also get rid of `is_background_{image,repeat}()` since they're no
longer needed. :^)
2021-09-23 17:47:40 +02:00
Sam Atkins
35eb8b0dc2 LibWeb: Add better debug logging for CSS parsing errors
Hidden behind `CSS_PARSER_DEBUG`, so I won't drive everyone else crazy.
:^)
2021-09-23 17:47:40 +02:00
Sam Atkins
5d6a4c5fc2 LibWeb: Check parsed CSS values with property_accepts_value()
This brings us a few nice benefits:

- We only generate a `StyleValueList` for properties that accept
  multiple values.
- We reject declarations that have too many values.
- We check the type of each value that is parsed, to make sure it's
  acceptable to the property.

Probably there are some regressions here, since this is

Later, we can also replace many of the `is_foo()` functions and lambas
inside the Parser with more calls to `property_accepts_value()`. Also we
can remove some checks when resolving styles, since only valid types of
values will get to that point. But one step at a time. :^)
2021-09-23 17:47:40 +02:00
Sam Atkins
4a1dbb4f36 LibWeb: Move color identifier checking to StyleValue::is_color()
This allows us to perform this check outside of the CSS Parser.
2021-09-23 17:47:40 +02:00
Sam Atkins
e40ea819d9 LibWeb: Prevent special-case CSS property parsing fallback
We don't want a property like `background` to fall back to parsing as a
single value or StyleValueList if `parse_background_style_value()`
fails. We just want it to fail.
2021-09-23 17:47:40 +02:00
Tobias Christiansen
9ebfafafbe LibWeb: Add transform property to the system
This patch adds parsing support as well as all the needed stuctures all
over LibWeb to pass Transformations around.
2021-09-18 21:53:37 +02:00
Sam Atkins
0053314dd1 LibWeb: Use initial values from Properties.json inside CSS Parser
This replaces several hard-coded initial values, with use of
`property_initial_value()`.
2021-09-17 23:06:45 +02:00
Sam Atkins
854d6e5822 LibWeb: Persuade CSS Parser that idents like currentcolor are colors
Shorthand properties were only checking for `ColorStyleValue`s, which
excludes identifier colors. Now they accept them too, including the
various `-libweb-foo` colors. :^)
2021-09-17 23:06:45 +02:00
Brian Gianforcaro
2b57018196 LibWeb: Use default instead of an empty constructor/destructor
Default implementations allow for more optimizations.
See: https://pvs-studio.com/en/docs/warnings/v832/
2021-09-16 17:17:13 +02:00
Idan Horowitz
4629f2e4ad LibWeb: Add the Web::URL namespace and move URLEncoder to it
This namespace will be used for all interfaces defined in the URL
specification, like URL and URLSearchParams.

This has the unfortunate side-effect of requiring us to use the fully
qualified AK::URL name whenever we want to refer to the AK class, so
this commit also fixes all such references.
2021-09-13 01:43:10 +02:00
Sam Atkins
5bb6936a2a LibWeb: Tidy up CSS parser logging 2021-09-12 21:34:57 +02:00
Sam Atkins
8f6017bc4e LibWeb: Ignore CSS properties with vendor-prefixed values
For example, this CSS previously produced a lot of log spam about the
`display` properties having invalid values:
```css
.foo {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
}
```

Now, it just ignores them, because we don't need to know about it. :^)
2021-09-12 21:34:57 +02:00
Sam Atkins
00b84249d6 LibWeb: Rename CSS::Parser::SelectorParsingResult => ParsingResult
I'm going to use it for non-selector purposes too, so the name needed to
change.
2021-09-12 21:34:57 +02:00
Sam Atkins
6c27e2938a LibWeb: Ignore vendor-prefixed at-rules
I don't know if I have ever seen one, but they are mentioned in the
spec, so we might as well do this.
https://wiki.csswg.org/spec/vendor-prefixes#css-vendor-prefixes
2021-09-12 21:34:57 +02:00
Sam Atkins
13c67f9920 LibWeb: Ignore vendor-prefixed pseudo-element/classes in selectors
Our debug logging when we fail to parse a legitimate selector, is less
useful when it's hidden among selectors that don't parse because they
contain vendor-prefixed pseudo-elements and classes. So, now we
specifically ignore these and don't produce a log message.
2021-09-12 21:34:57 +02:00
Sam Atkins
83cd2eef8f LibWeb: Return SelectorParsingResult from all selector parsing functions
This will allow us to know why a selector failed to parse, in situations
where that matters. (Like, when a selector includes vendor prefixes...)
2021-09-12 21:34:57 +02:00
Sam Atkins
e30b702c6c LibWeb: Reduce CSS_PARSER_DEBUG spam
Having every single function emit a debug message was useful at the
time, but now makes it impossible to spot important things.
2021-09-12 21:34:57 +02:00
Sam Atkins
7817c681d0 LibWeb: Ignore CSS properties with other people's vendor prefixes
This removes some `Property '-webkit-foo' has no value.` log spam. :^)
2021-09-12 21:34:57 +02:00
Andreas Kling
0bcab60463 LibWeb: Make CSSStyleDeclaration an abstract class
This patch moves the CSS property+value storage down to a new subclass
of CSSStyleDeclaration called PropertyOwningCSSStyleDeclaration.

The JavaScript wrapper for CSSStyleDeclaration now calls virtual
functions on the C++ object.

This is preparation for supporting computed style CSSStyleDeclaration
objects which won't have internal property storage, but rather an
internal element pointer. :^)
2021-09-12 20:44:50 +02:00
Sam Atkins
640a980080 LibWeb: Parse CSS selectors with no space before a combinator
Previously selectors like `.foo>.bar` did not parse, because there is no
whitespace between `.foo` and `>`. Now we correctly parse these. :^)
2021-09-12 17:27:34 +02:00
Sam Atkins
c66689ea76 LibWeb: Remove unused PropertyID parameter to StyleValue parsing methods
It's no longer used, and awkward to pass around.
2021-09-12 16:30:38 +02:00
Sam Atkins
8e4a4fd4db LibWeb: Remove "takes integer value" concept from parse_css_value()
This was only needed when numeric values had to be wrapped as Lengths,
and now serves no purpose.
2021-09-12 16:30:38 +02:00
Sam Atkins
b8c4320ffa LibWeb: Fix CSS opacity parsing
The StyleProperties code for opacity existed before NumericStyleValue
was a thing, and was affected by over-enthusiastic unitless-length
parsing, so it assumed everything was a length. Now it matches the
Color4 spec instead, accepting either a number, or a percentage.

We also get to remove the hack! :^)
2021-09-12 16:30:38 +02:00
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