Commit Graph

50 Commits

Author SHA1 Message Date
Andreas Kling
38b2cba6a2 LibWeb: Honor font-weight and font-style when using downloaded fonts
Instead of keying downloaded @font-face fonts on just the family name,
we now key them on a tuple of the family name, weight and slope.
2023-05-24 17:19:18 +02:00
Andreas Kling
fb722e69f3 LibWeb: Resolve CSS custom properties on pseudo elements
The resolved property sets are stored with the element in a
per-pseudo-element array (same as for pseudo element layout nodes).

Longer term, we should stop storing this with elements entirely and make
it temporary state in StyleComputer somehow, so we don't waste memory
keeping all the resolved properties around.

This makes various gradients show up on https://shopify.com/ :^)
2023-05-17 20:37:29 +02:00
Andreas Kling
3f4de06fc2 LibWeb: Cache the root element font metrics when calculating them
The root element font metrics were getting queried again and again
during style computation. Before this change we would do some work to
recalculate them each time.

This patch simply caches them in a StyleComputer member. Since style
updates always start with the root element, we know that it'll be
up-to-date by the time we look at any other element.

Before this change, we were spending ~5% of CPU time on Google Groups
in root_element_font_metrics().
2023-05-08 12:13:20 +02:00
Sam Atkins
d732a83de8 LibWeb: Propagate errors from absolutizing StyleValues 2023-05-06 08:07:28 +02:00
Sam Atkins
3c171593f8 LibWeb: Merge StyleComputer root-element font-metric calculation methods
This saves us from doing a lot of the same work multiple times, when we
want both the root font size and its line height.
2023-04-29 16:23:50 +02:00
Andreas Kling
620a34a463 LibWeb: Don't apply element inline style to associated pseudo elements
An element's inline style, if present, should not leak into any pseudo
elements generated by that element.
2023-04-02 15:00:06 +02:00
Simon Wanner
554c4af90f LibWeb: Add support for the lh and rlh length units
Resolving these units is somewhat tricky because of their interaction
with both font-size and line-height, but this implementation seems to
work as tested in http://wpt.live/css/css-values/lh-unit-001.html and
http://wpt.live/css/css-values/lh-unit-002.html
2023-03-18 20:14:52 +01:00
Matthew Olsson
7c0c1c8f49 LibJS+LibWeb: Wrap raw JS::Cell*/& fields in GCPtr/NonnullGCPtr 2023-03-15 08:48:49 +01:00
Andreas Kling
c598c092c2 LibWeb: Actually instantiate ::before/::after pseudo elements when found
Regressed in 3e970540b4.

Thanks to Luke for noticing the issue on YouTube! :^)
2023-03-14 18:47:20 +01:00
Andreas Kling
3e970540b4 LibWeb: Don't compute full style for ::before/::after unless matched
Before this patch, we would build full computed style for these pseudo
elements, for every DOM element, even if no ::before/::after selector
actually matched.

This was a colossal waste of time, and we can also just not do that.
Instead, just abort pseudo element style resolution early if no relevant
selectors matched. :^)
2023-03-14 16:53:03 +01:00
Andreas Kling
4bfdc4db17 LibWeb: Make CSS rule cache smarter about pseudo elements
Instead of putting every rule that matches a pseudo element in the
same bucket, let them go in the best ID/class/tag name bucket instead.
Then, add a flag to MatchingRule that says whether it contains a
pseudo element in the rightmost compound selector.

When deciding which selectors to run for an element, we can now simply
filter in/out pseudo element selectors as appropriate depending on what
we're trying to match.

This fixes an issue where pages using Font Awesome had 1700+ rules in the
pseudo-element rule cache. (This meant all those rules had to run
against every element twice or more while instantiating pseudo elements.)
2023-03-09 21:54:59 +01:00
Andreas Kling
72569bca1c LibWeb: Implement the CSS rule cache optimization for UA style as well
We were already sorting the author style selectors into buckets.
Now we do it for the built-in UA style as well.

This means less work for the selector engine everywhere :^)
2023-03-08 13:19:15 +01:00
Andreas Kling
689ca370d4 Everywhere: Remove NonnullRefPtr.h includes 2023-03-06 23:46:35 +01:00
Andreas Kling
8a48246ed1 Everywhere: Stop using NonnullRefPtrVector
This class had slightly confusing semantics and the added weirdness
doesn't seem worth it just so we can say "." instead of "->" when
iterating over a vector of NNRPs.

