Commit Graph

3725 Commits

Author SHA1 Message Date
Jelle Raaijmakers
1aeb080250 LibCompress: Treat LZW decoding errors as end of stream
The LZW data for both GIF and TIFF images is sometimes intentionally
missing an end-of-information (EOI) code, which technically is a
decoding error, but in practive is handled gracefully by Firefox, Safari
and Chrome for GIFs and Safari for TIFFs. Let's mirror their behavior.

The included WPT test exposes the fact that trailing garbage bytes can
also result in decoding errors. We handle this in the LZW logic rather
than in the image decoding since our LZW implementation is currently
only used by GIF and TIFF decoding. The error is logged behind the
LZW_DEBUG flag.
2026-04-29 20:28:15 +02:00
Andreas Kling
c9cac66677 Tests: Expand style invalidation coverage
Add broader style-invalidation regression coverage for selector shapes
that are easy to mis-model when invalidation data is flattened into
feature sets. Cover selector-list alternatives, nested pseudo-class
arguments, pseudo-element arguments, nth-child selector lists,
quirks-mode class matching, mixed-case ancestor filters, duplicate
invalidation rule merging, and concrete :has() feature filtering.

Keep the expectations on the unoptimized baseline so follow-up
optimization commits can show the counter progressions separately.
These tests dump the normal invalidation counters instead of asserting
specific counter values in script.
2026-04-29 15:47:23 +02:00
Callum Law
9a3f2b23a1 LibWeb: Add method to parse specific CSS keyword
This revealed an issue with `@counter-style/range` where parsing could
consume and ignore invalid keywords
2026-04-29 11:42:57 +01:00
Andreas Kling
ba23d6a8ca LibWeb: Fix abspos containing block for split inline ancestors
When an inline-relative is split by block-level descendants, the rect
computation only looked at one anonymous wrapper and returned empty
for everything else, sending abspos placement back to the initial
containing block. On Reddit this let an inline-relative ad host's <a>
overlay the entire viewport and steal clicks from the post gallery's
pager buttons.

Walk every descendant of the inline's real block container instead,
collecting fragments from any InlineNode part of the inline plus the
border-box rects of its in-flow Box descendants. Matches what other
engines expose via getClientRects() for split inlines.

While here, narrow the inline-CB detection to the triggers that
actually apply on non-atomic inlines: `position`, `filter`,
`backdrop-filter` (and their will-change hints). transform, contain
and the rest don't apply per their specs - the broader check would
have started routing the WPT contain-paint/contain-layout ib-split
tests through the inline path once the rect computation began
returning non-empty results.
2026-04-29 04:54:11 +02:00
Andreas Kling
fc931c93a9 Tests: Cover abspos CB for inline-relative with block descendant
An `inset: 0` abspos child of an inline-relative with block-level
descendants should size to the inline's full extent across the
"before"/"middle"/"after" wrappers. The expected output records the
current buggy state (most cases collapse to the viewport); the next
commit fixes it.
2026-04-29 04:54:11 +02:00
Andreas Kling
329a26307d LibWeb: Skip ::slotted matching for re-slotted slot elements
Per "find flattened slotables", a <slot> whose root is a shadow root is
recursed through, not appended to the result. ::slotted() in an outer
shadow must therefore not match such an intermediate slot.

Fixes the gallery on Reddit comment pages: a re-slotted <slot> was
picking up `::slotted(:not([slot])) { display: grid }` from the inner
shadow, which made the <ul> size to its content rather than the flex
container, leaving the carousel's "next" button with a 0px translate.
2026-04-29 04:54:11 +02:00
Andreas Kling
21a1f40691 Tests: Cover ::slotted matching against re-slotted slot elements
::slotted() rules from an outer shadow currently leak onto intermediate
<slot> elements re-slotted into that shadow. The expected output records
the buggy state; the next commit fixes it.
2026-04-29 04:54:11 +02:00
Andreas Kling
e9ed125bbe LibWeb: Don't fan sibling structural invalidation into descendants
When an insert or remove flips a sibling's match for :first-child,
:last-child, :nth-child, :nth-of-type, or a + / ~ combinator, only the
sibling's own match changes. Descendants only need rechecking when a
selector ties them to the sibling's position.

invalidate_structurally_affected_siblings used to mark the affected
sibling's entire subtree, so a sibling with N descendants turned a
match-state flip into N+1 element recomputes. Reuse the per-element
flags introduced for the same-parent-move optimization: when neither
affected_by_structural_pseudo_class_in_non_subject_position nor
affected_by_sibling_combinator_in_non_subject_position is set, mark
only the sibling root, and let the existing inherited-update path
reach descendants if the sibling's own style changes.

Rebaseline the impacted tests. styleInvalidations gains one per
affected sibling because set_needs_style_update increments the counter
(entire-subtree marks did not), but elementStyleRecomputations falls
accordingly.
2026-04-29 04:53:14 +02:00
Andreas Kling
5e2474e8c9 Tests: Cover sibling structural invalidation fanout
Pin the recompute counters (and shadow-DOM correctness) when a
structural mutation flips a sibling's match for :first-child,
:last-child, :nth-child, :nth-of-type, or a + / ~ combinator. The
affected sibling's entire subtree is marked dirty today, so an 8-leaf
sibling produces elementStyleRecomputations=11. Shadow-host cases pin
that inherited-style propagation reaches light DOM, light-DOM-hosted
shadow roots, and nested shadow DOM under the affected sibling.

The next commit replaces the entire-subtree mark with a root-only mark
when no descendant selector ties matching to the sibling's position,
and rebaselines the counter expectations.
2026-04-29 04:53:14 +02:00
Aliaksandr Kalenik
4762c4fa5c LibWeb: Add incremental HTML parsing
Introduce IncrementalDocumentParser, which streams the response body
through a TextCodec::StreamingDecoder into the HTMLTokenizer one chunk
at a time. The tokenizer pauses when it runs out of input and resumes
once the next chunk is appended; when the body closes we close the
tokenizer's input stream so it can finish the parse.

DocumentLoading routes HTML responses through the new parser instead of
buffering the full body before handing it to HTMLParser.
2026-04-29 04:12:44 +02:00
Aliaksandr Kalenik
f499edefae LibWeb: Track whether HTMLParser is script-created
Add a ScriptCreatedParser flag plumbed through HTMLParser's constructor
and create_for_scripting(). Only document.open()'s parser sets it to
Yes. Document::close() step 3 now checks is_script_created() so it
correctly skips parsers that weren't created via document.open(),
matching the spec.

Previously the check was just `if (!m_parser)`, which incorrectly let
document.close() insert an EOF into a network-driven parser. The bug
was mostly latent because the network parser used to finish quickly,
but it matters once the network parser stays alive for the duration of
a streamed parse.
2026-04-29 04:12:44 +02:00
Andreas Kling
d48db4e881 LibWeb: Implement TextDecoderStream
This is the streaming counterpart to TextDecoder, used by sites that
process responses with `pipeThrough(new TextDecoderStream())` or that
otherwise consume a Response body as decoded text.

The transform algorithm appends each chunk's bytes to the I/O queue
and decodes them via the underlying LibTextCodec decoder, holding back
any trailing partial UTF-8 sequence so chunk boundaries don't produce
spurious replacement characters. The flush algorithm emits a single
replacement character if a partial sequence was still pending. For
non-UTF-8 encodings the underlying decoders are stateless across
calls, so each chunk is decoded in full with nothing carried over.
2026-04-28 19:17:09 +02:00
Andreas Kling
f10f651e49 LibWeb: Don't treat first media-query evaluation as a flip
CSSStyleSheet::evaluate_media_queries previously flagged "no recorded
result yet" as a match-state change, so every freshly-loaded sheet
fired MediaQueryChangedMatchState on the first pass through
Document::evaluate_media_rules. For sheets added through
adoptedStyleSheets that piled an extra full-document style invalidation
on top of the AdoptedStyleSheetsList one, recomputing every element a
second time for nothing.

Drop the !has_value() leg so the very first evaluation establishes the
baseline silently. The sheet's rules already entered the cascade through
StyleSheetListAddSheet, AdoptedStyleSheetsList, or invalidate_owners,
each of which performs its own targeted invalidation.

Two callers relied on the implicit "first eval forces a refresh"
behavior to handle freshly-mutated state:

- invalidate_owners resets m_did_match, then leans on the next eval to
  repopulate it. With the new semantics it must also re-evaluate the
  sheet eagerly so MediaList::matches() and inner @media state are
  fresh before the next rule cache build reads them.
