Repository navigation
Conversation
formAssociated custom elements are labelable per HTML spec but isLabelable only checked built-in tag names. Add formAssociated check so getByLabelText finds them. Also add for/id fallback in getRealLabels for environments where label.control is null for custom elements (e.g. jsdom).
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 181abe4:
|
There was a problem hiding this comment.
Pull request overview
This PR updates label association logic so getByLabelText can work with form-associated custom elements by treating them as labelable and providing a for/id fallback when label.control is not implemented (e.g. in jsdom).
Changes:
- Extend
isLabelableto recognize form-associated custom elements. - Add a
for/idfallback ingetRealLabelswhenlabel.controlcannot be used. - Add/extend tests covering custom element label behavior via
getByLabelText.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/label-helpers.ts | Adds FACE detection to isLabelable and a for/id fallback path in getRealLabels. |
| src/tests/label-helpers.js | Adds tests intended to cover labelability of custom elements. |
| src/tests/element-queries.js | Adds an integration-style query test for custom elements via getByLabelText. |
馃挕 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
60
to
64
| /BUTTON|METER|OUTPUT|PROGRESS|SELECT|TEXTAREA/.test(element.tagName) || | ||
| (element.tagName === 'INPUT' && element.getAttribute('type') !== 'hidden') | ||
| (element.tagName === 'INPUT' && | ||
| element.getAttribute('type') !== 'hidden') || | ||
| (element as HTMLElement).formAssociated === true | ||
| ) |
Comment on lines
+9
to
+18
| test('form-associated custom elements are labelable', () => { | ||
| const element = document.createElement('my-input') | ||
| element.formAssociated = true | ||
| expect(getRealLabels(element)).toEqual([]) | ||
| }) | ||
|
|
||
| test('custom elements without formAssociated are not labelable', () => { | ||
| const element = document.createElement('my-input') | ||
| expect(getRealLabels(element)).toEqual([]) | ||
| }) |
Comment on lines
+1195
to
+1203
| test('can get a form-associated custom element by label', () => { | ||
| const {getByLabelText} = renderIntoDocument(` | ||
| <label for="custom-input">Custom Label</label> | ||
| <my-input id="custom-input"></my-input> | ||
| `) | ||
| const el = document.getElementById('custom-input') | ||
| el.formAssociated = true | ||
| expect(getByLabelText('Custom Label')).toBe(el) | ||
| }) |
This branch has not been deployed
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.
Fixes #1343
Summary
Adds a
formAssociatedcheck toisLabelableand afor/idfallback ingetRealLabelssogetByLabelTextcan find form-associated custom elements.Background
The HTML spec lists form-associated custom elements as labelable, but
isLabelableonly checked built-in tag names:A custom element with
formAssociated = trueshould be targetable by<label for>, butgetByLabelTextthrew "non-labellable" for it.getRealLabelsalso needed a fallback becauselabel.controlisnullfor custom elements in jsdom (only populated for built-in form controls). In real browserselement.labelsis defined for form-associated custom elements and the early return handles it. The fallback matches bylabel.htmlFor === element.idto cover jsdom.Verification
npm test: 669 tests pass (29 suites).npm run lint: clean.