Commit graph

859 commits

Author SHA1 Message Date
Timothy Flynn
2803d66d87 AK: Support UTF-16 string formatting
The underlying storage used during string formatting is StringBuilder.
To support UTF-16 strings, this patch allows callers to specify a mode
during StringBuilder construction. The default mode is UTF-8, for which
StringBuilder remains unchanged.

In UTF-16 mode, we treat the StringBuilder's internal ByteBuffer as a
series of u16 code units. Appending a single character will append 2
bytes for that character (cast to a char16_t). Appending a StringView
will transcode the string to UTF-16.

Utf16String also gains the same memory optimization that we added for
String, where we hand-off the underlying buffer to Utf16String to avoid
having to re-allocate.

In the future, we may want to further optimize for ASCII strings. For
example, we could defer committing to the u16-esque storage until we
see a non-ASCII code point.
2025-07-18 12:45:38 -04:00
Timothy Flynn
fe676585f5 AK: Add a UTF-16 string with optimized short- and ASCII-string storage
This is a strictly UTF-16 string with some optimizations for ASCII.

* If created from a short UTF-8 or UTF-16 string that is also ASCII,
  then the string is stored in an inlined byte buffer.

* If created with a long UTF-8 or UTF-16 string that is also ASCII,
  then the string is stored in an outlined char buffer.

* If created with a short or long UTF-8 or UTF-16 string that is not
  ASCII, then the string is stored in an outlined char16 buffer.

We do not store short non-ASCII text in the inlined buffer to avoid
confusion with operations such as `length_in_code_units` and
`code_unit_at`. For example, "😀" would be stored as 4 UTF-8 bytes
in short string form. But we still want `length_in_code_units` to
be 2, and `code_unit_at(0)` to be 0xD83D.
2025-07-18 12:45:38 -04:00
Timothy Flynn
7fad8c333d LibWeb: Use forward-declarations of structured serialized types
This reduces the rebuilt targets when touching StructuredSerialize.h
from ~1200 to ~400. The remaining are due to generated IPC headers.
2025-07-18 10:09:02 -04:00
norbiros
7ad01d28a8 LibWeb/CSS: Add basic registered properties with initial values
Add global registry for registered properties and partial support
for `@property` rule. Enables registering properties with initial
values. Also adds basic retrieval via `var()`.

Note: This is not a complete `@property` implementation.
2025-07-18 11:12:39 +01:00
Luke Wilde
8b0b3b186f LibWeb/CSP: Implement the style-src directive 2025-07-18 11:58:04 +12:00
Aliaksandr Kalenik
1bf4d3391e LibWeb: Use GC::Ptr for BrowsingContext pointer saved in Document
Likely we forgot to update `WeakPtr` to `GC::Ptr` after converting
`BrowsingContext` to GC-allocated object.
2025-07-17 15:55:30 +01:00
Sam Atkins
b7b504cf2d LibWeb: Remove XML-derived attribute name validation from DOMStringMap
Corresponds to 841fcfdb2c
2025-07-17 13:54:01 +02:00
Aliaksandr Kalenik
aa31b69e7e LibWeb+WebContent: Delete unused page_did_layout() 2025-07-17 08:24:19 +02:00
Aliaksandr Kalenik
b0ef7f4427 LibWeb: Defer invalidation sets processing until update_style()
Some checks are pending
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
This change converts `Node::invalidate_style()` (invalidation sets
overload) from eagerly doing tree traversal that marks elements affected
by invalidation set to instead adding "pending invalidation sets" into
`StyleInvalidator`, processing of which is deferred until the next
`update_style()`. By doing that we sometimes substantially reduce amount
of work done performing tree traversal that marks elements for style
recalculation.

Improves performance on Discord, were according to my measurements we
were previously spending 20% of time in style invalidation, but now it's
down to <1%.
2025-07-17 00:43:26 +02:00
Aliaksandr Kalenik
8cbe27b2f9 LibWeb: Move perform_pending_style_invalidations() in StyleInvalidator
This change introduces StyleInvalidator as a preparation for upcoming
change that will make `perform_pending_style_invalidations()` take care
of pending invalidation sets.
2025-07-17 00:43:26 +02:00
Aliaksandr Kalenik
9a5ef95022 LibWeb: Delete unused m_animation_driver_timer in Document
Some checks failed
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Build Dev Container Image / build (push) Has been cancelled
2025-07-16 22:09:46 +02:00
Sam Atkins
9bc5c6fb35 LibWeb/DOM: Add document getter to AbstractElement 2025-07-16 14:47:45 +01:00
Callum Law
6a9c8d7767 LibWeb: Don't resolve colors with unresolved components
`CSSColorValue`s which have unresolved `calc` components should be able
to be resolved. Previously we would always resolve them but with
incorrect values.

This is useful as we will now be able to now whether we should serialize
colors in their normalized form or not.

