-
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathhtml-aria-role-heading-requires-level.ts
More file actions
45 lines (37 loc) · 1.47 KB
/
html-aria-role-heading-requires-level.ts
File metadata and controls
45 lines (37 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { AttributeVisitorMixin, getAttributeName, getAttributes } from "./utils/rule-utils.js"
import { ParserRule } from "../types.js"
import type { LintOffense, LintContext } from "../types.js"
import type { Node, HTMLAttributeNode, HTMLOpenTagNode, HTMLSelfCloseTagNode } from "@herb-tools/core"
class AriaRoleHeadingRequiresLevel extends AttributeVisitorMixin {
// We want to check 2 attributes here:
// 1. role="heading"
// 2. aria-level (which must be present if role="heading")
checkAttribute(
attributeName: string,
attributeValue: string | null,
attributeNode: HTMLAttributeNode,
parentNode: HTMLOpenTagNode | HTMLSelfCloseTagNode
): void {
if (!(attributeName === "role" && attributeValue === "heading")) {
return
}
const allAttributes = getAttributes(parentNode)
// If we have a role="heading", we must check for aria-level
const ariaLevelAttr = allAttributes.find(attr => getAttributeName(attr) === "aria-level")
if (!ariaLevelAttr) {
this.addOffense(
`Element with \`role="heading"\` must have an \`aria-level\` attribute.`,
attributeNode.location,
"error"
)
}
}
}
export class HTMLAriaRoleHeadingRequiresLevelRule extends ParserRule {
name = "html-aria-role-heading-requires-level"
check(node: Node, context?: Partial<LintContext>): LintOffense[] {
const visitor = new AriaRoleHeadingRequiresLevel(this.name, context)
visitor.visit(node)
return visitor.offenses
}
}