Skip to content

Commit 8139627

Browse files
Merge pull request #2767 from johanrd/fix/require-input-label-id-false-positive
fix(require-input-label): don't count id as extra label when aria-label/labelledby present
2 parents b8acaa9 + 41bcb2e commit 8139627

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,22 @@ module.exports = {
160160
labelCount++;
161161
}
162162

163-
const hasId = hasAttr(node, 'id');
164-
const hasAriaLabel = hasAttr(node, 'aria-label');
165-
const hasAriaLabelledBy = hasAttr(node, 'aria-labelledby');
166-
if (hasId) {
163+
if (hasAttr(node, 'aria-label')) {
167164
labelCount++;
168165
}
169-
if (hasAriaLabel) {
170-
labelCount++;
171-
}
172-
if (hasAriaLabelledBy) {
166+
if (hasAttr(node, 'aria-labelledby')) {
173167
labelCount++;
174168
}
175169

176170
if (labelCount === 1) {
177171
return;
178172
}
179173

180-
if (validLabel && hasId) {
174+
// An `id` may pair with a sibling `<label for>` we can't see in this
175+
// template. Treat id-only as valid to avoid false positives, but don't
176+
// count it toward labelCount — otherwise id + aria-label is wrongly
177+
// flagged as multiple labels.
178+
if (labelCount === 0 && hasAttr(node, 'id')) {
181179
return;
182180
}
183181

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ ruleTester.run('template-require-input-label', rule, {
1919
'<template><input id="probablyHasLabel" /></template>',
2020
'<template><input aria-label={{labelText}} /></template>',
2121
'<template><input aria-labelledby="someIdValue" /></template>',
22+
// https://github.com/ember-template-lint/ember-template-lint/issues/3388
23+
// id alone does not establish a labeling relationship — a <label for> is
24+
// needed and cannot be verified statically. Only aria-label/labelledby
25+
// should count.
26+
'<template><input id="hello" aria-label="hello" /></template>',
27+
'<template><input id="hello" aria-labelledby="someIdValue" /></template>',
2228
'<template><div></div></template>',
2329
'<template><Input id="foo" /></template>',
2430
'<template>{{input id="foo"}}</template>',
@@ -97,12 +103,14 @@ ruleTester.run('template-require-input-label', rule, {
97103
errors: [{ message: MULTIPLE_LABELS }],
98104
},
99105
{
100-
code: '<template><input id="label-input" aria-label="second label"></template>',
106+
code: '<template><label>Input label<input aria-label="Custom label"></label></template>',
101107
output: null,
102108
errors: [{ message: MULTIPLE_LABELS }],
103109
},
104110
{
105-
code: '<template><label>Input label<input aria-label="Custom label"></label></template>',
111+
// id is irrelevant for labelling here — wrapping <label> + aria-label is
112+
// still multiple labels.
113+
code: '<template><label>Input label<input id="foo" aria-label="Custom label"></label></template>',
106114
output: null,
107115
errors: [{ message: MULTIPLE_LABELS }],
108116
},
@@ -173,6 +181,9 @@ hbsRuleTester.run('template-require-input-label', rule, {
173181
'<input id="probablyHasLabel" />',
174182
'<input aria-label={{labelText}} />',
175183
'<input aria-labelledby="someIdValue" />',
184+
// https://github.com/ember-template-lint/ember-template-lint/issues/3388
185+
'<input id="hello" aria-label="hello" />',
186+
'<input id="hello" aria-labelledby="someIdValue" />',
176187
'<div></div>',
177188
'<Input id="foo" />',
178189
'{{input id="foo"}}',
@@ -261,12 +272,12 @@ hbsRuleTester.run('template-require-input-label', rule, {
261272
errors: [{ message: MULTIPLE_LABELS }],
262273
},
263274
{
264-
code: '<input id="label-input" aria-label="second label">',
275+
code: '<label>Input label<input aria-label="Custom label"></label>',
265276
output: null,
266277
errors: [{ message: MULTIPLE_LABELS }],
267278
},
268279
{
269-
code: '<label>Input label<input aria-label="Custom label"></label>',
280+
code: '<label>Input label<input id="foo" aria-label="Custom label"></label>',
270281
output: null,
271282
errors: [{ message: MULTIPLE_LABELS }],
272283
},

0 commit comments

Comments
 (0)