Skip to content

fix: ignore hidden input without label #890

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 2 commits into from
Jan 27, 2022
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
6 changes: 4 additions & 2 deletions dist/core/rules/input-requires-label.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion dist/htmlhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,9 @@
const mapAttrs = parser.getMapAttrs(event.attrs);
const col = event.col + tagName.length + 1;
if (tagName === 'input') {
inputTags.push({ event: event, col: col, id: mapAttrs['id'] });
if (mapAttrs['type'] !== 'hidden') {
inputTags.push({ event: event, col: col, id: mapAttrs['id'] });
}
}
if (tagName === 'label') {
if ('for' in mapAttrs && mapAttrs['for'] !== '') {
Expand Down
2 changes: 1 addition & 1 deletion dist/htmlhint.min.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/core/rules/input-requires-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export default {
const col = event.col + tagName.length + 1

if (tagName === 'input') {
inputTags.push({ event: event, col: col, id: mapAttrs['id'] })
// label is not required for hidden input
if (mapAttrs['type'] !== 'hidden') {
inputTags.push({ event: event, col: col, id: mapAttrs['id'] })
}
}

if (tagName === 'label') {
Expand Down
18 changes: 18 additions & 0 deletions test/rules/input-requires-label.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ describe(`Rules: ${ruleId}`, () => {
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Hidden input tag with no matching label should result in no error', () => {
const code = '<input id="some-id" type="hidden" />'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Hidden input tag with matching label before should result in no error', () => {
const code = '<label for="some-id"/><input id="some-id" type="hidden" />'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Hidden input tag with matching label after should result in no error', () => {
const code = '<input id="some-id" type="hidden" /><label for="some-id"/>'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})
})

describe('Error cases', () => {
Expand Down