Closed Bug 2090823 Opened 19 days ago Closed 19 days ago

`:has(div + .a)` does not update when a `div` is inserted or removed

Categories

(Core :: CSS Parsing and Computation, defect)

defect

Tracking

()

RESOLVED FIXED
162 Branch
Tracking Status
firefox162 --- fixed

People

(Reporter: jwatt, Assigned: jwatt)

References

(Blocks 1 open bug)

Details

Attachments

(1 external file)

In something like:

<style>.x:has(div + .a) { color: red }</style>
<div class=x><span class=a>a</span></div>
<script>
  const x = document.querySelector(".x");
  getComputedStyle(x).color;  // flush style first
  x.prepend(document.createElement("div"));
</script>

the text should turn red, but stays black, even though x.matches(".x:has(div + .a)") returns true. Removing the div again also fails to update.

The same happens with :has(> div + .a), :has(div ~ .a), :has(div + .a + .b), and :has(:not(.x) + .a) when the sibling has class x.

These work: :has(.d + .a) with an inserted <div class=d>, and :has(.a + div) with the div appended after the span.

The problem is that RelativeSelectorDependencyCollector creates compound_state_attributes once per :has() argument and only resets compound_state between compounds. Compounds are visited right to left, so once one compound sets added_entry, every compound to its left skips add_non_unique_info. A compound with no class, id, attribute or pseudo-class of its own (other than :not(), :is() and :where()) then gets no type_to_selector or any_to_selector entry, and so inserting or removing an element that only it matches fails to trigger invalidation. SelectorDependencyCollector resets both pieces of state at each compound.

The selector in bug 2088020 is affected too (its :not(.a .b) compound), but its testcase fails for a different reason.

RelativeSelectorDependencyCollector::visit_whole_selector only reset
compound_state at the start of each compound, but not
compound_state_attributes (which is created once per relative selector).
Compounds are visited right to left, so once any compound sets added_entry,
every compound to its left skips add_non_unique_info, and a compound with
nothing of its own to key on (such as a class, id or attribute) gets no
type_to_selector or any_to_selector entry. Inserting or removing an element
that was only matched by such a compound selector (e.g. the div in
:has(div + .a)) would then fail to trigger invalidation.
(SelectorDependencyCollector already resets both at each compound.)

Pushed by jwatt@jwatt.org: https://github.com/mozilla-firefox/firefox/commit/82a62b7f0ccf https://hg.mozilla.org/integration/autoland/rev/dfa93444d497 Reset the per-compound relative selector state between compounds when building :has() invalidation dependencies. r=firefox-style-system-reviewers,emilio

Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/64945 for changes under testing/web-platform/tests

Status: NEW → RESOLVED
Closed: 19 days ago
Resolution: --- → FIXED
Target Milestone: --- → 162 Branch

Upstream PR merged by moz-wptsync-bot

QA Whiteboard: [qa-triage-done-c163/b162]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: