feat(text): support form-associated custom element labels#5061
feat(text): support form-associated custom element labels#5061Krishnachaitanyakc wants to merge 2 commits intodequelabs:developfrom
Conversation
…le name computation Update nativeTextAlternative to detect form-associated custom elements (those with `static formAssociated = true` and `attachInternals()`) and include labelText in their naming methods so the accessible name algorithm can find associated <label> elements. Update getExplicitLabels to use the `labels` NodeList exposed by ElementInternals directly when available, and add deduplication in labelText to prevent double-counting when the implicit label is already included in `actualNode.labels`. Closes issue dequelabs#5045
There was a problem hiding this comment.
Thanks for the pr. I'm not sure this works as expected though. I'm pretty sure the label and labels properties are only on the internals object and not the root element itself. This means actualNode.labels is always undefined. Trying out the following shows that's the case as #target.label and #target.labels is undefined.
<script>
customElements.define(
'custom-input',
class extends HTMLElement {
static formAssociated = true;
constructor() {
super();
this.internals_ = this.attachInternals();
}
}
);
</script>
<label for="target">Custom label</label>
<custom-input id="target"></custom-input>|
@straker Thanks for reviewing, good catch. the label and labels properties are only on the internals object. I've updated the approach by using customElements.get(tagName)?.formAssociated instead, and label resolution falls through to the standard label[for=id] matching via findElmsInContext. |
…elements The labels property is only available on the ElementInternals object, not on the element itself, so actualNode.labels is always undefined for custom elements. Use customElements.get(tagName)?.formAssociated for detection and fall through to standard label[for=id] matching via findElmsInContext.
|
Thanks for the update. Unfortunately |
Summary
Updates the accessible name computation to support
<label>elements associated with form-associated custom elements viaElementInternals.nativeTextAlternative: Detects form-associated custom elements (those withstatic formAssociated = truethat calledattachInternals()) by checking for thelabelsproperty on the actual DOM node. When detected, addslabelTextto the element's naming methods so the accessible name algorithm resolves associated<label>elements.getExplicitLabels(inlabel-text.js): For custom elements with alabelsNodeList (exposed viaElementInternals), returns the labels directly instead of querying the DOM byid. Includes deduplication to prevent double-counting when an implicit (wrapping) label is already part ofactualNode.labels.Test plan
nativeTextAlternativecovering:formAssociated(should return empty)labelTextcovering:forattribute on a custom elementtest:jsdom,test:node,test:tsc)Closes #5045