- The adoptedStyleSheets on_set callback didn't evaluate at all,
  relying on Document::evaluate_media_rules to populate
  MediaList::m_matches. That worked accidentally because the false
  flip retriggered invalidate_rule_cache after the matches had been
  populated. Mirror StyleSheetList::add_sheet by evaluating the sheet
  at adopt time so the rule cache build sees the correct match state
  even if it runs first (e.g. via a :has() invalidation pass).
2026-04-28 19:06:29 +02:00
Andreas Kling
3b098c08ea Tests: Cover media-query first-evaluation invalidation behavior
Add focused coverage for the counters produced when a stylesheet's media
queries are evaluated for the first time. Two paths are exercised:

- document.adoptedStyleSheets, where the sheet is added without an
  eager evaluate_media_queries call. The next update_style currently
  flips m_did_match from "no recorded result" to a recorded one and
  fires MediaQueryChangedMatchState on top of the AdoptedStyleSheetsList
  invalidation, so fullStyleInvalidations bumps twice.

- <style> element add via StyleSheetList::add_sheet, which evaluates
  media queries eagerly and absorbs the first-eval flip. These tests
  pin that behavior so future changes don't regress it.

Cases vary the outer MediaList (empty, all, screen, print, width-based
matching and not), the rule selector (matching, non-matching), inner
@media rules (matching, non-matching, mixed, nested), and @supports
wrapping inner @media. The expectations record behavior before the
optimization, so subsequent commits can show counter progressions.
2026-04-28 19:06:29 +02:00
Andreas Kling
ce5d0bdfc7 LibWeb: Narrow :has descendant invalidation fanout
When a :has() mutation is known to come from a specific subtree, use
that subtree as the mutation root while walking observed ancestors.

Before dirtying an anchor and its non-subject descendants, check whether
any cached :has() rule for that anchor can observe the changed subtree.
This keeps unrelated descendant mutations from invalidating every rule
that merely contains :has().
2026-04-28 15:34:49 +02:00
Andreas Kling
356a369aa6 LibWeb: Avoid descendant recomputes for same-parent moves
Moving a node within the same parent changes sibling and positional
relationships, but it does not make every descendant of the moved node
need a fresh computed style. Handle this as a structural mutation at
the old and new sibling edges and dirty only the moved root and the
affected ancestors, instead of marking the entire moved subtree.

Factor the existing previous- and next-sibling structural invalidation
out of Node::invalidate_style() into invalidate_structurally_affected_-
siblings(), and pull the ancestor child-needs-style-update walk into
mark_ancestors_as_having_child_needing_style_update(). The new
invalidate_style_after_same_parent_move() reuses both helpers.

Whether the moved root itself needs its own style recomputed depends
on whether any selector matched against it (or against a descendant)
relied on its position in the sibling list. Track that via two new
sticky bits on Element, set during selector matching:

  - m_affected_by_structural_pseudo_class_in_non_subject_position
  - m_affected_by_sibling_combinator_in_non_subject_position

Both are write-once (sticky) because matching descendants can set them
while we're not currently re-matching this element's own selectors;
keeping them set is conservative and avoids stale descendant style.

When neither bit is set and the element only carries subject-position
positional/sibling/has() involvement, we just dirty the root and skip
its descendants.

Rebaseline same-parent-move-root-only and the structural-feature filter
counters to reflect the new path. Matching behavior is unchanged.
2026-04-28 15:34:49 +02:00
Andreas Kling
11d9d4a7a5 LibWeb: Match attribute invalidation by presence and recent removal
Element::includes_properties_from_invalidation_set() previously
short-circuited and returned true for the id and class attributes,
because the parsed id and class-name state was treated as the source of
truth and the attribute presence check could disagree with it. That
shortcut over-invalidated for any [class] or [id] selector even when
neither attribute had ever been touched on the element.

Drop the special case and answer attribute presence queries the same
way for every attribute: the element either currently has the attribute,
or has had it removed earlier in this style update batch.

To handle the just-removed case, track the set of attribute names that
were removed since the last invalidation pass on a new per-Element
Vector<FlyString, 1> m_removed_attributes_for_style_invalidation. The
StyleInvalidator clears the vector for each element it visits during
perform_pending_style_invalidations, so it only ever holds names from
the current batch.

