Safari :has(:empty) bug

This section should have red bg and white text if the paragraph above it is empty. If the paragraph has content it should not have any special styles.

Safari, Mac and iOS, only.

I probably got some of the details wrong, but I'm fairly certain of the gist. I commented on the relevant Bugzilla issue.
Update: Fixed.

There should not be any rule such as x:empty + y, without a :has, that holds CSS declarations and selects the same element y as in the code examples below. If there is, all styles work as intended.

If there isn't...

However,

So far x's emptiness is only evaluated once. Whatever happens to x later, y doesn't change styles

If y has a display: none

As far as I can see :has(x:empty) is checked for emptiness only twice:

Most tests done on Mac 26, but iOS 18 also has this bug, and I suspect 26 has, as well.

Solving the bug

If you just want to solve the bug in your production code, try writing a selector without :has(:empty).

If you can't, add a non-:has selector such as the one below that just defines a variable you never use. It's OK to select way more elements than your :has(:empty) one does, as long as your element y is also selected.

:empty ~ * {
	--test: 'nonsense';
}

If you're OK with emptiness being evaluated only once, fire your script onDOMContentLoaded, and not onload.