Commit graph

716 commits

Author SHA1 Message Date
Tim Ledbetter
a8904451ff LibWeb: Set navigation start time when creating a new Document
This currently uses a non spec-compliant property on the Response
object, which represents the time that the Response was created.

Setting this value allows `Performance.timeOrigin` to return a
reasonable value.
2025-01-27 14:53:33 +01:00
Shannon Booth
4f80c7a5f3 LibWeb/DOM: Use as_if for node of type checks in iteration helpers
These are very hot functions in profiles, so let's avoid a potential
double dynamic_cast or virtual call. For consistency, port all of
these classes of function over to 'as_if' instead.
2025-01-27 00:10:14 +00:00
Shannon Booth
22a7cd9700 LibWeb: Port Document encoding_parse_url and parse_url to Optional<URL>
This ports two more APIs away from URL::is_valid.
2025-01-27 00:03:07 +00:00
Aliaksandr Kalenik
cfe9b7a82b LibWeb: Recompute relative units in Element::recompute_inherited_style()
Properties with relative units has to be recomputed in inherited style
update as they depend on the parent's style.

Fixes https://github.com/LadybirdBrowser/ladybird/issues/3364
2025-01-26 23:10:58 +01:00
Andreas Kling
f35152cf61 LibWeb: Defer entire-subtree style invalidations
Instead of traversing the entire DOM subtrees and marking nodes for
style update, this patch adds a new mechanism where we can mark a
subtree root as "entire subtree needs style update".

A new pass in Document::update_style() then takes care of coalescing
all these invalidations in a single traversal of the DOM.

This shaves *minutes* of loading time off of https://wpt.fyi/ subpages.
2025-01-26 22:58:42 +01:00
Andreas Kling
e03aedbdf0 LibWeb: Add direct pointer to CSS::Selector in MatchingRule struct
This avoids looking up the selector by index repeatedly, giving us a
~400ms reduction in load time on https://wpt.fyi/
2025-01-26 15:07:23 +01:00
Aliaksandr Kalenik
9f75e26385 LibWeb: Add missing handler in affected_by_invalidation_property()
...for :placeholder-shown pseudo-class.
2025-01-26 00:52:38 +01:00
Jelle Raaijmakers
0bb0061915 LibWeb: Fire input events in .execCommand()
We do not fire `beforeinput` events since other browsers do not seem to
do so either.

The spec asks us to check whether a command's action modified the DOM
tree. This means adding or removing nodes and attributes, or changing
character data anywhere in the tree. We have
`Document::dom_tree_version()` for node updates, but for character data
a new version number is introduced that allows us to easily keep track
of any text changes in the entire tree.
2025-01-24 23:53:26 +01:00
Jelle Raaijmakers
f731cffbd8 LibWeb: Refuse to recursively execute .execCommand()
Spec issue:

  https://github.com/w3c/editing/issues/477
2025-01-24 23:53:26 +01:00
Jelle Raaijmakers
486bce8dad LibWeb: Only clear command states and values if range actually changed 2025-01-24 23:53:26 +01:00
Andreas Kling
4bef0d0aea LibWeb: Don't rebuild rule cache so eagerly to check for :has/:defined
Instead, change the APIs from "has :foo" to "may have :foo" and return
true if we don't have a valid rule cache at the moment.

This allows us to defer the rebuilding of the rule cache until a later
time, for the cost of a wider invalidation at the moment.

Do note that if our rule cache is invalid, the whole document has
invalid style anyway! So this is actually always less work. :^)

Knocks ~1 second of loading time off of https://wpt.fyi/
2025-01-24 17:54:34 +01:00
Andreas Kling
adc25af8e2 LibWeb: Make "assign slottables for a tree" fast when there are no slots
We achieve this by keeping track of the number of HTMLSlotElements
inside each ShadowRoot (do via ad-hoc insertion and removal steps.)

This allows slottables assignment to skip over entire shadow roots when
we know they have no slots anyway.

Massive speedup on https://wpt.fyi/ which no longer takes minutes/hours
to load, but instead a "mere" 19 seconds. :^)
2025-01-23 21:38:31 +01:00
Andreas Kling
7269fc3e52 LibWeb: Pass old parent's root to Node::removed_from()
This will allow nodes to access the root they've just been removed from.
2025-01-23 21:38:31 +01:00
Jelle Raaijmakers
b7a554d1f2 LibWeb: Reorder paintable hit-testing to account for pointer-events
Instead of ignoring any paintable immediately when they're invisible to
hit-testing, consider every candidate and while the most specific
candidate is invisible to hit-testing, traverse up to its parent
paintable.

This more closely reflects the behavior expected when wrapping block
elements inside inline elements, where although the block element might
have `pointer-events: none`, it still becomes part of the hit-test body
of the inline parent.

