Skip to content

Form control has label 924 #975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/rules/__tests__/form-control-has-label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ makeRuleTester("form-control-has-label", rule, {
valid: [
"<label for=''><input type='text' /></label>",
Copy link

@jashaj jashaj Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the original issue I had filed, but this line doesn't pass HTML validation:

  1. An empty string is not allowed for for
  2. If label has a for attribute, the nested form control (input). must have an id that matches the value of for.

It's also questionable if a label without text should pass.

"<input type='text' aria-label='test' />",
"<label for=''>text</label><input type='text' />",
"<input type='button'>",
`
<div class="checkbox">
<label for="check">I agree</label>
<input id="check" type="checkbox" />
</div>
`,
`
<div class="checkbox">
<input id="myCheckbox" type="checkbox" aria-describedby="myCheckboxInfo" />
<div class="checkbox-label">
<label for="myCheckbox">I agree</label>
<p id="myCheckboxInfo">Here is some extra info what I agree upon</p>
</div>
</div>
`,
`
<label>
<div>
Expand All @@ -29,9 +43,15 @@ makeRuleTester("form-control-has-label", rule, {
"<b-form-input />"
],
invalid: [
"<label for=''>text</label><input type='text' />",
`
<div class="checkbox">
<input type="checkbox" />
<label>I agree</label>
</div>
`,
"<input type='text' />",
"<textarea type='text'></textarea>",
"<custom-label for='input'>text</custom-label><input type='text' id='input' />",
{
code: "<div><b-form-input /></div>",
options: [{ controlComponents: ["b-form-input"] }],
Expand Down
30 changes: 24 additions & 6 deletions src/rules/form-control-has-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,34 @@ function isLabelElement(
return isMatchingElement(node, allLabelComponents);
}

function hasLabelElement(
function hasNestedLabelElement(
node: AST.VElement,
options: FormControlHasLabelOptions
): boolean {
const { parent } = node;

if (isLabelElement(parent, options)) {
return true;
}

return (
[parent, ...parent.children].some((node) =>
isLabelElement(node, options)
) ||
(parent && parent.type === "VElement" && hasLabelElement(parent, options))
parent &&
parent.type === "VElement" &&
hasNestedLabelElement(parent, options)
);
}

/**
* Check if the form control at least has an "id" to be associated with a label
* Can't really check for the label with a matching "for" attribute, because
* checking every element in the file may lead to bad performance.
*/
function hasIdForLabelElement(node: AST.VElement): boolean {
const id = getElementAttributeValue(node, "id");

return Boolean(id);
}

const rule: Rule.RuleModule = {
meta: {
type: "problem",
Expand Down Expand Up @@ -81,6 +95,9 @@ const rule: Rule.RuleModule = {
"input",
"textarea",
"select",
"meter",
"output",
"progress",
...(options.controlComponents || [])
];

Expand All @@ -101,7 +118,8 @@ const rule: Rule.RuleModule = {
if (
!isAriaHidden(node) &&
!hasAriaLabel(node) &&
!hasLabelElement(node, options)
!hasNestedLabelElement(node, options) &&
!hasIdForLabelElement(node)
) {
context.report({ node: node as any, messageId: "default" });
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"moduleResolution": "nodenext",
"module": "nodenext",
"target": "es2019",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
Expand Down