Description
Full context: https://github.com/danielsakhapov/CSSPseudoElementDoc?tab=readme-ov-file#21-existence-confusion
Description:
CSSPseudoElement object is returned from pseudo(type) if type
is parsed correctly, whether the corresponding pseudo element is rendered or not doesn't matter.
Developers might need a clear way to distinguish if the pseudo element is being "live" or rendered.
Possible Solutions:
- A dedicated
exists
property orisGenerated()
method:pseudoElement.exists
(nullable boolean, read-only)- Pros: Explicit and clear, allows to specify the return value on a per pseudo element basis.
- Cons: Adds new API surface. Defining "exists" precisely across all pseudo element types can be tricky (e.g., does
::selection
"exist" if nothing is selected but it could be styled? - potentially returnnull
here).
getComputedStyle()
inspection:- Check
getComputedStyle(pseudoElement).display !== 'none'
. For::before
/::after
, also checkgetComputedStyle(pseudoElement).content !== 'none'
. - Pros: Uses existing mechanisms.
- Cons: Indirect; might not cover all cases (e.g., a pseudo element that exists but has 0x0 dimensions and no visual content). For highlight pseudos, "display" might not be the relevant property. Sometimes depends on the originating element style.
- Check
- Layout Information:
- Methods like a hypothetical
pseudoElement.getBoundingClientRects()
returning an empty list or specific values could indicate non-existence or non-rendering. - Pros: Provides practical information.
- Cons: Might be expensive to compute just for an existence check.
- Methods like a hypothetical
Recommendation:
Go with exists
, as it gives more control for each pseudo element while unifying an interface to get the needed information. As new pseudo elements are added to be returned from pseudo()
, the spec will be updated to define the meaning of exists
for them.
Otherwise: Relying on getComputedStyle()
initially seems pragmatic, given the limited list of pseudos supported currently. For highlight pseudos, "existence" might mean "has associated ranges/segments."