feat(commons/text): support form-associated labels via element internals#5182
Draft
chutchins25 wants to merge 1 commit into
Draft
feat(commons/text): support form-associated labels via element internals#5182chutchins25 wants to merge 1 commit into
chutchins25 wants to merge 1 commit into
Conversation
Detect form-associated custom elements in nativeTextAlternative and add labelText, and resolve labels from internals.labels in labelText. Both are gated by the elementInternals run option. Closes issue #5045
74d3009 to
b0fbb14
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates axe-core’s commons/text accessible-name computation to recognize native <label> associations for form-associated custom elements by leveraging ElementInternals.labels (when available and enabled via the existing elementInternals feature flag).
Changes:
- Extend
nativeTextAlternativeto appendlabelTextfor form-associated custom elements detected viaElementInternals. - Update
labelTextto preferinternals.labels(authoritative explicit + implicit labels) and fall back to DOM-based lookup otherwise. - Add/expand test utilities and unit tests covering explicit/implicit/multiple labels, deduping, global map internals, and Shadow DOM scenarios.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
lib/commons/text/native-text-alternative.js |
Detects form-associated custom elements via internals and conditionally adds labelText as a naming method. |
lib/commons/text/label-text.js |
Uses ElementInternals.labels (guarded) to compute label text, falling back to existing DOM resolution. |
test/testutils.js |
Introduces testutils-form-element (form-associated) for label-related tests. |
test/commons/text/native-text-alternative.js |
Adds unit tests validating label pickup + feature-flag gating for form-associated custom elements. |
test/commons/text/label-text.js |
Adds unit tests for internals-based label resolution (including global map + Shadow DOM). |
test/commons/aria/get-role.js |
Adds regression tests clarifying presentational role inheritance behavior with ElementInternals roles. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+47
to
+56
| const internals = virtualNode.elementInternals; | ||
| if (internals && !methods.includes('labelText')) { | ||
| try { | ||
| // `labels` throws on custom elements that aren't form-associated | ||
| internals.labels; | ||
| textMethods.push(nativeTextMethods.labelText); | ||
| } catch { | ||
| // not a form-associated custom element | ||
| } | ||
| } |
Comment on lines
+155
to
+166
| const { actualNode } = target; | ||
| const internals = actualNode._internals; | ||
| // remove the property protocol so the global map is the only source. | ||
| // elementInternals is read lazily, so the first read happens below | ||
| delete actualNode._internals; | ||
| globalThis._elementInternals = new WeakMap(); | ||
| globalThis._elementInternals.set(actualNode, internals); | ||
|
|
||
| assert.equal(labelText(target), 'My explicit label'); | ||
|
|
||
| delete globalThis._elementInternals; | ||
| }); |
Comment on lines
+73
to
+81
| it('does not add a label when element internals are disabled', () => { | ||
| axe._enableElementInternals = false; | ||
| const vNode = queryFixture(` | ||
| <label for="target">My explicit label</label> | ||
| <testutils-form-element id="target"></testutils-form-element> | ||
| `); | ||
| assert.equal(nativeTextAlternative(vNode), ''); | ||
| axe._enableElementInternals = true; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Form-associated custom elements (
static formAssociated = true+attachInternals()) can be labeled with native<label>elements, which the browser exposes throughElementInternals.labels. axe-core's accessible-name computation dropped these labels entirely, because:nativeTextAlternativebuilds its naming methods from the element spec, andgetElementSpec()returns{}for custom element names — solabelTextwas never invoked for a custom element.labelTextonly looked at the DOM byid(label[for]) / ancestor wrapping.This wires up the proposal's final step.
Changes
native-text-alternative.js—findTextMethodsdetects a form-associated custom element viaElementInternalsand appendslabelText.label-text.js—labelTextreadsinternals.labelsdirectly when present (the authoritative, complete set of explicit + implicit labels), falling back to the existing DOM resolution otherwise.ElementInternals.labelsthrowsNotSupportedErroron a custom element that isn't form-associated, so both reads are guarded with try/catch. Both paths are gated by the existingelementInternalsrun option — with the flag off,vNode.elementInternalsisundefinedand behavior is unchanged.Why
internals.labels(not the DOM lookup)?It's the browser-authoritative association and is the complete set — it already includes the ancestor-wrapping label. Routing it through the existing explicit + implicit merge would count a wrapping label twice, so
labelTextuses the internals list exclusively when present.Out of scope
checks/label(explicit/implicit) — no change needed. They resolve labels vialabel[for=id]/closest('label'), which already cover custom elements, and thelabelrule selector isinput, textareaanyway. Matches the proposal ("rules that look at implicit/explicit labels will not need to be updated") and feat(checks/label): support ARIA element internals properties #5170 leaving those checks untouched.<label for>— labelable-ness/role territory (ElementInternals: updateimplicitRoleto usegetElementInternals#5039), pre-existing.getResolvedRefs(feat(dom/getResolvedRefs): new function to get the resolved virtual nodes of idrefs #5151) — native<label>/forassociation isn't an ARIA idref, so it doesn't apply here.Testing
test/testutils.js— new sharedtestutils-form-element(form-associated).label-text.js— explicit, implicit, multiple, implicit+explicit dedup, global-map protocol, non-form-associated fall-through, Shadow DOM.native-text-alternative.js— explicit/implicit pickup, gating (non-form-associated gets no label), flag-off no-op.No integration test added — this is a
commons/textchange with no rule/check edits; the nearest rule impact (aria-input-field-nameon a role-bearing custom element) depends on role-via-internals selection (#5039), which isn't wired yet.Closes #5045