Skip to content

fix(core/title): detect id="title" collision and use fallback - #5285

Draft
marcoscaceres wants to merge 3 commits into
mainfrom
fix/4712-title-id-collision
Draft

fix(core/title): detect id="title" collision and use fallback#5285
marcoscaceres wants to merge 3 commits into
mainfrom
fix/4712-title-id-collision

Conversation

@marcoscaceres

@marcoscaceres marcoscaceres commented May 9, 2026

Copy link
Copy Markdown
Contributor

Closes #4712

When another element already uses id="title", emit an error and
fall back to id="document-title" for the h1.

Written with AI: this change was generated by Claude. Per AI_POLICY.md.

When another element already uses id="title", the spec's h1 now gets
id="document-title" instead of silently creating a duplicate ID. An
error is shown identifying the conflicting element.

Closes #4712
@marcoscaceres marcoscaceres added the AI Contains AI-generated logic; see AI_POLICY.md label Aug 5, 2026
Comment thread src/core/title.js

// Decorate the spec title
if (!h1Elem.id) h1Elem.id = "title";
if (!h1Elem.id) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a test. @copilot, can you add a test for this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests/spec/core/title-spec.js with two tests:

  1. Verifies that when another element already has id="title", the <h1> falls back to id="document-title" and a core/title error is emitted.
  2. Verifies normal behavior (no collision) — <h1> keeps id="title" and no error is emitted.

Co-authored-by: marcoscaceres <870154+marcoscaceres@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds title-ID collision handling, but the new path is unreachable and incompatible with existing title consumers.

Changes:

  • Detects an existing id="title".
  • Reports an error and assigns a fallback ID.
Suppressed comments (2)

src/core/title.js:61

  • Assigning the fallback ID breaks downstream title handling. The W3C, AOM, and DINI header templates still locate the title with document.querySelector("h1#title") (src/w3c/templates/headers.js:249, src/aom/templates/headers.js:51, and src/dini/templates/headers.js:115), so a reachable fallback title will not be moved into the document header; src/w3c/level.js:41 can also dereference null, and the back-to-top link still targets the colliding #title. Update these consumers to locate the generated title independently of its ID and link to its actual ID.
      h1Elem.id = "document-title";

src/core/title.js:61

  • The fixed fallback is not guaranteed to be free. If the source already contains id="document-title", this assignment creates another duplicate ID while claiming to avoid a collision. Generate a unique fallback (the existing addId helper already handles occupied IDs) and cover this case in the collision tests.
      h1Elem.id = "document-title";

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/core/title.js

// Decorate the spec title
if (!h1Elem.id) h1Elem.id = "title";
if (!h1Elem.id) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Contains AI-generated logic; see AI_POLICY.md

Projects

None yet

Development

Successfully merging this pull request may close these issues.

More flexible title id generation

3 participants