:text selector to match elements whose text node is non-empty

The problem I’m trying to solve is adding a text backplate (a background color) to elements that have text while not applying it unnecessarily to elements without text. Doing this would ensure a sufficient text contrast for elements that overlay images.

I’d love to be able to write

:text {
  background-color: var(--backplate-color);
}

Could you work with the :empty selector?

The :empty selector takes element children into account, not just text node children. So writing :not(:empty) would match every node that has children even if those children are eventually empty. (For example, it would match both the htmland body elements.)

I think using the :has selector could help determine if a node is a leaf node, meaning it doesn’t have element children.

:not(:has(> *)):not(:empty) should match leaf nodes that have text.

Not just text, any content (incl. whitespace), but not CSS text (::before).