Skip to content

Reference Target: Clarify how JS Element attributes work with referenceTarget#1066

Merged
Westbrook merged 1 commit intoWICG:gh-pagesfrom
behowell:attribute-special-cases
Aug 22, 2024
Merged

Reference Target: Clarify how JS Element attributes work with referenceTarget#1066
Westbrook merged 1 commit intoWICG:gh-pagesfrom
behowell:attribute-special-cases

Conversation

@behowell
Copy link
Copy Markdown
Contributor

Update the JavaScript attributes that reflect Element objects to clarify which attributes return the host element. Also, add special cases for the .form and .list attributes, which return null because their types don't allow returning the host element.

Copy link
Copy Markdown
Collaborator

@Westbrook Westbrook left a comment

Choose a reason for hiding this comment

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

This explainer is getting closer and closer to spec text every time. Thanks for the added specificity!

These will _always_ refer to the **host** element that they're targeting, and _never_ the referenceTarget element directly. This behavior maintains the design that an [IDL attribute with type Element](https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:element) can only refer to an element that is a descendant of a [shadow-including ancestor](https://dom.spec.whatwg.org/#concept-shadow-including-ancestor) of the element hosting the attribute.
These will _never_ directly return the referenceTarget element that's inside the shadow tree. This is because an [IDL attribute with type Element](https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#reflecting-content-attributes-in-idl-attributes:element) can only refer to an element that is a descendant of a [shadow-including ancestor](https://dom.spec.whatwg.org/#concept-shadow-including-ancestor) of the element hosting the attribute.

Instead, most attributes return the **host** element that they're targeting, as long as the attribute's expected type is `HTMLElement`. However, The `.form` and `.list` attributes will return `null` when used with a referenceTarget, because they are expected to be `HTMLFormElement` or `HTMLDataListElement` and the host element itself is not a form or datalist. Importantly, the underlying association will still exist: the input will be connected to the form, for example; it's just not reflected by the `.form` attribute.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This feels a little odd, doesn't it? There is a form/list reference, and it's valid, but the JS accessor returns null. Perhaps it'd be better to have the IDL for form and list return HTMLElement (or even Element?)?

I suppose there are likely web compat issues with this, of some variety.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I agree it seems odd. My main concern was worrying not to break the web by changing the return type to HTMLElement instead of HTMLFormElement/HTMLDataListElement. That said, the change would only affect new scenarios that were previously not possible, and it should not affect existing code as far as I can tell. It would be good to have an expert weigh in on the potential problems with changing the IDL return types in the new scenarios.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do you want to hold this PR for this feedback or open a new PR once we've heard from other implementors about possible issues here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it would benefit from further discussion, but I don't think it should block this PR for now. I logged #1072 to discuss the follow-up options. Depending on the outcome of the discussion, another PR could be made to update this section.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sounds good. Thanks Ben!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants