Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/core/title.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ export function run(conf) {
}

// 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.

const existing = document.getElementById("title");
if (existing && existing !== h1Elem) {
const msg =
'Another element already uses `id="title"`. ' +
"The spec's `<h1>` will use a different id to avoid a collision.";
showError(msg, name, { elements: [existing] });
h1Elem.id = "document-title";
} else {
h1Elem.id = "title";
}
}
h1Elem.classList.add("title");

setDocumentTitle(conf, h1Elem);
Expand Down
57 changes: 57 additions & 0 deletions tests/spec/core/title-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"use strict";

import {
flushIframes,
makeDefaultBody,
makeRSDoc,
makeStandardOps,
} from "../SpecHelper.js";

describe("Core — Title", () => {
afterAll(flushIframes);

it("uses id='document-title' when another element already uses id='title'", async () => {
const body = `
<span id="title">not the title</span>
<h1>My Spec Title</h1>
${makeDefaultBody()}
`;
const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);

// The h1 should fall back to id="document-title"
const h1 = doc.querySelector("h1.title");
expect(h1).toBeTruthy();
expect(h1.id).toBe("document-title");

// The original id="title" element should still exist
const original = doc.getElementById("title");
expect(original).toBeTruthy();
expect(original.localName).toBe("span");

// An error should have been shown
const errors = doc.respec.errors.filter(
err => err.plugin === "core/title"
);
expect(errors).toHaveSize(1);
expect(errors[0].message).toContain('id="title"');
});

it("uses id='title' normally when there is no collision", async () => {
const body = `
<h1 id="title">My Spec Title</h1>
${makeDefaultBody()}
`;
const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);

const h1 = doc.querySelector("h1#title");
expect(h1).toBeTruthy();
expect(h1.id).toBe("title");

const errors = doc.respec.errors.filter(
err => err.plugin === "core/title"
);
expect(errors).toHaveSize(0);
});
});
Loading