Slight regression in that we now serialize (RGB, HSL and HWB) colors
with components that rely on compute-time information as an empty
string, but that will be fixed in the next commit.
2025-07-16 13:05:33 +01:00
Aliaksandr Kalenik
6ddc582551 LibWeb: Delete unreachable code in Node::invalidate_style()
`if (invalidation_set.needs_invalidate_whole_subtree())` branch in the
end of the function was always false.
2025-07-15 18:31:43 -04:00
Sam Atkins
632ce9523b LibWeb/CSS: Add :unchecked pseudo-class
This just got added to the Selectors spec:

b78c97c19d

It's thus missing from the HTML spec and WPT, but I figured it was
simple enough to add.
2025-07-15 21:27:44 +02:00
Sam Atkins
b973c8d275 LibWeb/CSS: Remove the :target-within pseudo-class
This has been removed from the spec:

03b340c34f
2025-07-15 21:27:44 +02:00
Aliaksandr Kalenik
8142c311ec LibWeb: Reset document's cursor blink timer after selection change
For contenteditable `Selection` in collapsed state represents cursor
position, so whenever it's update, we should also reset the blink timer.

Fixes bug on Discord when cursor is blinking while it is being moved by
pressing arrow keys.
2025-07-15 17:25:02 +01:00
Aliaksandr Kalenik
9b9acf0bd5 LibWeb: Delete schedule_style_update() and schedule_layout_update()
Both functions schedule HTML event loop processing but that's
unnecessary, because we schedule a rendering task that checks if
style/layout needs an update 60/s anyway.
2025-07-15 16:57:35 +02:00
Shannon Booth
15657ec4c3 LibWeb: Include or declare missing definitions for PseudoElement.h
clangd was loudly complaining about this file.
2025-07-15 13:54:17 +01:00
Aliaksandr Kalenik
9bb5e70f5a LibWeb: Skip tree traversal if invalidation set doesn't have properties
This allows us to skip unnecessary tree traversal to mark style for
recalculation when invalidation set only has `InvalidateSelf`.
2025-07-14 09:32:05 +02:00
Aliaksandr Kalenik
8ae7417445 LibWeb: Add internals call to dump display list
It's useful to have tests that dump display list items, so we can more
easily see how changes to the display list recording process affect the
output. Even the small sample test added in this commit shows that we
currently record an unnecessary AddClipRect item for empty paint phases.