This makes the following link work as expected:

  <a href="https://ladybird.org">
    <div style="pointer-events: none">Ladybird</div>
  </a>
2025-01-23 09:33:10 +01:00
Jelle Raaijmakers
7eb4f3da37 LibGfx: Add Rect::unite()
The existing `::unite_horizontally()` and `::unite_vertically()` tests
did not properly test the edge cases where left/top in the Rect were
updated, so they get re-arranged a bit.
2025-01-23 09:33:10 +01:00
Shannon Booth
fd27eef0d1 LibWeb: Return OptionalNone from DOMURL::parse on failure
This ports one more function away from needing to use the awkward
valid state of the URL class.
2025-01-22 12:33:55 +00:00
Aliaksandr Kalenik
c50e89bb73 LibWeb: Don't include changed attribute twice in changed_properties
Changed attribute name is always added into this list right before
`invalidate_style()` is called.
2025-01-21 21:07:47 +01:00
Aliaksandr Kalenik
3c41bdbf92 LibWeb: Optimize style invalidation for presentational hint attributes
Use invalidation sets for presentational hint attribute invalidation
instead of falling back to full descendants and siblings invalidation.
The only difference for presentational hint attributes is that we always
have to invalidate the style of element itself.
2025-01-21 21:07:47 +01:00
Tim Ledbetter
a0b0e91d4f LibWeb: Disallow Editing API calls on non-HTML documents
This is not directly mentioned in the Editing API spec, but all major
browsers do this and there is a WPT for this behavior.
2025-01-21 19:08:37 +01:00
Sam Atkins
6f8d3014ea LibWeb/HTML: Update Document::create_and_initialize() to latest spec
This corresponds to part of https://github.com/whatwg/html/pull/10818
2025-01-21 18:17:18 +01:00
Sam Atkins
c073127b99 LibWeb/DOM: Return Document's Origin by reference
We don't want to have to copy these unnecessarily.
2025-01-21 18:17:18 +01:00
Timothy Flynn
85b424464a AK+Everywhere: Rename verify_cast to as
Follow-up to fc20e61e72.
2025-01-21 11:34:06 -05:00
Aliaksandr Kalenik
e0051db62e LibWeb: Use invalidation sets for "style" attribute invalidation 2025-01-20 18:23:34 +01:00
Aliaksandr Kalenik
34bf833a0a LibWeb: Expand invalidation sets usage to any attribute change
Before this change invalidation sets were only used for "class" and "id"
attribute changes.
2025-01-19 19:54:38 +01:00
Aliaksandr Kalenik
039e0d478a LibWeb: Use invalidation sets for id attribute style invalidation 2025-01-19 19:54:38 +01:00
Aliaksandr Kalenik
c5f2a88f69 LibWeb: Use invalidation sets to reduce style recalculation
Implements idea described in
https://docs.google.com/document/d/1vEW86DaeVs4uQzNFI5R-_xS9TcS1Cs_EUsHRSgCHGu8

Invalidation sets are used to reduce the number of elements marked for
style recalculation by collecting metadata from style rules about the
dependencies between properties that could affect an element’s style.

Currently, this optimization is only applied to style invalidation
triggered by class list mutations on an element.
2025-01-19 19:54:38 +01:00
Andreas Kling
e5d521bef8 LibWeb: Enable partial layout tree update in a bunch of cases
These common cases now cause us to invalidate the layout tree starting
at the relevant parent node instead of invalidating the entire tree.

- DOM node insertion
- DOM node removal
- innerHTML setter
- textContent setter

This makes a lot of dynamic content much faster. For example, demos
on shadertoy.com go from ~18 fps to ~28 fps on my machine.
2025-01-18 21:01:01 +01:00
Andreas Kling
c01d810e5a LibWeb: Implement partial layout tree updates
DOM nodes now have two additional flags:

- Needs layout tree update
- Child needs layout tree update

These work similarly to the needs-style-update flags, but instead signal
the need to rebuild the corresponding part of the layout tree.

When a specific DOM node needs a layout tree update, we try to create
a new subtree starting at that node, and then replace the subtree in the
old layout tree with the newly created subtree.

This required some refactoring in TreeBuilder so that we can skip over
entire subtrees during a tree update.

Note that no partial updates happen yet (as of this commit) since we
always invalidate the full layout tree still. That will change in the
next commit.
2025-01-18 21:01:01 +01:00
Shannon Booth
76397c9ecd LibWeb: Use finalize for cleaning up all navigables
The use of this HashMap looks very spooky, but let's at least use
finalize when cleaning them up on destruction to make things slightly
less dangerous looking.
2025-01-17 10:08:42 +01:00
Timothy Flynn
0c09a099a5 LibWeb: Add missing #include to InputEventsTarget.h
While investigating an issue with Unicode::Segmenter (the result of
which is passed to functions in this file), this missing include was
causing clangd to be unable to find non-virtual implementations.
2025-01-16 23:22:48 +01:00
Aliaksandr Kalenik
98691810b1 LibWeb: Fix insert/delete rule invalidation for adopted style sheets
Invalidation for adopted style sheets was broken because we had an
assumption that "active" style sheet is always attached to
StyleSheetList which is not true for adopted style sheets. This change
addresses that by keeping track of all documents/shadow roots that own
a style sheet and notifying them about invalidation instead of going
through the StyleSheetList.
2025-01-13 23:03:07 +01:00
Tim Ledbetter
619df0bc2c LibWeb: Don't crash when upgrading custom element with a bad constructor
Previously, a crash would occur when attempting to throw an error in
this case because the method used to create the exception tried to get
the current realm from the execution context stack, which is empty. The
realm is now passed explicitly when constructing the error, avoiding
the crash.
2025-01-13 10:55:46 +00:00
Gingeh
403cda5cd6 LibWeb: Allow color keywords in theme-color metadata 2025-01-12 19:47:01 +00:00
Gingeh
9f2ce259ad LibWeb: Check for missing node or invalid query in obtain_theme_color 2025-01-12 19:47:01 +00:00
Timothy Flynn
a4324ee6e9 LibWeb: Remove the initialize_strings methods
We added these methods to propagate OOM errors at process startup, but
we longer fret about these tiny OOM failures. Requiring that these init
methods be called prohibits using these strings in processes that have
not set up a MainThreadVM. So let's just remove them and initialize the
strings in a sane manner.

In doing so, this also standardizes how we initialize strings whose C++
variable name differs from their string value. Instead of special-casing
these strings, we just include their string value in the x-macro list.
2025-01-12 00:35:58 +01:00
Psychpsyo
19f97472eb LibWeb: Verify element namespace in DOM::create_element() 2025-01-11 23:29:17 +01:00
Tim Ledbetter
1e9e2b6564 LibWeb: Clone all attribute properties when cloning a single node
Previously, the namespace of the attributes on the cloned element was
not being set.
2025-01-11 23:10:09 +01:00
Tim Ledbetter
a467005855 LibWeb: Make node cloning methods const 2025-01-11 23:10:09 +01:00
Psychpsyo
31b20e38ee LibWebView: Fix capitalization in devtools 2025-01-11 16:28:51 -05:00
Shannon Booth
64eeda6450 LibWeb: Return a representation of an 'Agent' in 'relevant agent'
This makes it more convenient to use the 'relvant agent' concept,
instead of the awkward dynamic casts we needed to do for every call
site.

mutation_observers is also changed to hold a GC::Root instead of raw
GC::Ptr. Somehow this was not causing problems before, but trips up CI
after these changes.
2025-01-11 10:39:48 -05:00
Sam Atkins
f11347739a LibWeb/HTML: Stop observing lazy-loaded iframes when navigating 2025-01-11 11:10:43 +01:00
Sam Atkins
97616fa108 LibWeb/DOM: Move "stop intersection observing..." code into a method
We'll need to call it from elsewhere.

Also add a missing step 5 from where we previously called it.
2025-01-11 11:10:43 +01:00
Sam Atkins
c6a18f795d LibWeb/HTML: Pass user_involvement through navigables code
This corresponds to part of https://github.com/whatwg/html/pull/10818
2025-01-11 11:10:43 +01:00
Jelle Raaijmakers
9750896af3 LibWeb: Implement the "push down values" editing algorithm 2025-01-10 23:33:35 +01:00
Jelle Raaijmakers
e21ee10b3c LibWeb: Add command state & value overrides to DOM::Document 2025-01-10 23:33:35 +01:00
Jelle Raaijmakers
d08febcf67 LibWeb: Add Node::for_each(_inclusive)_ancestor()
Allows for easy iteration over the chain of ancestors for a node.
2025-01-10 23:33:35 +01:00
Jelle Raaijmakers
4323669939 LibWeb: Add Range::for_each_contained()
This centralizes the logic for iterating over a Range's contained nodes.
2025-01-10 23:33:35 +01:00
Aliaksandr Kalenik
cf57efd252 LibWeb: Unload fonts when adopted style sheet is removed
Add missing unloading step that we do for regular style sheets but
mistakenly missed for adopted style sheets.
2025-01-10 19:12:48 +03:00
Shannon Booth
1f4c7ac62b LibWeb/DOM: Remove uneccessary ExceptionOr for NodeIterator::create 2025-01-10 08:08:46 +00:00
Shannon Booth
17c92d7c2b LibWeb/DOM: Create TreeWalker in document's realm 2025-01-10 08:08:46 +00:00