Skip to content

Commit 1e4a16e

Browse files
Merge pull request #85 from htmlacademy/fix/htmlacademy/attr-req-value
Fix/htmlacademy/attr req value
2 parents 8e56f24 + d1827f1 commit 1e4a16e

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

rules/attr-req-value/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@
5151
<img src="images/image.jpg" width="100" height="100" alt="">
5252
<section data-test></section>
5353
```
54+
55+
## Исключения
56+
Один `<option>` в `<select>` может быть с пустым значением для атрибута `value`, если он выбран по умолчанию.
57+
58+
Следующий шаблон **не** считается проблемой:
59+
60+
```html
61+
<label for="fruits">Fruits</label>
62+
<select id="fruits" name="fruits" required>
63+
<option value="">Select...</option>
64+
<option value="banana">Banana</option>
65+
<option value="apple">Apple</option>
66+
</select>
67+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!doctype html>
2+
<html lang="ru">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
9+
<label for="">Fruits</label>
10+
11+
<label for="fruits">Fruits</label>
12+
<select id="fruits" name="fruits" required>
13+
<option value="">Select...</option> <!--problem-->
14+
<option value="">Shmelect...</option> <!--problem-->
15+
<option value="banana">Banana</option>
16+
<option value="apple">Apple</option>
17+
</select>
18+
</body>
19+
</html>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html lang="ru">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
9+
<label for="fruits-3">Fruits</label>
10+
<select id="fruits-3" name="fruits" required>
11+
<option value="orange">Orange</option>
12+
</select>
13+
14+
<label for="fruits-2">Fruits</label>
15+
<select id="fruits-2" name="fruits" required>
16+
<option value="orange">Orange</option>
17+
<option value="banana">Banana</option>
18+
<option value="apple">Apple</option>
19+
</select>
20+
21+
<label for="fruits">Fruits</label>
22+
<select id="fruits" name="fruits" required>
23+
<option value="">Select...</option>
24+
<option value="banana">Banana</option>
25+
<option value="apple">Apple</option>
26+
</select>
27+
</body>
28+
</html>

rules/attr-req-value/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ const matchesIgnoreList = (attributeName, ignoreList) => ignoreList.some((ignore
1313
}
1414
});
1515

16+
const isValidOptionValue = (node, name) => {
17+
if (name !== 'value' || !node.parent || node.parent.tagName !== 'select') {
18+
return true;
19+
}
20+
21+
const emptyOptions = node.parent.children.filter((child) =>
22+
child.tagName === 'option' && !has_non_empty_attribute(child, 'value')
23+
);
24+
25+
return emptyOptions.length > 1;
26+
};
1627

1728
module.exports = {
1829
name: 'htmlacademy/attr-req-value',
@@ -24,17 +35,18 @@ module.exports = {
2435
const name = attribute.name.chars.toLowerCase();
2536

2637
// eslint-disable-next-line camelcase
27-
if (!has_non_empty_attribute(node, name) && !is_boolean_attribute(attribute) && !matchesIgnoreList(name, rule_config.ignore)
28-
) {
29-
report({
30-
code: 'E006',
31-
position: attribute.loc,
32-
meta: {
33-
data: {
34-
attribute: name,
38+
if (!has_non_empty_attribute(node, name) && !is_boolean_attribute(attribute) && !matchesIgnoreList(name, rule_config.ignore)) {
39+
if (isValidOptionValue(node, name)) {
40+
report({
41+
code: 'E006',
42+
position: attribute.loc,
43+
meta: {
44+
data: {
45+
attribute: name,
46+
},
3547
},
36-
},
37-
});
48+
});
49+
}
3850
}
3951
});
4052
}

0 commit comments

Comments
 (0)