For now, the dump doesn't include every single property of an item, but
we can shape it to include more useful information as we iterate on it.
2025-07-13 19:15:05 +02:00
Aliaksandr Kalenik
410e82c9fd LibWeb: Rearrange code such that a lot less files include Command.h
Some checks failed
CI / Linux, x86_64, Fuzzers, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, GNU (push) Waiting to run
CI / macOS, arm64, Sanitizer, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer, Clang (push) Waiting to run
Package the js repl as a binary artifact / Linux, arm64 (push) Waiting to run
Package the js repl as a binary artifact / macOS, arm64 (push) Waiting to run
Package the js repl as a binary artifact / Linux, x86_64 (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Build Dev Container Image / build (push) Has been cancelled
With this change number of recompiled files after modification of
`Command.h` goes down from >1000 to <100.
2025-07-11 17:37:27 +02:00
Callum Law
d980321a77 LibWeb: Include own shadow root in for_each_shadow_including_descendant
Fixes a crash in wpt.live/css/cssom/CSSStyleSheet-constructable.html
2025-07-11 16:21:11 +02:00
Jelle Raaijmakers
2998049fe9 LibWeb: Implement the unreachable scrollable overflow
Whenever we end up with a scrollable overflow rect that goes beyond
either of its axes (i.e. the rect has a negative X or Y position
relative to its parent's absolute padding box position), we need to clip
that rect to prevent going into the "unreachable scrollable overflow".

This fixes the horizontal scrolling on https://ladybird.org (gets more
pronounced if you make the window very narrow).
2025-07-11 08:23:46 +02:00
Shannon Booth
05ddf5c718 LibWeb/DOM: Add StorageEvent and DragEvent to Document.createEvent
We have since implemented these interfaces.
2025-07-11 08:22:36 +02:00
Luke Wilde
0cff47828d LibWeb/CSP: Implement the script-src directive 2025-07-09 15:52:54 -06:00
Luke Wilde
2368641de5 LibWeb: Track if element was created from token with dupe attributes
This is required for CSP to ignore the nonce attribute to prevent
duplicate attributes hijacking the attribute.

See https://w3c.github.io/webappsec-csp/#security-nonce-hijacking
2025-07-09 15:52:54 -06:00
Lucien Fiorini
5d85959f5f LibWeb/SVG: Add FEGaussianBlurElement 2025-07-09 18:07:12 +01:00
Lucien Fiorini
f8b12614df LibWeb/SVG: Add FEBlendElement 2025-07-09 18:07:12 +01:00
Lucien Fiorini
d3684a36b0 LibWeb/SVG: Add FEFloodElement 2025-07-09 18:07:12 +01:00
Sam Atkins
cd4ea67706 LibWeb/DOM: Add custom-property helpers to AbstractElement 2025-07-09 16:44:20 +01:00
Sam Atkins
97ad1ea7e0 LibWeb/DOM: Don't crash getting custom properties from invalid pseudos 2025-07-09 16:44:20 +01:00
Tim Ledbetter
57dd85e4ac LibWeb/DOM: Throw errors from correct realm in Node::move_node() 2025-07-09 15:59:06 +02:00
Andreas Kling
b3fd939628 LibWeb: Make sure we run selectors for mixed-case tag names
Before this change, we would never apply CSS rules where the selector
had a mixed-case tag name. This happened because our rule caches would
key them on the lowercased tag name, but we didn't lowercase the tag
name when fetching things from the cache.

This uncovered the fact that the SVG2 spec has a bunch of style applied
to non-rendered elements in a way that doesn't match other browsers.
Instead of blindly following the spec, we now match other browsers.
2025-07-09 14:36:08 +02:00
Shannon Booth
24d522afce LibWeb/DOM: Avoid passing null to valid name prefix
Applies spec change of 1b4bd48
which we already happened to be doing to avoid crashing.
2025-07-09 10:57:14 +02:00
Shannon Booth
642a2430a9 LibWeb/DOM: Properly extract local name and prefix from strict split
Previously we were taking the local name from everything after the
first ':', instead of second element of strictly splitting qualified
name.
2025-07-09 10:57:14 +02:00
Sam Atkins
a424a06d45 LibWeb/DOM: Copy document's allow declarative shadow roots when cloning
Corresponds to 77920094a4
2025-07-08 17:08:39 +01:00
Tim Ledbetter
d7deb6d58f LibWeb: Make document origin opaque by default
This aligns our behavior with the specification, which says the default
value for a Document's origin is opaque unless otherwise specified
2025-07-06 13:39:45 +12:00
Tim Ledbetter
b3fa54a791 LibWeb: Set origin of new document in Document.parseHTMLUnsafe()
Previously, a crash would occur when accessing the origin of a document
created with this method.
2025-07-06 13:39:45 +12:00
Jelle Raaijmakers
1ec3b1c6df LibWeb: Simplify Document::elements_from_point()
We should not need to check if the result of a hit test is actually
visible for hit testing, because if it wasn't, it should not have been
returned from PaintableBox::hit_test() in the first place.
2025-07-05 23:56:42 +01:00
Sam Atkins
b6ffea8990 LibWeb: Give Element a CustomStateSet, exposed by ElementInternals 2025-07-04 18:10:28 +01:00
Sam Atkins
e63d81b36e LibWeb: Add CustomStateSet IDL type 2025-07-04 18:10:28 +01:00
Aliaksandr Kalenik
082053d781 LibWeb+WebContent+WebWorker: Move backing store allocation in Navigable
Making navigables responsible for backing store allocation will allow us
to have separate backing stores for iframes and run paint updates for
them independently, which is a step toward isolating them into separate
processes.

Another nice side effect is that now Skia backend context is ready by
the time backing stores are allocated, so we will be able to get rid of
BackingStore class in the upcoming changes and allocate PaintingSurface
directly.
2025-07-04 16:12:47 +02:00
Aliaksandr Kalenik
b73525ba0e LibWeb+WebContent: Delete unused "has focus" flag from paint config 2025-07-04 16:12:47 +02:00
Callum Law
9ab7c5d08d LibWeb: Support relative lengths in calc color values
Gains us ~40 WPT tests.
2025-07-04 13:18:55 +01:00
Callum Law
62d138ebf7 LibWeb: Allow passing a resolution context to CSSStyleValue::to_color
This will be used for resolving any calculated style values within the
various `CSSColorValue` sub-classes.

No functionality changes.
2025-07-04 13:18:55 +01:00
Aliaksandr Kalenik
5874b7a76f LibWeb: Skip update_associated_selection() when there's no selection
This change fixes at least two issues:
- `update_associated_selection()` is responsible for selectionchange
  dispatch, and we were incorrectly dispatching this event on ranges
  that were not associated with a selection.
- `Range::get_client_rects()` was using `update_associated_selection()`
  to refresh the selection state in the paintable tree for the current
  range, but since a range might not be associated with a selection,
  this could make the painted selection reflect the state of an
  arbitrary range instead of the actual selection range.

Fixes a bug on Discord where any text typed into the message input would
get selected.
2025-07-03 22:16:39 +02:00
Timothy Flynn
9fc3e72db2 AK+Everywhere: Allow lonely UTF-16 surrogates by default
By definition, the web allows lonely surrogates by default. Let's have
our string APIs reflect this, so we don't have to pass an allow option
all over the place.
2025-07-03 09:51:56 -04:00
Timothy Flynn
86b1c78c1a AK+Everywhere: Prepare Utf16View for integration with a UTF-16 string
To prepare for an upcoming Utf16String, this migrates Utf16View to store
its data as a char16_t. Most function definitions are moved inline and
made constexpr.

This also adds a UDL to construct a Utf16View from a string literal:

    auto string = u"hello"sv;

This let's us remove the NTTP Utf16View constructor, as we have found
that such constructors bloat binary size quite a bit.
2025-07-03 09:51:56 -04:00
Aliaksandr Kalenik
2618956b6f LibWeb: Delete unused Range::inverted() 2025-07-03 15:00:49 +02:00