Commit graph

639 commits

Author SHA1 Message Date
Sam Atkins
531b92d467 LibWeb/CSS: Make font implicitly reset some properties
This is a weird behaviour specific to `font` - it can reset some
properties that it never actually sets. As such, it didn't seem worth
adding this concept to the code generator, but just manually stuffing
the ShorthandStyleValue with them during parsing.
2025-02-12 16:00:42 +00:00
Sam Atkins
0fd0596dbf LibWeb: Support strings as list-style-types
We've long claimed to support this, but then silently ignored string
values, until 4cb2063577 which would
not-so-silently crash instead. (Oops)

So, actually pass the string value along and use it in the list marker.

As part of this, rename our `list-style-type` enum to
`counter-style-name-keyword`. This is an awkward name, attempting to be
spec-based. (The spec says `<counter-style>`, which is either a
`<counter-style-name>` or a function, and the `<counter-style-name>` is
a `<custom-ident>` that also has a few predefined values. So this is the
best I could come up with.)

Unfortunately only one WPT test for this passes - the others fail
because we produce a different layout when text is in `::before` than
when it's in `::marker`, and similar issues.
2025-02-11 10:39:27 +01:00
Sam Atkins
b987d53926 LibWeb: Migrate ListItemMarkerBox's text from ByteString to String 2025-02-11 10:39:27 +01:00
Aliaksandr Kalenik
875a7141a3 LibWeb: Skip pending :has() invalidations if there are no :has()
`invalidate_style()` already tries to avoid scheduling invalidation for
`:has()` by checking result of `may_have_has_selectors()`, but it might
still result in unnecessary work because `may_have_has_selectors()`
does not force building of rules cache. This change adds
`have_has_selectors()` that forces building of rules cache and is
invoked in `update_style()` to double-check whether we actually need to
process scheduled `:has()` invalidations.

This allows to skip ~100000 ancestor traversals on this WPT test:
https://wpt.live/html/select/options-length-too-large.html
2025-02-11 10:22:23 +01:00
Sam Atkins
dc58f6567f LibWeb: Support :open for file and color <input> elements 2025-02-10 13:57:36 +00:00
Aliaksandr Kalenik
adc17c3576 LibWeb: Deduplicate code for pseudo class selector matching
Moves pseudo class matching helpers into Element methods, so they don't
have to be duplicated between SelectorEngine and function that checks if
element is included in invalidation set.
2025-02-10 01:26:47 +01:00
Aliaksandr Kalenik
e677ab1699 LibWeb: Narrow :has() style invalidation to ancestor nodes
The current implementation of `:has()` style invalidation is divided
into two cases:
- When used in subject position (e.g., `.a:has(.b)`).
- When in a non-subject position (e.g., `.a > .b:has(.c)`).

This change focuses on improving the first case. For non-subject usage,
we still perform a full tree traversal and invalidate all elements
affected by the `:has()` pseudo-class invalidation set.

We already optimize subject `:has()` invalidations by limiting
invalidated elements to ones that were tested against `has()` selectors
during selector matching. However, selectors like `div:has(.a)`
currently cause every div element in the document to be invalidated.
By modifying the invalidation traversal to consider only ancestor nodes
(and, optionally, their siblings), we can drastically reduce the number
of invalidated elements for broad selectors like the example above.

On Discord, when scrolling through message history, this change allows
to reduce number of invalidated elements from ~1k to ~5.
2025-02-10 01:13:53 +01:00
Andreas Kling
39abd75d59 LibWeb: Use invalidation sets for link-related pseudo classes
- :any-link
- :link
- :local-link
2025-02-08 18:11:24 +01:00
Aliaksandr Kalenik
a6bea99959 LibWeb: Use invalidation sets for :defined style invalidation 2025-02-06 20:07:11 +01:00
Aliaksandr Kalenik
61c952fb43 LibWeb: Optimize style invalidation caused by DOM structural changes
With this change, siblings of an inserted node are no longer invalidated
unless the insertion could potentially affect their style. By
"potentially affected," we mean elements that are evaluated against the
following selectors during matching:
- Sibling combinators (+ or ~)
- Pseudo-classes :first-child and :last-child
- Pseudo-classes :nth-child, :nth-last-child, :nth-of-type, and
  :nth-last-of-type
2025-02-06 20:07:11 +01:00
Sam Atkins
6a4d80b9b6 LibWeb/CSS: Integrate ParsingContext into the Parser
This is not really a context, but more of a set of parameters for
creating a Parser. So, treat it as such: Rename it to ParsingParams,
and store its values and methods directly in the Parser instead of
keeping the ParsingContext around.

This has a nice side-effect of not including DOM/Document.h everywhere
that needs a Parser.
2025-02-06 16:47:25 +00:00
Sam Atkins
30ba7e334e LibWeb/CSS: Avoid creating a new Parser unnecessarily
This attr() code can instead just tokenize the input manually, and then
re-use the existing Parser.
2025-02-06 16:47:25 +00:00
Sam Atkins
0cb7f2ab6d LibWeb: Add some missing includes
These were being transitively included before via ParsingContext.h, but
that will break in a subsequent commit.
2025-02-06 16:47:25 +00:00
Sam Atkins
4d0537ee80 LibWeb/CSS: Make CSS Parser non-copyable/movable
We never actually need to do this, and doing so was complicated because
of the token stream, so just disable it altogether.
2025-02-06 16:47:25 +00:00
Sam Atkins
ca5dee4c55 LibWeb/CSS: Remove an unnecessary ParsingContext constructor
Anywhere we have a Node, we have a Document. So just use that.
2025-02-06 16:47:25 +00:00
Sam Atkins
0ac133d73b LibWeb/CSS: Replace is_generic_font_family() with a CSS enum
Also add the missing "math" value to it.
2025-02-06 16:47:25 +00:00
Sam Atkins
6da7a6eab5 LibWeb/CSS: Un-template some CSS Parser methods
A few of these are only ever called with T=Token, so let's simplify them
a bit.

As a drive-by change: Also correct the "unnecessairy" typos and use
discard_a_token().
2025-02-06 16:47:25 +00:00
Sam Atkins
1413760047 LibWeb/CSS: Split up Parser.cpp
This file has been a pain to edit for a while, even with the previous
splits. So, I've divided it up into 3 parts:
- Parser.cpp has the "base" code. It's the algorithms and entry-points
  defined in the Syntax spec.
- ValueParsing.cpp contains code for parsing single values, such as a
  length, or a color, or a calculation.
- PropertyParsing.cpp contains code for parsing an entire property's
  value. A few of these sit in a grey area between being a property's
  value and a value in their own right, but the rule I've used is "is
  this useful outside of a single property and its shorthands?"

This only moves code, with as few modifications as possible to make that
work. I did add explicit instantiations for the template implementations
as part of this, which revealed a few that are actually only compatible
with a single type, so I'll clear those up in a subsequent commit.
2025-02-06 16:47:25 +00:00
Andreas Kling
8cf16da6c2 Fix build breakage caused by badly sequenced PR merges 2025-02-05 19:49:24 +01:00
Sam Atkins
070c4a2045 LibWeb: Implement text-align: match-parent
At computed-value time, this is converted to whatever the parent's
computed value is. So it behaves a little like `inherit`, except that
an inherited start/end value uses the parent's start/end, which might
be different from the child's.
2025-02-05 17:45:44 +00:00
Sam Atkins
75168fa4d6 LibWeb/CSS: Remove redundant CSS:: namespaces from ComputedProperties
We're already in the CSS namespace, so let's remove the clutter.
2025-02-05 18:12:36 +01:00
Sam Atkins
4cb2063577 LibWeb/CSS: Stop returning Optional for enum properties
While keyword_to_foo() does return Optional<Foo>, in practice the
invalid keywords get rejected at parse-time, so we don't have to worry
about them here. This simplifies the user code quite a bit.
2025-02-05 18:12:36 +01:00
Luke Wilde
d3057a9c79 LibWeb: Preserve opening quotation in string token original source text
Used by chess.com, where it stores URLs to assets in CSS URL variables.
It then receives the value of them with getComputedStyle() and then
getPropertyValue(). With this, it trims off the url('') wrapper with a
simple slice(5, -2). Since we didn't preserve the opening quotation, it
would slice off the `h` in `https` instead of the quotation.
2025-02-05 16:02:09 +00:00
Psychpsyo
bad7324307 LibWeb: Implement CSS validity pseudo-classes 2025-02-05 12:38:55 +00:00
Glenn Skrzypczak
0fe30886f5 LibWeb/CSS: Implement mix-blend-mode
This adds support for the `mix-blend-mode` CSS property.
2025-02-05 11:26:58 +00:00
Mehran Kamal
cfe6702767 LibWeb/CSS: Fix linear-gradient single color-stop usage
The Web::CSS::Parser's GradientParsing ignores color-stops if
it is only a single one. This change allows to have color-stops
with double positions against a single color.