This patch replaces NonnullRefPtrVector<T> with Vector<NNRP<T>>.
2023-03-06 23:46:35 +01:00
Sam Atkins
6cc5e09c71 LibWeb: Port StyleComputer to new Strings 2023-02-19 00:51:16 +01:00
Sam Atkins
1c77867c78 LibWeb: Port FontCache to new Strings 2023-02-19 00:51:16 +01:00
Timothy Flynn
f3db548a3d AK+Everywhere: Rename FlyString to DeprecatedFlyString
DeprecatedFlyString relies heavily on DeprecatedString's StringImpl, so
let's rename it to A) match the name of DeprecatedString, B) write a new
FlyString class that is tied to String.
2023-01-09 23:00:24 +00:00
Sam Atkins
4084c66ad2 LibWeb: Use CSS Pixels for viewport rects 2023-01-05 17:42:31 +01:00
Sam Atkins
8cc0bdf777 LibWeb: Resolve Lengths to CSSPixels 2023-01-05 17:42:31 +01:00
Thomas Queiroz
eacfcac932 LibWeb: Use HashMap::try_ensure_capacity in StyleComputer 2022-12-10 14:29:46 +01:00
Linus Groh
6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00
Andreas Kling
ab9aa9da0d LibWeb: Do CSS var() expansion in a separate pass
By expanding all the var() values first, we allow var() to occur
anywhere, even as arguments to CSS functions.
2022-11-02 22:42:48 +01:00
Sam Atkins
54be317fa9 LibWeb: Use TokenStreams when expanding unresolved CSS values
This fixes a bug where any whitespace inside the `var()` or `attr()`
functions would make the StyleComputer fail to resolve them.
2022-09-27 19:36:31 +02:00
Andreas Kling
12042f0757 LibWeb: Make CSSRule and all its subclasses GC-allocated 2022-09-06 00:27:09 +02:00
Sam Atkins
fff2c35f51 LibWeb: Move ComponentValue to CSS::Parser namespace 2022-04-12 23:03:46 +02:00
Andreas Kling
6e70670e0b LibWeb: Load fonts from style sheet once when sheet is added
Previously, we were running the "load fonts if needed" machine at the
start of every style computation. That was a lot of unnecessary work,
especially on sites with lots of style rules, since we had to traverse
every style sheet to see if any @font-face rules needed loading.

With this patch, we now load fonts once per sheet, right after adding
it to a document's style sheet list.
2022-04-08 21:27:35 +02:00
Sam Atkins
8b538b1578 LibWeb: Rename StyleComponentValueRule -> ComponentValue
"Component value" is the term used in the spec, and it doesn't conflict
 with any other types, so let's use the shorter name. :^)

Also, this doesn't need to be friends with the Parser any more.
2022-04-07 21:20:14 +02:00
Andreas Kling
1f9aed2617 LibWeb: Load and use fonts described by @font-face rules :^)
When encountering a @font-face rule, StyleComputer will now fire off
a resource request and download the first source URL specified.

Once downloaded, we try to parse it as a TrueType font file, and if it
works, it's added to a cache in StyleComputer. This effectively makes
fonts per-document since every document has its own StyleComputer.

This is very unoptimized and could definitely use some caching, etc.
But it does work on Acid3. :^)
2022-03-29 02:14:20 +02:00
Andreas Kling
aa45cdf71d LibWeb: Give StyleComputer a helper for getting the current viewport 2022-03-19 22:04:43 +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
Andreas Kling
759bfbb572 LibWeb: Use StyleComputer::invalidate_rule_cache() directly everywhere
Get rid of the old, roundabout way of invalidating the rule cache by
incrementing the StyleSheetList "generation".

Instead, when something wants to invalidate the rule cache, just have it
directly invalidate the rule cache. This makes it much easier to see
what's happening anyway.
2022-03-15 19:48:19 +01:00
Andreas Kling
39389b5704 LibWeb: Don't make deep copy of custom properties for every element
Previously we were making a copy of the full set of custom properties
that applied to a DOM element. This was very costly and dominated the
profile when mousing around on GitHub.

