Skip to content

input-requires-label: warn when label wraps input but contains no visible text #1860

@ShivaniNR

Description

@ShivaniNR

Is your feature request related to a problem? Please describe.
After #1852 landed implicit label support, input-requires-label now correctly allows <input> nested inside <label>. However, it doesn't check whether the label actually contains visible text. This passes without warning:

<label><input type="password" /></label>

This is not accessible as a screen reader finds the input but has no name to announce. (Raised by @coliff in
#1852 (comment).)

Describe the solution you'd like
The rule should warn when an implicit <label> wraps an <input> but contains no non-whitespace text content.
Should warn:
<label><input type="password" /></label>
<label> <input type="text" /> </label>

Should pass:
<label>Password <input type="password" /></label>
<label><span>Email</span> <input type="email" /></label>

Proposed approach
The parser already emits a text event for text nodes between tags, and h1-require uses this exact pattern to check for empty <h1> tags. The same approach applies here:

  1. When <label> (without for) opens, reset a labelHasText flag to false
  2. On the text event, if inside an implicit label and the text isn't whitespace-only, set labelHasText = true
  3. When </label> closes, if the label contained inputs but labelHasText is still false, report a warning

This reuses the labelDepth tracking from #1852 and adds one event listener + one boolean flag.

Questions before I start

  1. Should this live in input-requires-label, or would you prefer a separate rule like label-requires-text?
  2. Should <label><img alt="icon"> <input></label> (no text node, but alt text on a child element) warn or pass? I'm leaning toward warn, since alt isn't visible label text, but wanted to check.

Happy to submit a PR for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions