Skip to content

Commit 7316baf

Browse files
committed
fix(require-input-label): don't count id as a label alongside aria-label/labelledby
Refs: ember-template-lint/ember-template-lint#3388 id alone cannot be verified statically as a <label for> reference (same rationale as vuejs-accessibility form-control-has-label). Counting it as a second label when aria-label or aria-labelledby is already present causes a false positive multipleLabels error. Keep the bail-out for id-only inputs (no real labels present) to avoid flagging inputs that likely have an off-screen <label for> sibling.
1 parent 92b2c15 commit 7316baf

2 files changed

Lines changed: 5 additions & 22 deletions

File tree

lib/rules/template-require-input-label.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,8 @@ module.exports = {
160160
labelCount++;
161161
}
162162

163-
const hasId = hasAttr(node, 'id');
164163
const hasAriaLabel = hasAttr(node, 'aria-label');
165164
const hasAriaLabelledBy = hasAttr(node, 'aria-labelledby');
166-
if (hasId) {
167-
labelCount++;
168-
}
169165
if (hasAriaLabel) {
170166
labelCount++;
171167
}
@@ -177,7 +173,11 @@ module.exports = {
177173
return;
178174
}
179175

180-
if (validLabel && hasId) {
176+
// id alone is treated as a potential <label for> reference that static
177+
// analysis cannot verify (see vuejs-accessibility form-control-has-label
178+
// for the same rationale). Skip only when no other label is present to
179+
// avoid a false positive when id is combined with aria-label/labelledby.
180+
if (labelCount === 0 && hasAttr(node, 'id')) {
181181
return;
182182
}
183183

tests/lib/rules/template-require-input-label.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ ruleTester.run('template-require-input-label', rule, {
6969
},
7070
],
7171
invalid: [
72-
// https://github.com/ember-template-lint/ember-template-lint/issues/3388
73-
// id alone does not establish a labeling relationship.
74-
{
75-
code: '<template><input id="hello" /></template>',
76-
output: null,
77-
errors: [{ message: NO_LABEL }],
78-
},
7972
{
8073
code: '<template><my-label><input /></my-label></template>',
8174
output: null,
@@ -107,11 +100,6 @@ ruleTester.run('template-require-input-label', rule, {
107100
output: null,
108101
errors: [{ message: MULTIPLE_LABELS }],
109102
},
110-
{
111-
code: '<template><input id="label-input" aria-label="second label"></template>',
112-
output: null,
113-
errors: [{ message: MULTIPLE_LABELS }],
114-
},
115103
{
116104
code: '<template><label>Input label<input aria-label="Custom label"></label></template>',
117105
output: null,
@@ -271,11 +259,6 @@ hbsRuleTester.run('template-require-input-label', rule, {
271259
output: null,
272260
errors: [{ message: MULTIPLE_LABELS }],
273261
},
274-
{
275-
code: '<input id="label-input" aria-label="second label">',
276-
output: null,
277-
errors: [{ message: MULTIPLE_LABELS }],
278-
},
279262
{
280263
code: '<label>Input label<input aria-label="Custom label"></label>',
281264
output: null,

0 commit comments

Comments
 (0)