Rebaseline the affected attribute-presence tests; counters drop because
elements that never had id/class no longer match [class] / [id]
invalidations.
2026-04-28 15:34:49 +02:00
Andreas Kling
9fae2bcff9 LibWeb: Avoid unrelated structural :has invalidation
Track the simple selector features that appear inside :has() arguments
on each StyleScope, then consult that metadata before scheduling an
ancestor walk for a structural mutation. If the mutated subtree has no
tag, id, class, attribute, or pseudo-class feature that any cached
:has() argument cares about, skip the walk entirely.

Stay conservative for featureless-sensitive arguments such as :has(*),
:has(:not(...)), :has(:empty), and child-index pseudos: an unfeatured
node can still start or stop matching there. Track that case via a new
has_selectors_sensitive_to_featureless_subtree_changes flag on
StyleInvalidationData and fall back to the old conservative walk.

Stay conservative for pseudo-classes the subtree filter cannot probe
(:focus, :hover, validation pseudos). Move :default out of the set of
trackable feature pseudo-classes for the same reason; it now triggers
the conservative walk where it previously recorded metadata.

Tag and attribute names are stored lowercased, so for non-HTML elements
(SVG, MathML) treat lowercased matches as scheduling hints only; the
actual :has() match still goes through case-sensitive selector matching.

Test counter expectations are rebaselined to reflect the skipped walks
and reduced recomputations. Matching behavior is unchanged.
2026-04-28 15:34:49 +02:00
Andreas Kling
bacfbb4d97 Tests: Cover structural style invalidation edge cases
Add focused coverage for style invalidation matrix behavior.
Cover concrete invalidation metadata collection, :defined triggers,
same-parent moves, :has() mutation roots, and feature filtering.

Also cover dynamic and unprobeable pseudo-classes inside :has().
These include mixed-metadata cases where another :has() selector in the
same scope records concrete metadata.

Add SVG and MathML case-sensitive name coverage for :has() filters.

The expectations record behavior before the optimization, so the next
commit can show counter progressions clearly.
2026-04-28 15:34:49 +02:00
Andreas Kling
caad205467 LibWeb: Share singleton constructed stylesheet rule caches
Share the style cache for shadow roots whose only active author sheet is
the same constructed stylesheet. Matching already carries the effective
shadow root separately, so the cache can be reused while selectors such
as :host and ::slotted() still evaluate against each consuming shadow
root.

Keep the optimization conservative by falling back to the existing
per-scope cache whenever the shadow root has multiple active sheets, a
non-constructed sheet, or a page user stylesheet. Drop the shared cache
when the stylesheet rules or media query match state change.

Add coverage for two shadow roots adopting the same constructed sheet,
including :host, ::slotted(), and replaceSync() invalidation.
2026-04-28 13:07:52 +02:00
Andreas Kling
b8e5f07eed LibWeb: Keep rule caches for CSSOM declaration changes
CSSOM declaration mutations on style rules and nested declarations
do not change selector buckets, layer ordering, or keyframe sets stored
in rule caches. Keep those caches valid and only invalidate affected
styles, while leaving keyframe declaration mutations on the existing
cache-invalidating path.

Add coverage showing a CSSOM style declaration mutation is observed
through an already-built rule cache.
2026-04-28 09:49:50 +02:00
Andreas Kling
118802b3f0 LibWeb: Scope media rule cache invalidation
Invalidate only the style scope whose media rules changed instead
of throwing away every shadow root rule cache whenever any active
stylesheet changes media query match state. Shadow-root stylesheet
changes still dirty the host side because :host and ::slotted
selectors can affect nodes outside the shadow tree.

When scoped invalidation leaves dirty descendants in a shadow root,
preserve the host ancestor chain so the document style update walk
reaches them before forced layout.

Add coverage that a matching media rule introduced in one shadow tree
does not broadly invalidate a page full of unrelated shadow roots,
and that a dirty shadow root is updated before layout is forced.
2026-04-28 09:49:50 +02:00
Aliaksandr Kalenik
09d0278561 LibWeb/CSS: Don't track @font-face rules as critical subresources
CSSFontFaceRule inherited from CSSStyleSheet::Subresource, which made
each face a critical subresource of its parent stylesheet. New
subresources start in the Unloaded state, and the stylesheet's
loading_state() treats Unloaded as Loading.

When a @font-face declares a unicode-range and no codepoint in that
range is ever rendered, FontComputer only registers the face for
matching and never calls FontFace::load() on it. The face stays
Unloaded, so the parent stylesheet stays stuck reporting Loading,
which keeps HTMLLinkElement's load-event delayer alive and prevents
the document load event from firing. HTMLParserEndState then times
out in phase 2 (WaitingForLoadEventDelay) after 15 seconds.

Decouple @font-face from the stylesheet's loading state. Font loading
remains tracked by FontFaceSet, which is the correct place.
2026-04-28 00:15:30 +02:00
Aliaksandr Kalenik
b6ffd51d1c LibWeb: Pause tokenizer at a CR right before the insertion point
HTML newline normalization collapses CRLF into a single LF, so
next_code_point() needs one code point of lookahead at a CR to decide
whether the CR stands alone or is the first half of a CRLF pair. When
the tokenizer is paused at the insertion point and the next code point
to consume is a CR sitting one position before it, that lookahead has
not been written yet.

Previously the tokenizer consumed the CR and emitted it as LF, so a
subsequent document.write() that began with LF surfaced as a second
LF instead of being absorbed into the original CRLF pair.

Stop one code point earlier in this case and wait for the next write
to arrive. This makes four html5lib write_single WPT tests pass.
2026-04-27 21:44:56 +02:00
Luke Wilde
b3ecb3c6da LibWeb/CSS: Implement stroke-dasharray interpolation 2026-04-27 19:06:01 +02:00
Aliaksandr Kalenik
737691c43a LibWeb: Keep worker startup reachable until script load completes
Fixes flakiness in worker tests that create a Worker or SharedWorker
with a missing script URL and only attach an error handler to it.
Once the test callback returns, nothing keeps the worker rooted from
JavaScript. If GC ran before the WebWorker process reported the
script fetch failure, the Worker/WorkerAgentParent cycle could be
collected and the error event never delivered, leaving the test hung
until timeout.

Hold startup-pending WorkerAgentParents from the outside
EnvironmentSettingsObject and release that edge once the script load
succeeds, fails, or the worker closes. The worker now survives long
enough to deliver its first script-load result.
2026-04-27 18:02:49 +02:00
Aliaksandr Kalenik
c44c36416e LibWeb: Preserve old insertion points across reentrant scripts
The HTML parser's script end tag algorithms save the current insertion
point in an "old insertion point" local before executing a script, then
restore that local after script execution. Ladybird modeled that local
as a single tokenizer field, so nested script execution via
document.write() could overwrite the outer script's saved value.

Keep a stack of old insertion points instead, and adjust saved offsets
when document.write() inserts new input before them. This keeps the
normal script and SVG script paths aligned with the spec text while
leaving the parser-blocking script resume path to set the insertion
point to undefined again.
2026-04-27 18:02:19 +02:00
Andreas Kling
c9fa971905 LibWeb: Don't strand a fresh navigation behind a sync history step
When a click handler calls history.replaceState and the link's
cross-document activation behavior runs in the same task, the queued
sync step runs apply-the-history-step on the navigable mid-navigation,
transitioning its ongoing navigation through "traversal" and back to
null. That aborts the link's navigate event and bails out its deferred
work, leaving the link nav abandoned.

Skip this transient when the navigable already has a fresh ongoing
navigation. No major engine reproduces the race; long term, sync
same-document nav should bypass the traversal queue entirely (matching
Chromium).

This solves the long-standing issue where clicking on a box of tea
on https://twinings.co.uk/ would freeze the browser. :^)
2026-04-27 17:24:13 +02:00
Andreas Kling
37130e7300 LibWeb: Don't apply connect-src to top-level navigation
Top-level navigation requests use the document fetch destination. CSP's
effective directive algorithm does not list document as a handled fetch
request destination, but our fallback path treated it like an unknown
fetch destination and applied connect-src.

Return no effective fetch directive for document destinations. This lets
top-level navigation use the CSP navigation checks instead. Keep nested
navigation on the existing frame and iframe path, since HTML rewrites
such requests to the container local name when a navigable has a
container.

This makes https://reddit.com/ load instead of redirecting to a blocked
challenge reponse URL.
2026-04-27 10:30:57 +02:00
Aliaksandr Kalenik
70ac025eff LibWeb: Implement the speculative HTML parser
When the HTML parser blocks on a synchronous external script, run a
separate tokenizer over the unparsed input and issue speculative fetches
for the resources it finds (script src, link rel=stylesheet|preload, img
src), with <base href> tracking and template/foreign-content skipping.