Further, also allows for `linear-gradient(black)` and similar
other gradient functions
2025-02-03 17:24:10 +00:00
Aliaksandr Kalenik
0f17ad9ebc LibWeb: Use fast CSS selector matching in default matches() code path
Before this change, checking if fast selector matching could be used was
only enabled in style recalculation and hover invalidation. With this
change it's enabled for all callers of SelectorEngine::matches() by
default. This way APIs like `Element.matches()` and `querySelector()`
could take advantage of this optimization.
2025-02-03 10:28:08 +01:00
Aliaksandr Kalenik
0cfe90b59e LibWeb: Don't allow "display: none" start CSS animations
This is both a correctness fix and a performance optimization.
2025-02-01 13:42:00 +01:00
Sam Atkins
6ebe19d13b LibWeb/CSS: Correct "percentages-resolve-to" data for properties
These were missing for properties that take a `<position>`, and were
incorrectly present for opacity-related properties.
2025-01-31 14:24:39 +01:00
Sam Atkins
d15e1eb9f6 LibWeb/CSS: Don't multiply non-canonical dimensions in calc()
This fixes the layout of tweakers.net, which regressed when calc
simplification was added in ee712bd98f.
2025-01-31 14:24:39 +01:00
Luke Wilde
6701aaf1cd LibWeb/CSS: Make inline-size a logical alias for the height property 2025-01-31 14:18:21 +01:00
Luke Wilde
010cdd8f90 LibWeb/CSS: Implement the ({min,max}-)block-size properties
These are heavily used by morrisons.com, using them in place of the
usual properties these map to.
2025-01-31 14:18:21 +01:00
Sam Atkins
f8cc990bcd LibWeb/CSS: Remove ad-hoc calc simplification from var/attr() expansion
Now that we simplify calculations after parsing them, this is redundant.
2025-01-30 19:31:54 +01:00
Sam Atkins
ee712bd98f LibWeb/CSS: Simplify calculations after parsing them
If a calculation was simplified down to a single numeric node, then most
of the time we can instead return a regular StyleValue, for example
`calc(2px + 3px)` would be simplified down to a `5px` LengthStyleValue.
This means that parse_calculated_value() can't return a
CalculatedStyleValue directly, and its callers all have to handle
non-calculated values as well as calculated ones.

