seo-graph icon indicating copy to clipboard operation
seo-graph copied to clipboard

fix(astro-seo-graph): recognize bare-boolean alt in image-alt check

Open xav-ie opened this issue 6 months ago • 0 comments

Problem

validateImageAlt warns on every <Picture alt=""> rendered by Astro, defeating the documented decorative-image escape hatch for any consumer using astro:assets.

Repro (against a fresh astro:assets site):

<Picture src={preview} alt="" formats={['avif','webp']} />

Build output:

[WARN] [@jdevalk/astro-seo-graph] Image alt validation: index.html has 1 <img> without alt: /_astro/preview.xxx.webp

But the source alt="" is correct — these are decorative previews adjacent to a captioned heading.

Cause

Vite's HTML minifier (used by Astro) implements HTML5 §2.3.2 and drops the value from empty boolean-form attributes, so alt="" in source becomes <img ... alt ...> in the built HTML. The check at src/integration.ts:503 was:

if (/\balt\s*=/i.test(tag)) continue;

The trailing \s*= required a literal =, so the bare-boolean form was missed and counted as missing.

Fix

if (/\salt(?:\s*=|\s|\/?>|$)/i.test(tag)) continue;

The lookahead group accepts:

  • \s*= — alt="..." (today's case)
  • \s — bare boolean followed by another attribute: <img ... alt sizes="...">
  • \/?> — bare boolean as last attribute: <img alt> or <img alt/>
  • $ — defensive; not reachable on well-formed tags

The leading \s (instead of \b) anchors alt to an attribute position. This also closes a latent false-skip in the old regex: \b matched the boundary between - and a in data-alt, so <img src="/x" data-alt="caption"> was incorrectly skipped as having an alt attribute.

Tests

Six new assertions in findImagesWithoutAlt:

  • Four bare-boolean placements: alone, before another attribute, as the first attribute, before />.
  • Two data-alt cases (with and without value) confirming the latent false-skip is closed.

All 264 tests pass (pnpm --filter @jdevalk/astro-seo-graph test).

Compatibility

No API change. Behavior change is a strict superset: anything previously recognized is still recognized, plus the cases above.

xav-ie avatar May 22 '26 03:05 xav-ie