Also fills in the previously-stubbed "consume a preloaded resource"
algorithm and the document's "map of preloaded resources", so that
<link rel="preload"> followed by a matching consumer deduplicates to
a single fetch.
2026-04-26 18:48:29 +02:00
Shannon Booth
7beac55210 LibWeb/Bindings: Pass exact typed array types through WebIDL bindings
Generate exact JS buffer types for exact IDL buffer arguments instead
of widening them to BufferSource or ArrayBufferView.

This fixes cases like TextEncoder.encodeInto(), whose IDL requires
a Uint8Array destination. Previously the generated binding accepted
any BufferSource, so DataView, other typed arrays, and
ArrayBuffer-backed values were let through. With exact conversion,
those are rejected at the binding layer as expected.
2026-04-26 17:50:58 +02:00
Aliaksandr Kalenik
54244f9e4a LibWeb: Re-evaluate picture source set on source mutations
The img inside a <picture> has to re-run "update the image data" when
nearby <source> elements change, so script-driven swaps of srcset (and
the other dimension/media attributes) actually take effect.

Per the HTML spec, the relevant mutations for an img element include:
"The element's parent is a picture element and a source element that
 is a previous sibling has its srcset, sizes, media, type, width or
 height attributes set, changed, or removed."

The same applies to source insertion, moving, and removal.

Fixes image loading on https://www.apple.com/mac/
2026-04-26 17:49:19 +02:00
Tim Ledbetter
6bb037aec7 LibWeb: Skip backward sibling invalidation when no child needs it
We now track when a parent has a child affected by a backward structural
pseudo-class. These are selectors whose match result for an element can
depend on siblings after that element, such as `:last-child`,
`:only-child`, `:last-of-type`, `:only-of-type`, `:nth-last-child`, and
`:nth-last-of-type`.

When inserting or removing a node, previous siblings only need style
invalidation if one of them was matched against such a selector. Use the
parent-level flag to skip the previous-sibling walk when no child under
that parent can be affected.

This saves a lot of invalidation work on sites that insert a lot of
nodes into the DOM via JS.
2026-04-26 16:14:43 +02:00
Aliaksandr Kalenik
b1ccab81ad LibWeb: Replace spin_until in HTMLParser::handle_text with async resume
Spinning a nested event loop to wait for a parser-blocking script blocks
the calling thread, can deadlock, and creates reentrancy hazards. Switch
to an event-driven pause/resume model, mirroring the prior
HTMLParserEndState refactor (df96b69e7a).

Three WPT document.write tests flip from Fail to Pass and are
rebaselined: all write an external script via document.write() followed
by inline content. With spin_until, control did not return to the caller
of document.write() between writing the script and observing its effects
so the test's order assertions saw a different sequence than the spec
mandates.
2026-04-26 10:44:45 +02:00
Andreas Kling
13c81b4636 Tests: Split CI-timing-out structural-* stress tests by mode
Several structural-* stress tests iterated over enough scopes,
mutation cases, and modes to time out on the slower CI runners.
Split each timing-out test along mode boundaries so no single file
runs more than a quarter of the original work, mirroring the
warm/cold/pseudo/detached split shape across the suite:

* structural-descendant-stress: split scope-wise into doc/shadow,
  slot, and part files (already there from the earlier commit),
  then mode-wise into attached/detached and warm/pseudo halves.
* structural-descendant-state-stress: split mode-wise into the
  same four-quadrant attached/detached x warm/pseudo layout.
* structural-descendant-direction-language-stress: ditto.
* structural-child-part-shadow-stress and structural-child-slot-
  stress: same four-quadrant split (these already had no -cold
  detached modes so the detached half is smaller).

