Commit graph

813 commits

Author SHA1 Message Date
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
Aliaksandr Kalenik
481ab338b4 LibWeb: Delete unused Range::normalized() 2025-07-03 15:00:49 +02:00
Sam Atkins
ffd5503dcb LibWeb/DOM: Serialize pseudo-elements in the correct order
Make Element responsible for serializing all its children, so it can put
them in order.
2025-07-03 09:56:56 +01:00
Sam Atkins
4df472988c LibWeb/DOM: Only list pseudo-elements that actually exist
This stops `::before` and `::after` nodes showing up for every single
element in the inspector tree. Unfortunately there's no way for us to
detect that one of these doesn't exist in layout but has *some* style
specified for it, but that seems like a rare use case.
2025-07-03 09:56:56 +01:00
Andreas Kling
b3d9e39bad LibWeb: Avoid infinite loop in HTMLElement.scrollParent
Some checks failed
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
Push notes / build (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, 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
Build Dev Container Image / build (push) Has been cancelled
We were failing to actually climb up the containing block chain,
causing this API to infinite loop for anything but the most
trivial cases.

By fixing the loop structure, we also make a bunch of the already
imported WPT tests pass. :^)
2025-06-30 20:38:21 +01:00
Shannon Booth
a2b523eeb8 LibURL: Replace use of URL::get_public_suffix
It is confusing to have both URL::Host::public_suffix and
URL:get_public_suffix, both with slightly different semantics.

Instead, use PublicSuffixData for cases that just want a direct
match against the list, and URL::Host::public_suffix in LibWeb
land as the URL spec defined AO.
2025-06-29 12:47:57 +01:00
Shannon Booth
20d369b96d LibWeb/HTML: Implement the exception checks for Document.domain setter 2025-06-27 18:45:48 +12:00
Tim Ledbetter
48f1bf7ef3 LibWeb: Remove Document::parse_url()
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (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 is no longer used.
2025-06-24 19:55:43 +02:00
Tim Ledbetter
ff3d3840ac LibWeb: Replace usages of Document::parse_url()
The spec has been updated to use `encoding_parse_url()` and
`encoding_parse_and_serialize_url()` instead.
2025-06-24 19:55:43 +02:00
Sam Atkins
423cdd447d LibWeb+LibGfx: Apply editorial punctuation/whitespace/markup fixes
Corresponds to d426109ea1
and fd08f81d06
2025-06-25 03:12:19 +12:00
Sam Atkins
2e2b456009 LibWeb/DOM: Update spec steps for scrolling into view
Corresponds to dad91f49ef

The spec text doesn't actually require any changes from us, but I
noticed we were incorrectly calling `is_shadow_including_ancestor_of()`
instead of `is_shadow_including_inclusive_ancestor_of()`, so that's
fixed.
2025-06-25 03:12:19 +12:00
Jelle Raaijmakers
53c35c5d3b LibWeb: Bring update_the_visibility_state() steps in sync with the spec
No functional changes.
2025-06-24 11:33:41 +02:00
Shannon Booth
3383a781f6 LibWeb/DOM: Support changing document to observe in DocumentObserver 2025-06-24 09:56:14 +02:00
Glenn Skrzypczak
6b84cd8d11 LibWeb/HTML: Correctly set base elements frozen base url
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (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 commit implements the fallback to the documents fallback base url
if the href of the first base element is a data or javascript url.

Additionally the frozen base url is set, if a base element becomes the
first base element with an href content attribute because the previous
one got removed.
2025-06-23 18:56:42 +12:00
Tomasz Strejczek
6fb2be96bf Everywhere: Replace DateTime::to_string() with UnixDateTime::to_string()
Replace LibCore::DateTime::to_string() with
AK::UnixDateTime::to_string().
Remove unncessary #include <LibCore/DateTime.h>.
2025-06-19 18:42:45 -06:00
Tim Ledbetter
70f9eb9d8f LibWeb: Remove Document::validate_qualified_name()
This is no longer used and appears to have been removed from the
specification.
2025-06-19 15:06:58 +02:00
Tim Ledbetter
16dbb44de2 LibWeb: Update DOMImplementation.createDocumentType() name validation
This now follows the latest specification steps.
2025-06-19 07:55:47 -04:00
Sam Atkins
f98312d022 LibWeb/DOM: Move pseudo-element scroll offsets into PseudoElement 2025-06-19 12:35:31 +01:00
Sam Atkins
c1d4323cf7 LibWeb: Support counter-* properties on pseudo-elements
There are multiple things happening here which are interconnected:

- We now use AbstractElement to refer to the source of a counter, which
  means we also need to pass that around to compute `content`.

- Give AbstractElement new helper methods that are needed by
  CountersSet, so it doesn't have to care whether it's dealing with a
  true Element or PseudoElement.

- The CountersSet algorithms now walk the layout tree instead of DOM
  tree, so TreeBuilder needs to wait until the layout node exists
  before it resolves counters for it.

- Resolve counters when creating a pseudo-element's layout node. We
  awkwardly compute the `content` value up to twice: Once to figure out
  what kind of node we need to make, and then if it's a string, we do
  so again after counters are resolved so we can get the true value of
  any `counter()` functions. This will need adjusting in the future but
  it works for now.
2025-06-19 12:35:31 +01:00
Sam Atkins
67241d6419 LibWeb/DOM: Give PseudoElement a CountersSet
This is not yet used
2025-06-19 12:35:31 +01:00
Sam Atkins
46e95db355 LibWeb: Make Element::get_pseudo_element() public 2025-06-19 12:35:31 +01:00
Sam Atkins
498e143687 LibWeb/DOM: Stop including Element.h from AbstractElement.h
We only need the forward declaration here, and this will let us include
AbstractElement from Element.
2025-06-19 12:35:31 +01:00
Sam Atkins
a57595faf5 LibWeb: Make [resolve,inherit]_counters() take AbstractElement
This is one of those cases where the spec says "element" and
means "element or pseudo-element". The easiest way to handle both is to
make these be free functions that take an AbstractElement, and then
give AbstractElement some helper methods so that the caller doesn't
have to care which it's dealing with.

There are some FIXMEs here because PseudoElement doesn't have a
CountersSet yet, and because the CountersSet currently uses a
UniqueNodeID to identify counter sources, which doesn't support
pseudo-elements.
2025-06-19 12:35:31 +01:00
Sam Atkins
ce380a59c7 LibWeb/DOM: Rename ElementReference to AbstractElement
This isn't some kind of identifier, it's a handle on an actual Element
or PseudoElement.
2025-06-19 12:35:31 +01:00
Sam Atkins
e7c2f0dd52 LibWeb: Make PseudoElement a class in its own right
It's getting a bit large and complicated to be a struct hidden in
DOM::Element.
2025-06-19 12:35:31 +01:00
Tim Ledbetter
727b4f5e89 LibWeb: Update attribute name validation in Document.createAttribute()
This now follows the latest specification steps.
2025-06-19 11:00:53 +01:00
Tim Ledbetter
ba641d4784 LibWeb: Update attribute name validation in Element.setAttribute()
This now follows the latest specification steps.
2025-06-19 11:00:53 +01:00
Tim Ledbetter
acf2d773d5 LibWeb: Update attribute name validation in Element.toggleAttribute
This now follows the latest specification steps.
2025-06-19 11:00:53 +01:00
Tim Ledbetter
4593c28769 LibWeb: Update validate_and_extract and its users to the latest spec 2025-06-19 11:00:53 +01:00
Gingeh
e96338dd63 LibWeb: Don't play initially-paused animations
Some checks are pending
CI / macOS, arm64, Sanitizer_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Fuzzers_CI, Clang (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, GNU (push) Waiting to run
CI / Linux, x86_64, Sanitizer_CI, Clang (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
2025-06-18 17:17:29 +02:00
Shannon Booth
fc62a05c98 LibWeb/DOM: Set Document's origin in JS constructor
We were missing this spec step, which meant that the created
document had no origin, causing a crash when accessed for same-origin
checks.
2025-06-18 08:49:01 +02:00
Shannon Booth
e0d7278820 LibURL+LibWeb: Make URL::Origin default constructor private
Instead, porting over all users to use the newly created
Origin::create_opaque factory function. This also requires porting
over some users of Origin to avoid default construction.
2025-06-17 20:54:03 +02:00
Shannon Booth
5deb8ba2f8 LibWeb: Explicitly set Document's origin
As part of the effort of removing the default constructor of
Origin, since document has the origin set after construction,
port Document's origin over to an Optional<Origin>.

This exposes that we were never setting the origin of the document
during fragment parsing. For now, to maintain previous behaviour,
let's explicitly set it to an opaque origin.
2025-06-17 20:54:03 +02:00
Shannon Booth
0d905b1846 LibWeb: Give NavigationParams a constructor
This allows for NavigationParams to hold non-default constructable
types.
2025-06-17 20:54:03 +02:00
Jelle Raaijmakers
e8e6dbcee0 LibWeb: Fix document element's .scrollHeight and .scrollWidth
We were using the viewport's size as the viewport scrolling area, but
those are completely different things.
2025-06-17 17:17:34 +01:00
Jelle Raaijmakers
e104d896eb LibWeb: Rename Navigable::m_size to ::m_viewport_size
All other viewport-related dimensions are referenced to by 'viewport',
so let's rename the member that stores the viewport size to prevent
further confusion.
2025-06-17 17:17:34 +01:00
Sam Atkins
3fbd3146a1 LibWeb/DOM: Implement "is closed-shadow-hidden" algorithm 2025-06-17 12:38:27 +01:00
Manuel Zahariev
00d43b39d1 LibWeb: Retain calculated Element::ordinal_value for lists
`Element::ordinal_value` is called for every `li` element in
a list (ul, ol, menu).

Before:
  `ordinal_value` iterates through all of the children of the list
  owner. It is called once for each element: complexity $O(n^2)$.

After:
  - Save the result of the first calculation in `m_ordinal_value`
  then return it in subsequent calls.
  - Tree modifications are intercepted and trigger invalidation
    of the first node's `m_ordinal_value`:
    - insert_before
    - append
    - remove
  Results in noticeable performance improvement rendering' large
  lists: from 20s to 4s for 20K elements.
2025-06-16 12:44:58 +01:00
Manuel Zahariev
d27b43c1ee LibWeb: Add specialized fast_is for lists
Before:
  `is<HTMLOLListElement>` and other similar calls in this commit
  are resolved to `dynamic_cast`, which incurs runtime overhead
  for resolving the type. The Performance hit becomes apparent
  when rendering large lists. Callgrind analysis points to a
  significant performance hit from calls to `is<...>` in
  `Element::list_owner`.

Reference: Michael Gibbs and Bjarne Stroustrup (2006) Fast dynamic
casting. Softw. Pract. Exper. 2006; 36:139–156

After:
  Implement inline `fast_is` virtual method that immediately
  resolves the type. Results in noticeable performance improvement
  2x-ish for lists with 20K entries.

Bonus: Convert starting value for LI elements to signed integer
    The spec requires the start attribute and starting value to be
    "integer". Firefox and Chrome support a negative start attribute.

FIXME: At the time of this PR, about 134 other objects resolve
`is<...>` to `dynamic_cast`. It may be a good idea to coordinate
similar changes to at least [some of] the ones that my have impact
on performance (maybe open a new issue?).
2025-06-16 12:44:58 +01:00
Jelle Raaijmakers
15c436b332 LibWeb: Implement basic focus indication for :focus-visible
This causes links to no longer show an outline when clicked; only when
using keyboard navigation with the tab key will the outline show up.
2025-06-13 17:39:11 +02:00
Jelle Raaijmakers
7016921067 LibWeb: Remember last focus trigger in Document
We will need this to implement focus indication.
2025-06-13 17:39:11 +02:00
Jelle Raaijmakers
b42c2c5e8f LibWeb: Use code unit offsets in Document::find_matching_text()
We were passing in byte offsets instead of UTF-16 code unit offsets,
which could lead to crashes if the offsets found exceeded the number of
code units in text fragments on the page.

Fixes #4908.

Co-authored-by: Tim Ledbetter <tim.ledbetter@ladybird.org>
2025-06-13 15:08:26 +02:00
Andreas Kling
07ff75bbec LibWeb: Don't traverse entire layout tree in every HTML::EventLoop tick
Instead, collect a list of all the elements with content-visibility:auto
after layout.

This way we can skip the tree traversal when updating the rendering.

This was previously eating up ~300 µs of the 60fps frame budget on
our GitHub repo pages (and even more on large pages).
2025-06-11 16:25:42 +02:00
Callum Law
fc46abb83f LibWeb: Make element_reference optional in create_resolved_style 2025-06-09 12:28:41 +01:00
Callum Law
610ad25555 LibWeb: Update m_inline_style when element style attribute is removed
Previously we would just throw it away and construct a new (empty) one
when required. This doesn't work as any existing references to the old
instance will contain out of date information. Now we retain and update
the existing instance instead.
2025-06-09 12:06:44 +01:00
Sam Atkins
ea33bdc975 LibWeb: Move non-DOM-related methods from DOM::Node to TreeNode
Motivated by wanting to do `is_before()` on a Layout::Node, and thought
I might as well move as many as possible while I was at it.
2025-06-07 16:51:00 +02:00
Andreas Kling
6dba720370 LibWeb: Invalidate layout tree at nearest non-anonymous ancestor
When marking a part of the layout tree for rebuild, if the subtree root
that we're marking has an anonymous parent, we now mark from the nearest
non-anonymous ancestor instead.

This ensures that subtrees inside anonymous wrappers don't just get
duplicated (i.e recreated but inserted instead of replaced).
2025-06-04 00:43:23 +02:00