Re: [csswg-drafts] How to handle addEventListener on `CSSPseudoElement`? (#12163)

# Proposed rough draft changes to the DOM and the CSS specs to add `pseudoTarget` and `currentPseudoTarget` to the Event interface.

## CSS spec.

### Dispatching an event on a pseudo-element.

- First either (a) make internally a `target` a CSSPseudoElement or (b) add an argument `pseudoTarget` to the dispatching struct.

- Then when an event is dispatched on `target` the user agent must run the following algorithm to determine the event's `pseudoTarget` and `currentPseudoTarget`:

  - Let `event` be the Event object being dispatched.

  - Let `event.pseudoElement` be either the `target` if we go with (a) or `pseudoElement` parameter if we go with (b).

  - If we go with (a):
    
    - Let `parent` be the ultimate originating element of the `pseudoElement`.

    - Set `target` to be `parent`.

  - Set `currentPseudoTarget` to null.

### Adding pseudo-elements to the event path algorithm:

- Let `pseudoElements` be a list of pseudo-element ancestors starting with the `pseudoTarget` and going up to the outermost pseudo-element attached to the `target`.

- Insert the `pseudoElements` list into the event's path right before the `target` or maybe as a separate parameter if it’s a web compat problem?

### The algorithm to update `currentPseudoTarget` and `currentTarget` during event capture:

- If invoked on `CSSPseudoElement`, set `currentPseudoTarget` to that `CSSPseudoElement`.

- Otherwise set `currentTarget` to the currently invoked target and set `currentPseudoTarget` to null.

- Maybe clear pseudo-elements from the event path, once we processed them there.

## DOM spec.

- In the event dispatching algorithm a new step should be added to call the CSS spec algorithm. (after the `target` is set, probably).

- The event path creation algorithm should call the CSS spec algorithm for adding pseudo-elements to the event path.

- Call current targets setting algorithm during event capture and bubbling.

The overall idea is to add the new functionality for CSSPseudoElement in events, while masking the changes for the code which doesn't know anything about CSSPseudoElement in events.

-- 
GitHub Notification of comment by danielsakhapov
Please view or discuss this issue at https://github.com/w3c/csswg-drafts/issues/12163#issuecomment-3052929530 using your GitHub account


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Wednesday, 9 July 2025 14:41:23 UTC