Files
co-lite/openspec/changes/add-page-anchor-links/design.md
T
ngqileandClaude Opus 5.5 92e62fb3fd fix(pages): reserve the reg- prefix for the sign-up form's field ids
A block named reg-email would have duplicated the form's input id, so its
label and #reg-email links would bind to the wrong element.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-03 09:43:23 +08:00

2.7 KiB

Context

See docs/superpowers/specs/2026-12-03-page-anchor-links-design.md. Blocks are one JSON column on cms_page; services-counter validates them; mobile-api passes them through as maps; the portal renders them. Custom HTML runs in an opaque-origin frame (allow-scripts allow-popups allow-popups-to-escape-sandbox) whose injected script already posts height reports, which HtmlFrame accepts only from its own contentWindow.

Goals / Non-Goals

Goals: named block targets; #name links from buttons, rich text and Custom HTML.

Non-Goals: automatic ids; any new frame permission; scroll offsets for a sticky header (the public page has none).

Decisions

  1. Field anchor on the block, pattern ^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$, unique per page. reg- is reserved because the sign-up form renders its fields with literal reg-* ids. Stored in the blocks JSON, so no DDL. Refusal codes: PagesBadAnchor, PagesAnchorTaken.
  2. Link rule. # plus the same pattern is a safe link everywhere a link is checked: CmsBlockValidator.safeLink, isSafeLink, the editor's SAFE_LINK, and the sanitiser (sanitize-html already keeps scheme-less hrefs; a test pins it).
  3. Ids. The block wrapper in CmsBlocks gets id={anchor} for every type, the HTML frame included. Rendered only when the name matches the pattern, so rows written another way cannot inject odd ids.
  4. Frame bridge. The injected script adds one click listener: for a[href^="#"] with a valid name it prevents the default and posts {type:"colite-frame-anchor", name, y}, where y is the in-frame element's document offset or null. HtmlFrame checks the source window as for heights, validates the message in a pure frameAnchor() helper, then scrolls the window to frame top + y, or to document.getElementById(name), else ignores it. Smooth scroll.
  5. Messages are data only. The page never navigates or evaluates anything from a frame message; it only scrolls.

Risks / Trade-offs

  • A frame script could post anchor requests at will → it can only scroll its own page, which it could already make tall. Accepted.
  • A link name equal to an id the portal already uses on that page (e.g. main) would clash → the sign-up form's field ids all start with reg-, which link names may not; other ids on the page come from useId and cannot match the pattern. The staff preview runs inside the back-office shell, whose ids are not checked against link names.
  • Pages saved by an older release lose link names → only on rollback; noted in the proposal.

Migration Plan

Deploy services-counter and the portal; run sql/01-anchor-labels.sql. Rollback: previous images and sql/99-rollback.sql.