Commit graph

716 commits

Author SHA1 Message Date
Shannon Booth
c954d0be27 LibWeb/DOM: Add missing UTF-8 decode without BOM on fragment ID
We were previously crashing instead of using the replacement
character on invalid bytes.
2025-02-10 09:48:08 +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
5f10f8c54c LibWeb: Do includes_properties_from_invalidation_set() for :link & co
Fixes #3511.
2025-02-09 15:20:53 +01:00
Andreas Kling
187f8c5460 LibWeb: Run queued HTML tasks after associated document is destroyed
Before this change, tasks associated with a destroyed document would get
stuck in the task queue forever, since document-associated tasks are not
allowed to run when their document isn't fully active (and destroyed
documents never become fully active again). This caused everything
captured by task callbacks to leak.

We now treat tasks for destroyed documents as runnable immediately,
which gets them out of the queue.

This fixes another massive GC leak on Speedometer.
2025-02-07 16:53:11 +01:00
Andreas Kling
53c9c6f3ee LibWeb: Make Agent's MutationObserver list weak
Before this change, Agent held on to all of the live MutationObserver
objects via GC::Root. This prevented them from ever getting
garbage-collected.

Instead of roots, we now use a simple IntrusiveList and remove them
from it in the finalizer for MutationObserver.

This fixes a massive GC leak on Speedometer.
2025-02-07 16:53:11 +01:00
Aliaksandr Kalenik
ccb513abf7 LibWeb: Invalidate layout tree of parent of inserted node
f7a3f78 made the layout tree invalidate only the inserted nodes
themselves, but it turned out that CSS containment invalidation relies
on the parent being invalidated as well.
2025-02-07 01:23:10 +01:00
Aliaksandr Kalenik
a6bea99959 LibWeb: Use invalidation sets for :defined style invalidation 2025-02-06 20:07:11 +01:00
Aliaksandr Kalenik
51a5ebb91d LibWeb: Use bitfields for booleans in DOM::Element 2025-02-06 20:07:11 +01:00
Aliaksandr Kalenik
f7a3f785a8 LibWeb: Don't invalidate style of parent of inserted node
There is no need for this invalidation because taking care of siblings
is already done by invalidation with `NodeInsertBefore` reason. Parent
element itself (without subtree) is always invalidated by
`Node::children_changed()` hook, so `:empty` pseudo-class invalidation
is already covered.
2025-02-06 20:07:11 +01:00
Aliaksandr Kalenik
daf7c1ef60 LibWeb: Fix early return condition in Node::invalidate_style()
When checking whether an early return is possible because some ancestor
already has the whole subtree invalidation flag set, the check should
begin with the current node's parent rather than with the node itself.
Otherwise, if a node already has the whole subtree invalidation flag
set and is subsequently invalidated for the reason `NodeInsertBefore`
or `NodeRemove`, we will skip the sibling invalidation required for
these operations

This fix is required for optimizations in subsequent commits.
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
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
Psychpsyo
9b8120d8e8 Meta: Disallow links to single-page HTML spec 2025-02-05 16:04:50 -07:00
Aliaksandr Kalenik
da579e11b0 LibWeb: Start implementing render-blocking mechanism from HTML spec
This change implements enough spec steps to block rendering until
execution of sync scripts inserted from HTML parser is complete.
2025-02-05 18:28:46 +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
Aliaksandr Kalenik
a5e4a51b52 LibWeb: Change animation to schedule repaint only when necessary
Animation should trigger repaint only if it's required by animated style
update.
2025-02-05 14:34:41 +01:00
Andreas Kling
b8537f760d Revert "LibWeb: Make "assign slottables for a tree" fast when there are no slots"
This reverts commit adc25af8e2.

This caused images to stop loading on https://reddit.com/ and needs
more investigation.
2025-02-04 22:19:25 +01:00
Tim Ledbetter
67c950b002 LibWeb: Update create_element() to follow the latest specification 2025-02-04 20:07:32 +00:00
Tim Ledbetter
08c68cf699 LibWeb: Set is value when creating a customized built-in element 2025-02-04 20:07:32 +00:00
Tim Ledbetter
70097a7141 LibWeb: Add missing elements to valid local name map
Previously, attempting to create a custom element that extended any of
these elements would fail.
2025-02-04 20:07:32 +00:00
Felipe Muñoz Mazur
f6c4304e89 LibWeb: Check if event is not "beforeunload" before cancelling 2025-02-03 19:17:58 +00:00
Aliaksandr Kalenik
dfcee2bbdf LibWeb/DOM: Inherit Node from TreeNode
This allows to delete lots of tree helper functions duplicated between
Node and TreeNode.
2025-02-03 18:36:57 +01:00
Jelle Raaijmakers
314f4ff0da LibWeb: Prevent double page title update in DOM::Document
This ad-hoc code informs the client of a potentially changed page title.
But because we always update the title element (either the SVG or HTML
title) the client was already informed, causing the code to run twice.
2025-02-03 18:34:22 +01: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
Tim Ledbetter
6d7b7e7822 LibWeb: Use as to cast global object to WindowOrWorkerGlobalScopeMixin
No functional changes.
2025-02-02 17:18:56 +01:00
Sam Atkins
3794665b0b LibWeb/DOM: Add spec steps for WebDriver BiDi to document history algo
This is "update document for history step application" but that's too
long for the commit title. :^)

No code changes, just adding more FIXME comments for the new steps.
(And indented step 7's substeps for clarity.)

Corresponds to https://github.com/whatwg/html/pull/10910
2025-02-01 23:32:39 +01:00
Aliaksandr Kalenik
0c5b61b7e1 LibWeb: Fix infinite repaint loop when cached display list is used
Before this change, `m_needs_repaint` was reset in
`Document::record_display_list()` only when the cached display list was
absent. This meant that if the last triggered repaint used the cached
display list, we would keep repainting indefinitely until the display
list was invalidated (We schedule a task that checks if repainting is
required 60/s).

This change also moves `m_needs_repaint` from Document to
TraversableNavigable as we only ever need to repaint a document that
belongs to traversable.
2025-02-01 23:31:16 +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
Aliaksandr Kalenik
74dde4dc0f LibWeb: Rename is_scrollable() to could_be_scrolled_by_wheel_event()
Previous name for misleading because it checks if box could be scrolled
by user input event which is diffent from checking if box is scrollable.
For example box with `overflow: hidden` is scrollable but it can't be
scrolled by user input event.
2025-01-31 00:11:34 +01:00
Gingeh
108f3a9aac LibWeb: Implement popovertarget buttons 2025-01-30 15:46:52 -07:00
Shannon Booth
903c8860f8 LibWeb: Add metadata to children update steps invocation
Currently, this metadata is only provided on the insertion steps,
though I believe it would be useful to extend to the other cases
as well. This metadata can aid in making optimizations for these
steps by providing extra context into the type of change which
was made on the child.
2025-01-30 13:55:40 -07:00
Tim Ledbetter
39445d6dd6 LibWeb: Implement basic high resolution time coarsening
Several interfaces that return a high resolution time require that
time to be coarsened, in order to prevent timing attacks. This
implementation simply reduces the resolution of the returned timestamp
to the minimum values given in the specification. Further work may be
needed to make our implementation more robust to the kind of attacks
that this mechanism is designed to prevent.
2025-01-30 18:37:53 +01:00
Sam Atkins
a5be7cb6fb LibWeb: Initialize clearTargets in event dispatch correctly
Corresponds to https://github.com/whatwg/dom/pull/1347
2025-01-30 17:04:20 +01:00
Sam Atkins
3775f3399c LibWeb: Fix various typos
Corresponds to:
- https://github.com/whatwg/html/pull/10934
- https://github.com/whatwg/html/pull/10950
2025-01-30 15:00:16 +00:00
Sam Atkins
3e4b1056b5 LibWeb: Consistently use navigables for WebDriver BiDi
Corresponds to https://github.com/whatwg/html/pull/10909
2025-01-30 15:00:16 +00:00
Aliaksandr Kalenik
7da3b06e3e LibWeb: Postpone invalidating style of elements affected by :has()
...until Document::update_style(). This allows to avoid doing full
document DOM tree traversal on each Node::invalidate_style() call.

Fixes performance regression on wpt.fyi
2025-01-29 17:21:47 +01:00
Shannon Booth
1126049b91 LibWeb/DOM: Remove FIXME dbgln about unknown element name creation
Now that we have implemented most of the element types, this dbgln
is a lot less useful for detecting issues on live sites, and is in
my experience more likely to log cases that are not a FIXME. It also
cleans up some of the log spam from running tests.
2025-01-29 11:28:11 +01: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
e33037ad52 LibWeb: Add method to check if element affected by invalidation set
...by replacing existing method to check if an element is affected by
invalidation property. It turned out there is no need to check if an
element is affected only by some specific property, so it's more
convenient to have a method that accepts the whole set.
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
Luke Wilde
18b75afef8 LibWeb: Fire resize event at VisualViewport in the resize steps
Used on x.com to determine the layout to use on resize.
2025-01-29 09:23:52 +01:00
Luke Wilde
d21bac8028 LibWeb: Mark the Window resize event as trusted 2025-01-29 09:23:52 +01:00
Aliaksandr Kalenik
748d316973 LibWeb: Avoid separate tree traversal to mark for inherited style update
...in hover style invalidation. Instead, pass down a flag that indicates
all subsequent nodes in tree traversal have to be marked for inherited
style update.
2025-01-28 18:55:42 +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
b153420feb LibWeb: Reduce memory usage in hover style invalidation
Instead of allocating 3 vectors with size equal to the number of
elements potentially affected by hover:
- for the elements themselves
- for selector match state of each element before hovered node change
- for selector match state of each element after hovered node change

now we allocate none of them, but mark element for style recalculation
as we traverse the tree.
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
Tim Ledbetter
05351dfe45 LibWeb: Return wall time from PerformanceTiming interface
This matches the behavior of other browsers.
2025-01-27 14:53:33 +01:00