This simplification is reflected in the new test results. Serialization
is not yet correct in all cases but we're closer than we were. :^)
2025-01-30 19:31:54 +01:00
Sam Atkins
39cefd7abf LibWeb/CSS: Tell CalculationContext whether to treat numbers as integers
Calc simplification eventually produces a single style-value as its
output. This extra context data will let us know whether a calculated
number should be treated as a `<number>` or an `<integer>`, so that for
example, `z-index: 12` and `z-index: calc(12)` both produce an
`IntegerStyleValue` containing 12.
2025-01-30 19:31:54 +01:00
Sam Atkins
91831438e0 LibWeb/CSS: Implement "simplify a calculation" algorithm
This is not yet called anywhere.
2025-01-30 19:31:54 +01:00
Sam Atkins
581f5dfc86 LibWeb/CSS: Remove inaccurate VERIFY from CalculationResult::from_value
The goal of this VERIFY was to ensure that we didn't mess up the logic
for calculating the correct type. However, it's now unable to do so
because it doesn't have access to the CalculationContext, which
determines what type percentages are. This makes it crash when running
the simplification algorithm. The benefits of this check are small, and
it meant doing extra work, so let's just remove it.
2025-01-30 19:31:54 +01:00
Sam Atkins
b947ae60db LibWeb/CSS: Remove custom <integer> parsing from math-depth 2025-01-30 19:31:54 +01:00
Sam Atkins
37f6c09984 LibWeb/CSS: Add some FIXME comments for missing math-function features 2025-01-30 19:31:54 +01:00
Sam Atkins
46b9497a66 LibWeb/CSS: Make non-finite Numbers serialize as themselves
We're required to serialize NaN, infinity, and -infinity as their
keyword names, even after they've been converted to Numbers.
2025-01-30 19:31:54 +01:00
Sam Atkins
c3d61020e7 LibWeb/CSS: Make CalculationNodes ref-counted
Calc simplification (which I'm working towards) involves repeatedly
deriving a new calculation tree from an existing one, and in many
cases, either the whole result or a portion of it will be identical to
that of the original. Using RefPtr lets us avoid making unnecessary
copies. As a bonus it will also make it easier to return either `this`
or a new node.

In future we could also cache commonly-used nodes, similar to how we do
so for 1px and 0px LengthStyleValues and various keywords.
2025-01-30 19:31:54 +01:00
Sam Atkins
385c3d273a LibWeb/CSS: Update CalculatedOr API to use CalculationResolutionContext
To be properly compatible with calc(), the resolved() methods all need:
- A length resolution context
- To return an Optional, as the calculation might not be resolvable

A bonus of this is that we can get rid of the overloads of `resolved()`
as they now all behave the same way.

A downside is a scattering of `value_or()` wherever these are used. It
might be the case that all unresolvable calculations have been rejected
before this point, but I'm not confident, and so I'll leave it like
this for now.
2025-01-30 19:31:54 +01:00
Sam Atkins
1d71662f31 LibWeb/CSS: Wrap calc()-resolution data in a struct
Initially I added this to the existing CalculationContext, but in
reality, we have some data at parse-time and different data at
resolve-time, so it made more sense to keep those separate.

Instead of needing a variety of methods for resolving a Foo, depending
on whether we have a Layout::Node available, or a percentage basis, or
a length resolution context... put those in a
CalculationResolutionContext, and just pass that one thing to these
methods. This also removes the need for separate resolve_*_percentage()
methods, because we can just pass the percentage basis in to the regular
resolve_foo() method.

This also corrects the issue that *any* calculation may need to resolve
lengths, but we previously only passed a length resolution context to
specific types in some situations. Now, they can all have one available,
though it's up to the caller to provide it.
2025-01-30 19:31:54 +01:00
Sam Atkins
8877a4f691 LibWeb/CSS: Hide "No property (from N properties) matched foo" message
This greatly reduces the amount of log spam on certain websites.
2025-01-29 12:04:14 +00:00
Aliaksandr Kalenik
d762d16938 LibWeb: Use invalidation sets for :has() invalidation
Prior to this change, we invalidated all elements in the document if it
used any selectors with :has(). This change aims to improve that by
applying a combination of techniques:
- Collect metadata for each element if it was matched against a selector
  with :has() in the subject position. This is needed to invalidate all
  elements that could be affected by selectors like `div:has(.a:empty)`
  because they are not covered by the invalidation sets.
- Use invalidation sets to invalidate elements that are affected by
  selectors with :has() in a non-subject position.

Selectors like `.a:has(.b) + .c` still cause whole-document invalidation
because invalidation sets cover only descendants, not siblings. As a
result, there is no performance improvement on github.com due to this
limitation. However, youtube.com and discord.com benefit from this
change.
2025-01-29 09:30:18 +01:00
Aliaksandr Kalenik
74dc335b28 LibWeb: Allow to early break from InvalidationSet::for_each_property() 2025-01-29 09:30:18 +01:00
Aliaksandr Kalenik
db47fa3db1 LibWeb: Use ancestor filters for hover style invalidation
By using ancestor filters some selectors could be early rejected
skipping selector engine invocation. According to my measurements it's
30-80% hover selectors depending on the website.
2025-01-28 18:55:42 +01:00
Aliaksandr Kalenik
d79bb1aac2 LibWeb: Fix underinvalidation when inline style has custom properties
We have an optimization that allows us to invalidate only the style of
the element itself and mark descendants for inherited properties update
when the "style" attribute changes (unless there are any CSS rules that
use the "style" attribute, then we also invalidate all descendants that
might be affected by those rules). This optimization was not taking into
account that when the inline style has custom properties, we also need
to invalidate all descendants whose style might be affected by them.

This change fixes this bug by saving a flag in Element that indicates
whether its style depends on any custom properties and then invalidating
all descendants with this flag set when the "style" attribute changes.
Unlike font relative lengths invalidation, for elements that depend on
custom properties, we need to actually recompute the style, instead of
individual properties, because values without expanded custom properties
are gone after cascading, and it has to be done again.

The test added for this change is a version of an existing test we had
restructured such that it doesn't trigger aggressive style invalidation
caused by DOM structured changes until the last moment when test results
are printed.
2025-01-28 11:38:06 +00:00
Psychpsyo
67ed676831 LibWeb: Implement CSS 'contain' property 2025-01-28 11:24:40 +00:00
Andreas Kling
c53c781745 LibWeb: Inline CSSStyleValue::to_keyword()
Shaves 120 ms of loading time off of https://wpt.fyi/
2025-01-28 01:12:45 +01:00