fix: validate accessible-name IDs against the diagram slug

This commit is contained in:
Matt Van Horn
2026-08-11 19:40:06 -07:00
parent 19561779c6
commit 9a3e7e35b2
2 changed files with 34 additions and 7 deletions
+22 -5
View File
@@ -168,7 +168,7 @@ class AccessibleSvgParser(HTMLParser):
element.text.append(data) element.text.append(data)
def lint_accessible_svgs(text): def lint_accessible_svgs(text, expected_slug):
parser = AccessibleSvgParser(text) parser = AccessibleSvgParser(text)
parser.feed(text) parser.feed(text)
parser.close() parser.close()
@@ -265,6 +265,18 @@ def lint_accessible_svgs(text):
'bare id="title" and id="desc" are not allowed', 'bare id="title" and id="desc" are not allowed',
) )
expected_title_id = f"{expected_slug}-title"
expected_description_id = f"{expected_slug}-desc"
if not placeholder_ids and (
title_id != expected_title_id or description_id != expected_description_id
):
add(
svg.line,
svg.offset,
f'accessible-name IDs must match file slug "{expected_slug}": '
f'expected "{expected_title_id}" / "{expected_description_id}"',
)
if ( if (
labelled_by labelled_by
and (title_id not in labelled_by or description_id not in labelled_by) and (title_id not in labelled_by or description_id not in labelled_by)
@@ -330,6 +342,10 @@ def display_path(path):
return str(path) return str(path)
def diagram_slug(path):
return path.stem.removeprefix("example-")
def named_families(value): def named_families(value):
families = [] families = []
for raw_family in value.split(","): for raw_family in value.split(","):
@@ -342,8 +358,8 @@ def named_families(value):
return families return families
def lint_text(text, colors, rgb_triplets): def lint_text(text, colors, rgb_triplets, expected_slug):
findings = lint_accessible_svgs(text) findings = lint_accessible_svgs(text, expected_slug)
def add(offset, category, message): def add(offset, category, message):
findings.append((line_number(text, offset), offset, category, message)) findings.append((line_number(text, offset), offset, category, message))
@@ -473,10 +489,11 @@ def main():
try: try:
text = path.read_text(encoding="utf-8") text = path.read_text(encoding="utf-8")
relative = display_path(path) relative = display_path(path)
expected_slug = diagram_slug(path)
if args.baseline and (path.name in baseline or relative in baseline): if args.baseline and (path.name in baseline or relative in baseline):
findings = lint_accessible_svgs(text) findings = lint_accessible_svgs(text, expected_slug)
else: else:
findings = lint_text(text, colors, rgb_triplets) findings = lint_text(text, colors, rgb_triplets, expected_slug)
except OSError as error: except OSError as error:
findings = [(0, 0, "read-error", str(error))] findings = [(0, 0, "read-error", str(error))]
+12 -2
View File
@@ -119,6 +119,13 @@ def main() -> int:
'bare id="title" and id="desc" are not allowed', 'bare id="title" and id="desc" are not allowed',
directory, directory,
) )
require_failure(
"wrong-file-slug",
VALID_SVG,
'accessible-name IDs must match file slug "wrong-file-slug": '
'expected "wrong-file-slug-title" / "wrong-file-slug-desc"',
directory,
)
require_failure( require_failure(
"placeholder-ids", "placeholder-ids",
VALID_SVG.replace("fixture-title", "[diagram-slug]-title").replace( VALID_SVG.replace("fixture-title", "[diagram-slug]-title").replace(
@@ -161,9 +168,12 @@ def main() -> int:
"logz": build_icons.normalize_logz("<svg><path/></svg>"), "logz": build_icons.normalize_logz("<svg><path/></svg>"),
} }
for source, icon in generated_icons.items(): for source, icon in generated_icons.items():
slug = f"generated-{source}-icon"
require_pass( require_pass(
f"generated-{source}-icon", slug,
VALID_SVG.replace("</svg>", f" {icon}\n</svg>"), VALID_SVG.replace("fixture-title", f"{slug}-title")
.replace("fixture-desc", f"{slug}-desc")
.replace("</svg>", f" {icon}\n</svg>"),
directory, directory,
) )