Skip to content

Add a property to the CSSPseudoElement IDL interface to determine if a pseudo element "exists" #12158

@danielsakhapov

Description

@danielsakhapov

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 or isGenerated() 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 return null here).
  • getComputedStyle() inspection:
    • Check getComputedStyle(pseudoElement).display !== 'none'. For ::before/::after, also check getComputedStyle(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.
  • 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.

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."

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions