From 0b501d1f0bacd4f129cab36e179bca16836f1d33 Mon Sep 17 00:00:00 2001 From: Nikolai Shabalin Date: Sat, 8 Jul 2023 12:11:10 +0300 Subject: [PATCH 1/3] Adds htmlacademy/section-has-heading --- rules/section-has-heading/README.md | 28 ++++++++++++++++++++++++++ rules/section-has-heading/index.js | 31 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 rules/section-has-heading/README.md create mode 100644 rules/section-has-heading/index.js diff --git a/rules/section-has-heading/README.md b/rules/section-has-heading/README.md new file mode 100644 index 0000000..c3062df --- /dev/null +++ b/rules/section-has-heading/README.md @@ -0,0 +1,28 @@ +# htmlacademy/section-has-heading + +Правило проверяет наличие заголовка любого уровня h1-h6 у `
`. Правило принимает значения `true` или `false` + +## true +У `
` есть дочерний заголовок любого уровня h1-h6. + +Проблемными считаются следующие шаблоны: +```html +
+ ... +
+``` + +Следующие шаблоны **не** считаются проблемами: +```html +
+

title

+
+ +
+
+

title

+
+
+``` + +Вложенность заголовка(h1-h6) может быть любой. diff --git a/rules/section-has-heading/index.js b/rules/section-has-heading/index.js new file mode 100644 index 0000000..64b28d8 --- /dev/null +++ b/rules/section-has-heading/index.js @@ -0,0 +1,31 @@ +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 checkChildNode = (node) => { + if (isHeadingElement(node)) { + return true; + } + + if (node.children) { + 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
element must contain a heading of any level.", + }); + } + } +}; From 5feef054736b02f9f6b72c4be939ea498fd4716c Mon Sep 17 00:00:00 2001 From: Nikolai Shabalin Date: Sat, 8 Jul 2023 12:11:38 +0300 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d82fa8..5b2312b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.0.5 +Adds new rule htmlacademy/section-has-heading + ## 1.0.4 Fixed name for `head-meta-charset` From 9102d5a4592283a0425789d0ec368fb8e13092eb Mon Sep 17 00:00:00 2001 From: Nikolai Shabalin Date: Sun, 9 Jul 2023 12:18:27 +0300 Subject: [PATCH 3/3] Filters svg elements --- rules/section-has-heading/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/section-has-heading/index.js b/rules/section-has-heading/index.js index 64b28d8..0509970 100644 --- a/rules/section-has-heading/index.js +++ b/rules/section-has-heading/index.js @@ -2,12 +2,13 @@ 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) { + if (node.children && isNotSvg(node)) { for (const child of node.children) { if (checkChildNode(child)) { return true;