Skip to content

Commit f9e0643

Browse files
Merge pull request #93 from htmlacademy/fix/tag-req-attr
Fix tag-req-attr
2 parents 954dd96 + 1a19168 commit f9e0643

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

rules/tag-req-attr/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# htmlacademy/tag-req-attr
2+
3+
Если установлено, указанные атрибуты должны присутствовать в указанном теге.
4+
5+
Форк: https://linthtml.vercel.app/user-guide/rules/list/tag-req-attr
6+
7+
## true
8+
9+
```json
10+
'htmlacademy/tag-req-attr': [
11+
true, {
12+
'input': [
13+
{
14+
name: 'name'
15+
},
16+
],
17+
// Другие элементы...
18+
},
19+
]
20+
```
21+
22+
```json
23+
{
24+
"tag-req-attr": [
25+
true,
26+
{
27+
"img": [
28+
{
29+
"name": "src"
30+
},
31+
{
32+
"name": "alt"
33+
}
34+
]
35+
}
36+
]
37+
}
38+
```
39+
40+
Нарушениями считаются следующие модели:
41+
42+
```html
43+
<img/>
44+
```
45+
46+
```html
47+
<img src="link"/>
48+
```
49+
50+
```html
51+
<img alt="No image">
52+
```
53+
54+
Следующие детали не считаются нарушениями:
55+
56+
```html
57+
<img alt="Picture of a cute cat" src="https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwiHzdu5n4ThAhXOxYUKHebmDXoQjRx6BAgBEAU&url=https%3A%2F%2Fimgur.com%2Fgallery%2FHzG2YW8&psig=AOvVaw3w5Zu0oMuDZy83zsfn0NMU&ust=1552742695628256">
58+
```
59+
60+
## ignore
61+
62+
Поле `ignore` позволяет игнорировать атрибуты в зависимости от их значений.
63+
64+
```json
65+
{
66+
'htmlacademy/tag-req-attr': [
67+
true,
68+
{
69+
'input': [
70+
{
71+
name: 'name',
72+
ignore: {
73+
'type': 'submit'
74+
}
75+
}
76+
]
77+
}
78+
]
79+
}
80+
```
81+
82+
Нарушениями считаются следующие модели:
83+
84+
```html
85+
<input name="name" type="submit">
86+
```
87+
88+
Следующие детали **не** считаются нарушениями:
89+
90+
Если у элемента `input` атрибут `type` имеет значение `submit`, то атрибут `name` не обязателен.
91+
```html
92+
<input type="submit" value="Submit">
93+
```

rules/tag-req-attr/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
// eslint-disable-next-line camelcase
3+
const { is_tag_node, has_non_empty_attribute, has_attribute, attribute_value } = require('@linthtml/dom-utils');
4+
const checkAttributes = (node, requiredAttributes, report) => {
5+
requiredAttributes.forEach(({ name, allowEmpty, ignore }) => {
6+
allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty;
7+
if (ignore) {
8+
let shouldIgnore = false;
9+
for (const key in ignore) {
10+
if (has_attribute(node, key) && attribute_value(node, key).chars === ignore[key]) {
11+
shouldIgnore = true;
12+
break;
13+
}
14+
}
15+
if (shouldIgnore) {
16+
return;
17+
}
18+
}
19+
20+
if (!has_attribute(node, name) || !has_non_empty_attribute(node, name, allowEmpty)) {
21+
report({
22+
code: 'E057',
23+
position: node.open.loc,
24+
meta: {
25+
data: {
26+
attribute: name,
27+
tag: node.name
28+
}
29+
}
30+
});
31+
}
32+
});
33+
};
34+
35+
36+
module.exports = {
37+
name: 'htmlacademy/tag-req-attr',
38+
// eslint-disable-next-line camelcase
39+
lint(node, rule_config, { report }) {
40+
if (is_tag_node(node)) {
41+
// eslint-disable-next-line camelcase
42+
for (const tagName in rule_config) {
43+
if (Object.hasOwnProperty.call(rule_config, tagName) && tagName === node.name) { // Ensured property belongs to object
44+
// eslint-disable-next-line camelcase
45+
const requiredAttributes = rule_config[tagName];
46+
checkAttributes(node, requiredAttributes, report);
47+
}
48+
}
49+
}
50+
},
51+
};

0 commit comments

Comments
 (0)