Skip to content

Add reference target to shadow root#1353

Open
dandclark wants to merge 11 commits into
whatwg:mainfrom
dandclark:dandclark/reference-target
Open

Add reference target to shadow root#1353
dandclark wants to merge 11 commits into
whatwg:mainfrom
dandclark:dandclark/reference-target

Conversation

@dandclark
Copy link
Copy Markdown
Contributor

@dandclark dandclark commented Feb 4, 2025

Reference Target is a feature to enable using IDREF attributes such as for and aria-labelledby to refer to elements inside a component's shadow DOM, while maintaining encapsulation of the internal details of the shadow DOM. The main goal of this feature is to enable ARIA to work across shadow root boundaries.

In this change, add the referenceTarget property to ShadowRoot and add a definition of reference target that's exported for use in other specs.

See the reference target explainer.


Preview | Diff

Comment thread dom.bs
Comment thread dom.bs Outdated
Comment thread dom.bs
dandclark and others added 4 commits May 6, 2025 23:12
- Add the concept of a "source" for an Event.
- Modify shadow root's "get the parent" algorithm such that events
propagate into the event's source's tree where appropriate.
Comment thread dom.bs
@dandclark
Copy link
Copy Markdown
Contributor Author

@annevk , @smaug---- , this is ready for another review along with whatwg/html#10995. For context on the event propagation change, see WICG/webcomponents#1098. Thanks!

@smaug----
Copy link
Copy Markdown
Contributor

What is the current testing situation? alice added several tests, but this is a rather complicated feature (at least from implementation point of view) so we need to ensure testing coverage is particularly good.
Gecko doesn't fail currently in too many (when run in Mozilla CI).
Chrome fails only some on wpt.fyi.

But are we testing all the complicated cases?

@dandclark
Copy link
Copy Markdown
Contributor Author

dandclark commented May 19, 2026

I've tried to ensure that the WPTs for this are thorough; I and other contributors added quite a few during work on the Chromium prototype.

E.g. https://wpt.fyi/results/shadow-dom/reference-target/tentative/property-reflection.html and https://wpt.fyi/results/shadow-dom/reference-target/tentative/property-reflection-idl-setters.html test reflection behavior for all the properties affected by reference target, with references pointing to and from every HTML element type.

https://github.com/web-platform-tests/wpt/blob/master/shadow-dom/reference-target/tentative/event-path.html covers all the interesting event propagation scenarios discussed in WICG/webcomponents#1098, plus some others I thought of.

All of the platform features testable in WPTs that can be affected by reference target (form submission, commandfor, interestfor, <label> (implicit association and with for)), popovertarget, and aria-labelledby, have WPTs under https://wpt.fyi/results/shadow-dom/reference-target/tentative.

There are a few other ARIA properties affected by reference target that aren't yet testable in WPTs; there are tests for those in the Chromium source tree at https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/web_tests/shadow-dom/reference-target/tentative/.

For some reason it doesn't seem like the Mozilla CI results are including the full set of tests. There are a larger number of tests in https://wpt.fyi/results/shadow-dom/reference-target/tentative that Gecko is shown as failing that don't appear in the linked Mozilla CI results.

@smaug----
Copy link
Copy Markdown
Contributor

smaug---- commented May 19, 2026

The pref in Gecko isn't enabled in Nightly, so wpt.fyi still shows many tests as failing. The pref is enabled in Mozilla CI.

Comment thread dom.bs Outdated
Comment thread dom.bs Outdated
Comment thread dom.bs Outdated
Comment thread dom.bs Outdated
<ol>
<li><p>If <var>event</var>’s <a>composed flag</a> is set, return <a for=/>shadow root</a>’s
<a for=DocumentFragment>host</a>.</p></li>
<li><p>Let <var>target</var> be <var>event</var>’s <a for=Event>path</a>'s first struct's
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use path[0] here?

Comment thread dom.bs
Comment thread dom.bs Outdated
<var>source</var>, return <a for=/>shadow root</a>'s <a for=DocumentFragment>host</a>.</p></li>
<li><p>If <var>source</var> is not null, and <var>source</var>'s <a for=tree>root</a> is a
<a>shadow-including ancestor</a> of <a for=/>shadow root</a>, return the result of
<a>retargeting</a> <var>target</var> against <var>source</var>.</p></li>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should add one or two examples here because this is getting rather involved. It seems really weird that as part of calculating the dispatch tree we are retargeting.

Also just to return the event target as the parent of the shadow tree. I can't really wrap my head around this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This in particular I would expect to have test coverage for every branch in detail. Including capture and bubble phases, the works. Does that exist?

Comment thread dom.bs
Comment thread dom.bs Outdated
Comment thread dom.bs Outdated
Comment thread dom.bs
<code>relatedTarget</code> attribute. [[UIEVENTS]]

<p>An <a>event</a> has an associated <dfn export for=Event>source</dfn> (a
<a>potential event target</a>). Unless stated otherwise it is null.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably have a note explaining where this is used. "Other specifications ..."

Comment thread dom.bs Outdated
Comment thread dom.bs

<li><p>If <var>source</var> is not null, and <var>source</var>'s <a for=tree>root</a> is a
<a>shadow-including ancestor</a> of <var>shadowRoot</var>, then return the result of
<a>retargeting</a> <var>target</var> against <var>source</var>.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I said in WICG/webcomponents#1098 (comment) I think we should still investigate relatedTarget model for this. Basically, the relevant events would be composed but propagate only up to the level where .target and .source are different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants