Skip to content

Commit 34df970

Browse files
Merge pull request #23 from htmlacademy/feature/section-has-heading
#14 adds new rule section-has-heading
2 parents 23c2a1d + 9102d5a commit 34df970

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.0.5
4+
Adds new rule htmlacademy/section-has-heading
5+
36
## 1.0.4
47
Fixed name for `head-meta-charset`
58

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# htmlacademy/section-has-heading
2+
3+
Правило проверяет наличие заголовка любого уровня h1-h6 у `<section>`. Правило принимает значения `true` или `false`
4+
5+
## true
6+
У `<section>` есть дочерний заголовок любого уровня h1-h6.
7+
8+
Проблемными считаются следующие шаблоны:
9+
```html
10+
<section>
11+
...
12+
</section>
13+
```
14+
15+
Следующие шаблоны **не** считаются проблемами:
16+
```html
17+
<section>
18+
<h2>title</h2>
19+
</section>
20+
21+
<section>
22+
<div>
23+
<h2>title</h2>
24+
</div>
25+
</section>
26+
```
27+
28+
Вложенность заголовка(h1-h6) может быть любой.

rules/section-has-heading/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { is_tag_node } = require("@linthtml/dom-utils");
2+
3+
const isSectionElement = (node) => is_tag_node(node) && node.name === "section";
4+
const isHeadingElement = (node) => is_tag_node(node) && /^h[1-6]$/.test(node.name);
5+
const isNotSvg = (node) => node.name !== 'svg';
6+
const checkChildNode = (node) => {
7+
if (isHeadingElement(node)) {
8+
return true;
9+
}
10+
11+
if (node.children && isNotSvg(node)) {
12+
for (const child of node.children) {
13+
if (checkChildNode(child)) {
14+
return true;
15+
}
16+
}
17+
}
18+
19+
return false;
20+
};
21+
22+
module.exports = {
23+
name: "htmlacademy/section-has-heading",
24+
lint(node, rule_config, { report }) {
25+
if (isSectionElement(node) && !checkChildNode(node)) {
26+
report({
27+
position: node.loc,
28+
message: "The <section> element must contain a heading of any level.",
29+
});
30+
}
31+
}
32+
};

0 commit comments

Comments
 (0)