Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.0.5
Adds new rule htmlacademy/section-has-heading

## 1.0.4
Fixed name for `head-meta-charset`

Expand Down
28 changes: 28 additions & 0 deletions rules/section-has-heading/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# htmlacademy/section-has-heading

Правило проверяет наличие заголовка любого уровня h1-h6 у `<section>`. Правило принимает значения `true` или `false`

## true
У `<section>` есть дочерний заголовок любого уровня h1-h6.

Проблемными считаются следующие шаблоны:
```html
<section>
...
</section>
```

Следующие шаблоны **не** считаются проблемами:
```html
<section>
<h2>title</h2>
</section>

<section>
<div>
<h2>title</h2>
</div>
</section>
```

Вложенность заголовка(h1-h6) может быть любой.
32 changes: 32 additions & 0 deletions rules/section-has-heading/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { is_tag_node } = require("@linthtml/dom-utils");

const isSectionElement = (node) => is_tag_node(node) && node.name === "section";
const isHeadingElement = (node) => is_tag_node(node) && /^h[1-6]$/.test(node.name);
const isNotSvg = (node) => node.name !== 'svg';
const checkChildNode = (node) => {
if (isHeadingElement(node)) {
return true;
}

if (node.children && isNotSvg(node)) {
for (const child of node.children) {
if (checkChildNode(child)) {
return true;
}
}
}

return false;
};

module.exports = {
name: "htmlacademy/section-has-heading",
lint(node, rule_config, { report }) {
if (isSectionElement(node) && !checkChildNode(node)) {
report({
position: node.loc,
message: "The <section> element must contain a heading of any level.",
});
}
}
};