// DIK-31: на страницах нет безымянных кнопок, полей, списков и ссылок; ☰ открывает понятное меню. import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import { renderPage } from "../src/page.js"; const ru = JSON.parse(fs.readFileSync("public/i18n/ru.json", "utf8")); const page = (f) => renderPage(fs.readFileSync(f, "utf8"), ru); const text = (s) => s.replace(/<[^>]*aria-hidden="true"[^>]*>[^<]*<\/\w+>/g, "").replace(/<[^>]+>/g, "").trim(); const attr = (tag, a) => (tag.match(new RegExp(`\\s${a}="([^"]*)"`)) || [])[1]; // Имя элемента: текст внутри, aria-label, label for=id или обёртывающий label. export function unnamed(html) { const bad = []; const labels = [...html.matchAll(/]*)>([\s\S]*?)<\/label>/g)]; for (const m of html.matchAll(/<(button|a|select|input|textarea)\b([^>]*)>/g)) { const [tag, name] = m; if (attr(tag, "type") === "hidden") continue; let label = (attr(tag, "aria-label") || "").trim(); if (!label && (name === "button" || name === "a")) { const end = html.indexOf(``, m.index); label = text(html.slice(m.index + tag.length, end)); } const id = attr(tag, "id"); if (!label && id) { const l = labels.find((x) => attr(``, "for") === id); if (l) label = text(l[2]); } if (!label) { const l = labels.find((x) => x.index < m.index && m.index < x.index + x[0].length); if (l) label = text(l[2]); } if (!label) bad.push(tag); } return bad; } for (const f of ["public/index.html", "public/base.html"]) { test(`Сценарий: у каждого элемента есть подпись — ${f}`, () => { assert.deepEqual(unnamed(page(f)), []); }); } test("Сценарий: элемент без подписи попал на страницу — проверка называет его", () => { assert.deepEqual(unnamed(''), ['`)); } assert.doesNotMatch(header, /\shidden[\s>=]/); });