Skip to content

New rule: links to in-page anchors that do not exist #706

Closed

Description

@nc9

Problem

A link to #pricing or /docs/setup#install whose target id no longer exists still "works": the page loads, nothing errors, and the reader lands at the top of the page instead of the section they clicked. It happens all the time after a redesign or a CMS migration: headings get new auto-generated ids, a "Jump to pricing" button in the site nav still points at the old one, a table of contents is copied from another page, a skip link points at #main-content on a template that renamed its <main>.

squirrel never checks this. links/broken-links normalizes every link target with the fragment stripped before looking up its status, so /docs#does-not-exist reads as a healthy link to /docs. a11y/skip-link passes as soon as a skip link exists and does not look at its target.

Proposal

New rule links/broken-anchors.

  • Warn (same page): a link to #id on the page itself, where no element on that page has id="id" and no <a name="id"> exists (the two targets the HTML spec resolves a fragment to).
  • Warn (another crawled page): a link to /other-page#id where /other-page was crawled this run and has no such id or name.
  • One finding per target URL and fragment, listing the pages that link to it, so a broken anchor in the site nav is one finding, not one per page.
  • Unconfirmed (info), not broken: the target page was fetched without rendering and looks client-rendered (the ids can be added by JavaScript after load).
  • Labels skip links: when the broken link is a skip link, the finding says so. a11y/skip-link keeps its current verdict, so the missing target is reported once, here.

Not reported:

  • # alone, and #top when the page has no element with that id (both scroll to the top by spec).
  • Hash routes and state, which are not element ids: fragments starting with / or ! (#/route, #!/route), fragments containing = (#access_token=..., #q=shoes), and text fragments (#:~:text=).
  • Links to pages the crawl did not fetch, to external sites, or to pages that answered 4xx/5xx (links/broken-links already reports those).

Matching follows the browser: the fragment is percent-decoded and compared case-sensitively.

Notes

  • Cost: the same-page check reads the DOM a page rule already has. The cross-page check needs each crawled page's set of ids and <a name> values as a small, capped per-page record, so it also works in streaming audits that do not keep every page's DOM in memory. When a page's id set hits the cap, links into it are not judged.
  • False-positive risk: low for same-page links in server-rendered HTML. Higher for SPA pages and JS-built tables of contents, which is why unrendered client-rendered targets are reported as unconfirmed.
  • Related: docs: make validate does not catch broken #anchor links #649 is the same problem for this repo's own docs build, not for audited sites.

Acceptance Criteria

  • Fixture: a page with <a href="#pricing"> and no element with id="pricing" produces a warning naming the link, the fragment and the page
  • Fixture: the same page with <section id="pricing">, or with <a name="pricing">, produces no finding
  • Fixture: page A links to /b#install, page B is crawled and has no id="install": one warning naming /b#install and listing page A as a source
  • Fixture: five pages share a nav link to /#pricing and the homepage has no id="pricing": one finding listing the five source pages, not five findings
  • href="#", #top (with no id="top" on the page), #/settings, #!/home, #access_token=abc and #:~:text=hello produce no finding
  • A link to /c#x where /c was not crawled, or answered 404, produces no finding from this rule
  • An id with non-ASCII or percent-encoded characters matches its decoded link (#caf%C3%A9 finds id="café"), and #Pricing does not match id="pricing"
  • Fixture: in an unrendered audit, a link to /app#settings where /app is an empty client-rendered shell (<div id="root"></div> plus a script) is reported as unconfirmed (info), not as a warning
  • A skip link to a missing #main-content is reported once, by this rule, and labelled as a skip link; the a11y/skip-link verdict for that page is unchanged
  • The cross-page check gives the same result in a streaming audit as in a non-streaming one
  • The rule has a docs page under docs/rules/links/

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementrulessize: M

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions