Commit graph

691 commits

Author SHA1 Message Date
Hendiadyoin1
fff12847d5 LibWeb: Pull out larger parsing parts from Parser::parse_simple_selector
This lowers its cognitive complexity from 271 to under 100.
The new `parse_pseudo_simple_selector` still has a complexity of 114.
2022-03-21 12:49:00 +01:00
Hendiadyoin1
397d8b4aca LibWeb: Use a switch-statement on the delimiter for MatchType selection
... in Parser::parse_simple_selector
2022-03-21 12:49:00 +01:00
Hendiadyoin1
19cca57f8a LibWeb: Condense Delim checks in Parser::parse_simple_selector
This also removes some else-after-returns and adds some const qualifiers
2022-03-21 12:49:00 +01:00
Hendiadyoin1
3f8347718c LibWeb: Use a u32 for a delim tokens value
The spec says:
> <delim-token> has a value composed of a single code point.

So using StringView is a bit overkill.
This also allows us to use switch statements in the future.
2022-03-21 12:49:00 +01:00
Sam Atkins
d60b3be29a LibWeb: Implement the :focus-within selector
This matches if it has focus, or any nodes inside it do.
2022-03-20 17:35:31 +00:00
Andreas Kling
bdd42c9b0e LibWeb: Add basic support for :lang() CSS selector
This doesn't have parsing support for multiple languages in the same
selector. Support for language subcodes is not great either. But it
does do the basics.
2022-03-20 13:36:45 +01:00
Simon Wanner
c4f46893f6 LibWeb: Add parsing support for the remaining transform functions 2022-03-18 18:51:42 +01:00
Sam Atkins
174a25db5b LibWeb: Combine identical relative/regular selector parsing functions 2022-03-18 11:34:02 +01:00
Sam Atkins
5b0187477b LibWeb: Implement :nth-[last-]child(n of foo) syntax
In Selectors level 4, `:nth-child()` and `:nth-last-child()` can both
optionally take a selector-list argument. This selector-list acts as a
filter, so that only elements matching the list are counted. For
example, this means that the following are equivalent:

```css
:nth-child(2n+1 of p) {}
p:nth-of-type(2n+1) {}
```
2022-03-18 11:34:02 +01:00
Sam Atkins
993653317c LibWeb: Implement the :where() selector
This is identical to :is() except for specificity, so we can use the
same code paths. :^)
2022-03-18 11:34:02 +01:00
Sam Atkins
c148ed50bb LibWeb: Implement the :is() selector
This lets us finally get rid of a FIXME in the default style sheet. :^)
2022-03-18 11:34:02 +01:00
Sam Atkins
5319e2ba8e LibWeb: Parse forgiving selector-lists
`<forgiving-selector-list>` and `<forgiving-relative-selector-list>` are
the same as regular selector-lists, except that an invalid selector
does not make the whole list invalid. The former is used by the `:is()`
pseudo-class.

For example:

```css
/* This entire selector-list is invalid */
.foo, .bar, !?invalid { }

/* This is valid, but the "!?invalid" selector is removed */
:is(.foo, .bar, !?invalid) { }
```

Also as part of this, I've removed the `parse_a_selector(TokenStream)`
and `parse_a_relative_selector(TokenStream)` methods as they don't add
anything useful.
2022-03-18 11:34:02 +01:00
Lenny Maiorani
c37820b898 Libraries: Use default constructors/destructors in LibWeb
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 17:23:49 +00:00
Simon Wanner
1ed5e79478 LibWeb: Fix resolving relative URLs in style sheets
Relative URLs in style sheets should be resolved relative to the
style sheet they're in instead of the document.
2022-03-14 22:22:53 +01:00
Sam Atkins
1c18bb13a2 LibWeb: Move pseudo-element-from-string code into Selector 2022-03-10 17:30:09 +01:00
Sam Atkins
56a36da44e LibWeb: Only try parsing valid types of media-feature values
This resolves the ambiguity between whether a single number is a number
or a ratio. :^)

Also removed the "no more tokens" checks from
deea129b8c - that logic was completely
wrong, since there are always tokens after a value in the `(123 < foo <
456)` syntax.
2022-03-09 23:06:30 +01:00
Sam Atkins
fd47460141 LibWeb: Use ValueID for media-query identifiers 2022-03-09 23:06:30 +01:00
Sam Atkins
0371d33132 LibWeb+Meta: Stop discrete media-features from parsing as ranges
Only "range" type media-features are allowed to appear in range syntax,
or have a `min-/max-` prefix.
2022-03-09 23:06:30 +01:00
Sam Atkins
12561327d6 LibWeb: Use MediaFeatureIDs instead of Strings :^) 2022-03-09 23:06:30 +01:00
Sam Atkins
deea129b8c LibWeb: Add Ratio type to MediaFeatureValue
As noted, the Parser can't handle the `<number>` syntax for this - it
gets parsed instead by the `<number>` branch. We can't actually resolve
the ambiguity without making the Parser aware of what type each
media-feature is, but I will get to that soon. :^)
2022-03-07 13:42:25 +01:00
Sam Atkins
5f93f1c161 LibWeb: Introduce and parse CSS Ratio type
This is only used by media-queries, so for now we can skip
adding/parsing a StyleValue for these.
2022-03-07 13:42:25 +01:00
Karol Kosek
0f7156ed81 LibWeb: Parse CSS text-decoration-thickness property 2022-03-06 22:04:41 +01:00
Sam Atkins
a57128467a LibWeb: Implement :nth-of-type and :nth-last-of-type selectors :^) 2022-02-26 15:30:24 +01:00
Sam Atkins
817cd13d59 LibWeb: Implement the ::marker pseudo-element
This matches the marker boxes of list-items.
2022-02-25 19:35:34 +01:00
Sam Atkins
caef4ec157 LibWeb: Move PseudoElement enum up a level
This in preparation for changing how we store these, plus it's unwieldy
having to type CSS::Selector::SimpleSelector::PseudoElement.
2022-02-25 19:35:34 +01:00
Sam Atkins
adaab23149 LibWeb: Parse the content property
For now, we only understand `none`, `normal`, `<image>` and `<string>`.
The various other functions and identifiers can be added later.

We can *almost* use a StyleValueList for this, except it's divided into
two parts - the content, and the optional "alt text". So, I've added a
new StyleValue for it.
2022-02-25 19:35:34 +01:00
Sam Atkins
fd2ef43cb4 LibWeb: Implement <resolution> as a media feature type
This is the only dimension type besides `<length>` that is used in any
media queries in levels 4 or 5 right now. Others can be included
if/when they're needed.
2022-02-24 08:04:25 +01:00
Sam Atkins
5c8ea81d21 LibWeb: Parse Angle/Frequency/Resolution/Time types 2022-02-24 08:04:25 +01:00
Sam Atkins
f76a541819 LibWeb: Move length-unit-from-string code into Length
This means the units are defined in a single place instead of two.

Also removed the verify that we didn't produce a bogus % dimension token
in the Tokenizer, since this has never happened and the parser is not a
tokenizer test suite. :^)
2022-02-24 08:04:25 +01:00
Sam Atkins
608bfac2a9 LibWeb: Implement CSS Time class
This corresponds to `<time>` in the grammar.
2022-02-24 08:04:25 +01:00
Sam Atkins
0465abcfec LibWeb: Implement CSS Resolution class
This corresponds to `<resolution>` in the grammar.
2022-02-24 08:04:25 +01:00
Sam Atkins
bd79c303f6 LibWeb: Implement CSS Frequency class
This corresponds to `<frequency>` in the grammar.
2022-02-24 08:04:25 +01:00
Sam Atkins
355d1936f2 LibWeb: Implement CSS Angle class
This corresponds to `<angle>` in the grammar.
2022-02-24 08:04:25 +01:00
Sam Atkins
7880718fa8 LibWeb: Implement @supports selector(.foo)
This is defined in
https://www.w3.org/TR/css-conditional-4/#at-supports-ext which just
became a CR. :^)
2022-02-19 19:33:54 +01:00
Andreas Kling
7c33a084fb LibWeb: Support CSS :only-of-type selector
This matches any element that doesn't have a sibling with the same tag
name as itself.
2022-02-18 01:49:32 +01:00
Sam Atkins
53ec2ace23 LibWeb: Explain discrepancy with media-query parsing
This had me confused for a while, but I am not smart enough today to
actually fix it properly. :^)
2022-02-16 21:51:15 +01:00
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