Note that this may break custom properties on pseudo elements a little
bit, and that's something we'll have to look into.
2022-03-13 18:09:43 +01:00
Sam Atkins
332799fbcb LibWeb: Stub getting the initial font instead of the root one
Previously this queried the root layout-node's font, which was both
wrong and could crash if the layout wasn't ready yet. Now, it's just
wrong. But at least all the font-related wrongness is grouped together
in StyleComputer. :^)
2022-03-11 15:53:35 +01:00
Andreas Kling
205208db11 LibWeb: Reduce HashMap thrashing during custom property cascade
Build the final custom property map right away instead of first making
a temporary pointer-only map. We also precompute the final needed
capacity for the map to avoid incremental rehashing.
2022-03-03 13:56:37 +01:00
Andreas Kling
e4fdb40158 LibWeb: Use FlyString for CSS custom property names 2022-03-03 13:56:37 +01:00
Sam Atkins
f3cda3c830 LibWeb: Add a rule-cache for PseudoElements
This works a little differently from the other caches - ALL rules
containing a pseudo-element are in this bucket. This lets us only look
at this bucket when finding styles for a pseudo-element, and ignore it
if we're not.
2022-02-25 19:35:34 +01:00
Sam Atkins
7eb7396f8b LibWeb: Match styles for pseudo-elements
Since each selector can only have zero or one pseudo-element, we match
against it as a separate step, before matching the rest of the
selector. This should be faster, but mostly I did this because I could
not figure out how else to stop selectors without a pseudo-element from
matching the pseudo-element, eg so `.foo` styles don't affect
`.foo::before`.
2022-02-25 19:35:34 +01:00
Sam Atkins
a99d02e14d LibWeb: Add an enum for !important 2022-02-12 16:13:27 +00:00
Andreas Kling
1667a80ade LibWeb: Remove CascadeOrigin::Any enum value
Looks like I removed all uses of this value, but not the value itself!
Thanks to Idan for pointing that out. :^)
2022-02-11 15:21:56 +01:00
Andreas Kling
5ff816abbf LibWeb: Remove unused CascadeOrigin::Any
This was an ad-hoc concept from before we implemented the CSS cascade.
2022-02-11 12:45:38 +01:00
Andreas Kling
b68c51379e LibWeb: Add "tag name" buckets to StyleComputer::RuleCache
We can skip rules that require a specific tag name when matching against
any element with a different tag name. :^)
2022-02-10 20:52:11 +01:00
Andreas Kling
031296cf7b LibWeb: Add "ID" buckets to StyleComputer::RuleCache
We can skip rules that require a specific ID when matching against any
element that doesn't have that ID.
2022-02-10 20:52:11 +01:00
Andreas Kling
646b37d1a9 LibWeb: Cache CSS rules in buckets to reduce number of rules checked
This patch introduces the StyleComputer::RuleCache, which divides all of
our (author) CSS rules into buckets.

Currently, there are two buckets:
- Rules where a specific class must be present.
- All other rules.

This allows us to check a significantly smaller set of rules for each
element, since we can skip over any rule that requires a class attribute
not present on the element.

This takes the typical numer of rules tested per element on Discord from
~16000 to ~550. :^)

We can definitely improve the cache invalidation. It currently happens
too often due to media queries. And we also need to make sure we
invalidate when mutating style through CSSOM APIs.
2022-02-10 20:52:11 +01:00
Andreas Kling
8d104b7de2 LibWeb: Perform CSS custom property cascade once instead of per-property
Previously we would re-run the entire CSS selector machinery for each
property resolved. Instead of doing that, we now resolve a final set of
custom property key/value pairs at the start of the cascade.
2022-02-10 20:52:11 +01:00
Andreas Kling
b248661f11 LibWeb: Fix a bunch of trivial clang-tidy warnings in StyleComputer
- Replace "auto" with "auto const" where appropriate.
- Remove an unused struct.
- Make sort_matching_rules() a file-local static function.
- Remove some unnecessary includes.
2022-02-10 20:51:44 +01:00
Andreas Kling
12932d187e LibWeb: Teach StyleComputer about "Automatic Box Type Transformation"
CSS has rules about automatic blockification or inlinification of boxes
in certain circumstances.

This patch implements automatic blockification of absolutely positioned
and floating elements. This makes the smile appear on ACID2. :^)
2022-01-24 14:44:46 +01:00
Sam Atkins
c3437bccb3 LibWeb: Handle dependency cycles in CSS var()s :^)
We now detect situations like this, where variables infinitely recur,
without crashing:

```css
div {
  --a: var(--b);
  --b: var(--a);
  background: var(--a);
}

p {
  --foo: var(--foo);
  background: var(--foo);
}
```
2021-12-09 21:30:31 +01:00
Sam Atkins
67e1125b4c LibWeb: Handle fallback values for CSS variables :^) 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
Andreas Kling
f8dd3e14ba LibWeb: Rename CSS::StyleResolver => StyleComputer
Resolved style is a spec concept that refers to the weird mix of
computed style and used style reflected by getComputedStyle().

The purpose of this class is to produce the *computed* style for a given
element, so let's call it StyleComputer.
2021-09-24 15:12:15 +02:00