Skip to content

Commit 043e187

Browse files
committed
aria-label-misuse
1 parent c941677 commit 043e187

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

rules/aria-label-misuse/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# htmlacademy/aria-label-misuse
2+
3+
Запрещает неправильное использование `aria-label`. Атрибут может быть использован только для следующих элементов:
4+
5+
- Interactive elements
6+
- Labelable elements
7+
- Landmark elements
8+
- `<area>`
9+
- `<form>` and `<fieldset>`
10+
- `<iframe>`
11+
- `<img>` and `<figure>`
12+
- `<summary>`
13+
- `<table>`, `<td>` and `<th>`
14+
15+
## true
16+
17+
Проблемными считаются следующие шаблоны:
18+
```html
19+
<img src="" aria-label="foobar">
20+
```
21+
22+
Следующие шаблоны **не** считаются проблемами:
23+
```html
24+
<input type="text" aria-label="foobar">
25+
```

rules/aria-label-misuse/index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const {
2+
is_tag_node,
3+
has_non_empty_attribute,
4+
} = require("@linthtml/dom-utils");
5+
6+
const whitelisted = [
7+
"main", "nav", "table", "td", "th", "aside", "header", "footer", "section", "article", "form", "fieldset", "summary", "figure",
8+
];
9+
10+
const interactive = ["a", "audio", "button", "details", "iframe", "input", "progress", "select", "textarea", "video"]
11+
12+
function isValidUsage(node) {
13+
/* landmark and other whitelisted elements are valid */
14+
if (whitelisted.includes(node.name.chars.toLowerCase())) {
15+
return true;
16+
}
17+
18+
if (interactive.includes(node.name.chars.toLowerCase())) {
19+
return true;
20+
}
21+
22+
/* elements with tabindex (implicit interactive) are valid */
23+
return has_non_empty_attribute(node, "tabindex");
24+
}
25+
26+
module.exports = {
27+
name: "htmlacademy/aria-label-misuse",
28+
lint(node, rule_config, { report }) {
29+
if (is_tag_node(node) && !isValidUsage(node)) {
30+
report({
31+
position: node.loc,
32+
message: "\"aria-label\" cannot be used on this element",
33+
});
34+
}
35+
},
36+
};

0 commit comments

Comments
 (0)