Skip to content

Commit

Permalink
fix: ignore hidden input without label
Browse files Browse the repository at this point in the history
fix #866
  • Loading branch information
is2ei authored and nschonni committed Jan 27, 2022
1 parent fdd4c21 commit cab2907
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
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
6 changes: 6 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,12 @@ 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)
})
})

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

0 comments on commit cab2907

Please sign in to comment.