Every resulting file is well under 700 lines of expected output,
keeping the per-test runtime comfortably below the CI timeout.
2026-04-26 10:40:58 +02:00
Andreas Kling
89c0b124e4 Tests: Report invalidation counters without PASS/FAIL judgment
The two no-op-invalidation-counters tests asserted an exact target
count and printed FAIL when the live count didn't match. The numbers
they record document how much invalidation work happens for a given
mutation, and we expect those numbers to drop as the invalidation
logic improves rather than to be a fixed target right now. Keep the
tests as a moving record of current counts instead of as pass/fail
gates, so churn shows up in expected file diffs without making the
tests look broken.
2026-04-26 10:40:58 +02:00
Andreas Kling
2ff002b0bb LibWeb: Invalidate dir=auto descendant styles on text mutation
When the text content under a dir=auto ancestor changes, the
ancestor's effective directionality can flip and inherits down to
every descendant. The previous commit only marked the ancestor
itself dirty, so descendants whose :dir()-dependent style depended
on the ancestor's direction were left rendered against the old
value.

Walk the ancestor's shadow-including inclusive subtree and mark
every element for style update so the new direction propagates
through the cascade.
2026-04-26 10:40:58 +02:00
Andreas Kling
5ff832081d LibWeb: Make :dir() and :lang() invalidation cross shadow boundaries
Three related fixes around how dir and lang attribute changes flow
through shadow boundaries:

* Element::lang() looked up the host through parent_element(), which
  is null for elements directly inside a shadow root. The shadow
  root -> host fallback in step 3 therefore never fired and
  shadow-tree elements never inherited their host document's
  language. Walk through parent() instead so the shadow root case is
  detected.

* The dir/lang attribute_changed handlers each marked descendants
  for style update, but neither one descended through shadow
  boundaries via for_each_shadow_including_inclusive_descendant the
  same way (and only one cleared the cached language). Merge both
  handlers so dir and lang share the same shadow-including descendant
  walk.

* :has(:dir(...)) and :has(:lang(...)) on ancestors aren't keyed on
  any property the regular invalidation plan tracks, so the :has()
  ancestor walk has to be scheduled explicitly. Schedule it on the
  document scope and on every shadow scope reachable from this
  element via parent_or_shadow_host.
2026-04-26 10:40:58 +02:00
Andreas Kling
43f2021484 LibWeb: Recompute shadow part style when exportparts changes
When a shadow host's exportparts attribute changes, elements with
part tokens inside its shadow tree may newly become or stop being
targets of ::part() rules in the outer scope. Mark every such
descendant for style update so the new exposure is reflected.
2026-04-26 10:40:58 +02:00
Andreas Kling
48c4a254a6 LibWeb: Recompute style when an element's part attribute changes
::part(...) rules in the outer scope target shadow descendants by
their part-name tokens. When an element's part attribute changed,
m_parts and the IDL part list were updated but the element itself
was never marked for style update, so previously-matched ::part()
rules kept applying or new matches stayed unevaluated.
2026-04-26 10:40:58 +02:00
Andreas Kling
996080eeea LibWeb: Recompute style for slottables when slot assignment changes
assign_slottables only updated each slottable's m_assigned_slot
pointer. The slottable's match against ::slotted(...) rules is
determined by whether it's currently assigned to a slot, so gaining
or losing an assignment must trigger a fresh style computation.
Without that, mutations like changing a slot's name or a slottable's
slot attribute left the slottable rendered against stale ::slotted()
rules.

Mark the slottable element for style update both when it loses an
assignment and when it gains one.
2026-04-26 10:40:58 +02:00
Andreas Kling
09c0caf986 LibWeb: Build invalidation sets from ::slotted() simple selectors
build_invalidation_sets_for_simple_selector ignored ::slotted()
pseudo-element selectors entirely. As a result, the invalidation
plans built for a shadow scope didn't list class, attribute, or
pseudo-class properties referenced inside ::slotted(), so changing
those properties on a slottable couldn't enqueue an invalidation
plan for the rule.

Recurse into ::slotted()'s compound argument and feed each simple
selector through the same invalidation-set builder.
2026-04-26 10:40:58 +02:00
Andreas Kling
7d6f77bdf3 LibWeb: Recompute style for descendants when dir/lang changes
The dir and lang attributes inherit, so descendants' :dir() and
:lang() matches and any direction-dependent layout/text depend on
ancestor values. Element::attribute_changed updated the m_dir field
and refreshed cached language values, but never marked descendants
for style recomputation, so existing styled descendants kept their
old :dir()/:lang() results.

Mark every descendant for style update when dir or lang changes.
2026-04-26 10:40:58 +02:00
Andreas Kling
3cbe6bc89a LibWeb: Schedule attribute :has() walks on outer scopes too
The property-based Node::invalidate_style only added inner shadow
scopes (the element's own shadow and enclosing hosts' shadows). When
the mutated element lived inside a shadow tree, mutations could not
reach ::part(...:has(...)) rules in the outer document or outer
shadow root because those scopes were never collected.

Walk parent_or_shadow_host and add the document scope (or each
crossed shadow root scope) when the ancestor's root differs from
this element's root, alongside the inner shadow scopes already
collected.
2026-04-26 10:40:58 +02:00
Andreas Kling
fead65d9b2 LibWeb: Recurse into ::slotted() args when collecting :has() metadata
collect_properties_used_in_has only inspected pseudo-class argument
selectors. With ::slotted(.x:has(.descendant)) rules, the property
references inside the :has() argument were therefore never recorded
as :has()-affecting, so attribute and state changes on a slottable's
descendants couldn't enqueue an invalidation plan that covered the
::slotted() rule.

Recurse into the ::slotted() compound argument so the :has() metadata
maps include the properties used inside.
2026-04-26 10:40:58 +02:00
Andreas Kling
85a9239faf LibWeb: Make :has() invalidation reach ::slotted/:host rules
Two related fixes that together let :host(...:has(...)) and
::slotted(.x:has(...)) rules re-evaluate when their light-DOM input
changes:

* StyleScope::collect_selector_insights only recursed into
  pseudo-class argument selectors. The :has() inside a
  ::slotted(...) compound argument was therefore invisible to a
  shadow scope's insights, so may_have_has_selectors() reported
  false and Node::invalidate_style skipped the :has() walk for that
  scope. Walk the ::slotted() argument selector through the same
  insight collection.

* Element::invalidate_style_if_affected_by_has only set
  needs_style_update for elements in subject position. Inside
  ::slotted(.x:has(...)) and :host(...:has(...)) the rule's
  selector subject is the slot or host, but the styled element is
  this element; when the :has() result flips, this element's own
  computed style must be recomputed. Mark it dirty in the
  non-subject branch as well.
2026-04-26 10:40:58 +02:00
Andreas Kling
60113a3be2 LibWeb: Schedule attribute :has() walks on enclosing shadow scopes
The property-based Node::invalidate_style only collected the element's
own shadow scope (when the element itself was a shadow host), not the
shadow scopes of enclosing hosts. So a class or attribute change on a
light-DOM descendant of a shadow host could not flip
:host(...:has(.descendant)) rules in that host's shadow root.

Walk parent_or_shadow_host from the mutated element and add every
enclosing shadow root's StyleScope to the set we check :has()
metadata against, schedule walks on, and run invalidation plans
through.
2026-04-26 10:40:58 +02:00
Andreas Kling
bc4ff9038a LibWeb: Schedule :has() walks on enclosing shadow scopes for inserts
Light-DOM mutations under a shadow host can flip
:host(...:has(...)) rules that live entirely in the host's shadow
root. Node::invalidate_style only scheduled the :has() walk on the
root scope of the mutated node, so those shadow-side rules never
re-evaluated when their light-DOM input changed.

Walk up parent_or_shadow_host from the mutation parent and schedule
the :has() walk on every enclosing shadow root's StyleScope that has
:has() rules.
2026-04-26 10:40:58 +02:00
Andreas Kling
b8573eba6f LibWeb: Use document scope for invalidation in user-agent shadow trees
SVG <use> shadow trees opt into the host document's stylesheets via
ShadowRoot::uses_document_style_sheets. Style computation already
honors this in AbstractElement::style_scope(), but
Node::invalidate_style picked the shadow root's own StyleScope based
solely on root().is_shadow_root(), so mutations inside such trees
never consulted document :has() metadata or invalidation plans.

Match the existing style-computation scope selection so invalidation
runs against the document StyleScope when the root is a shadow root
that uses document stylesheets.
2026-04-26 10:40:58 +02:00
Andreas Kling
5461b44d04 LibWeb: Invalidate shadow subtree on host attribute change
Rules in a shadow root that match :host(...) can apply different style
to shadow descendants when the host's attributes or classes change.
The host's own invalidation flow doesn't reach into the shadow tree,
so descendants kept their cascaded values from the previous host
state.

When the host's stylesheets contain :host()-style rules that may match
the shadow host, mark the entire shadow subtree dirty so descendant
style is recomputed against the new host state.
2026-04-26 10:40:58 +02:00