MCPcopy
hub / github.com/htmlhint/HTMLHint / init

Function init

src/core/rules/input-requires-label.ts:7–57  ·  view source on GitHub ↗
(parser, reporter)

Source from the content-addressed store, hash-verified

5 id: 'input-requires-label',
6 description: 'All [ input ] tags must have a corresponding [ label ] tag. ',
7 init(parser, reporter) {
8 const labelTags: Array<{
9 event: Block
10 col: number
11 forValue?: string
12 }> = []
13 const inputTags: Array<{ event: Block; col: number; id?: string }> = []
14
15 parser.addListener('tagstart', (event) => {
16 const tagName = event.tagName.toLowerCase()
17 const mapAttrs = parser.getMapAttrs(event.attrs)
18 const col = event.col + tagName.length + 1
19
20 if (tagName === 'input') {
21 // label is not required for hidden input
22 if (mapAttrs['type'] !== 'hidden') {
23 inputTags.push({ event: event, col: col, id: mapAttrs['id'] })
24 }
25 }
26
27 if (tagName === 'label') {
28 if ('for' in mapAttrs && mapAttrs['for'] !== '') {
29 labelTags.push({ event: event, col: col, forValue: mapAttrs['for'] })
30 }
31 }
32 })
33
34 parser.addListener('end', () => {
35 inputTags.forEach((inputTag) => {
36 if (!hasMatchingLabelTag(inputTag)) {
37 reporter.warn(
38 'No matching [ label ] tag found.',
39 inputTag.event.line,
40 inputTag.col,
41 this,
42 inputTag.event.raw
43 )
44 }
45 })
46 })
47
48 function hasMatchingLabelTag(inputTag: { id?: string }) {
49 let found = false
50 labelTags.forEach((labelTag) => {
51 if (inputTag.id && inputTag.id === labelTag.forValue) {
52 found = true
53 }
54 })
55 return found
56 }
57 },
58} as Rule

Callers

nothing calls this directly

Calls 4

hasMatchingLabelTagFunction · 0.85
addListenerMethod · 0.80
getMapAttrsMethod · 0.80
warnMethod · 0.80

Tested by

no test coverage detected