Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
fix(core/examples): don't add number to id when possible (speced#3799)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi authored Oct 3, 2021
1 parent dc87c16 commit 0bbbb4d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
13 changes: 2 additions & 11 deletions src/core/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,7 @@ export function run() {
++number;
const div = makeTitle(example, number, report);
example.prepend(div);
if (title) {
addId(example, `example-${number}`, title); // title gets used
} else {
// use the number as the title... so, e.g., "example-5"
addId(example, "example", String(number));
}
const { id } = example;
const id = addId(example, "example", title || String(number));
const selfLink = div.querySelector("a.self-link");
selfLink.href = `#${id}`;
pub("example", report);
Expand All @@ -115,10 +109,7 @@ export function run() {
const div = html`<div class="example" id="${id}">
${exampleTitle} ${example.cloneNode(true)}
</div>`;
if (title) {
addId(div, `example-${number}`, title);
}
addId(div, `example`, String(number));
addId(div, "example", title || String(number));
const selfLink = div.querySelector("a.self-link");
selfLink.href = `#${div.id}`;
example.replaceWith(div);
Expand Down
8 changes: 4 additions & 4 deletions tests/spec/core/examples-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("Core — Examples", () => {
const example = doc.querySelector("div.example pre");
const div = example.closest("div");
expect(div.classList).toContain("example");
expect(div.id).toBe("example-1-ex");
expect(div.id).toBe("example-ex");

const markers = div.querySelectorAll("div.marker");
expect(markers).toHaveSize(1);
Expand All @@ -41,7 +41,7 @@ describe("Core — Examples", () => {
const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);
const example = doc.querySelector("aside.example");
expect(example.id).toBe("example-1-ex");
expect(example.id).toBe("example-ex");

const markers = example.querySelectorAll("div.marker");
expect(markers).toHaveSize(1);
Expand Down Expand Up @@ -98,7 +98,7 @@ describe("Core — Examples", () => {
const [example1, example2, example3] = exampleLinks;
expect(example1.getAttribute("href")).toBe("#example-1");
expect(example2.getAttribute("href")).toBe("#pass");
expect(example3.getAttribute("href")).toBe("#example-3-pass");
expect(example3.getAttribute("href")).toBe("#example-pass");
});
it("self-links examples made from pre", async () => {
const body = `
Expand All @@ -113,7 +113,7 @@ describe("Core — Examples", () => {
const [example1, example2, example3] = exampleLinks;
expect(example1.getAttribute("href")).toBe("#example-1");
expect(example2.getAttribute("href")).toBe("#pass");
expect(example3.getAttribute("href")).toBe("#example-3-pass");
expect(example3.getAttribute("href")).toBe("#example-pass");
});
it("relocates ids and doesn't duplicate them", async () => {
const body = `
Expand Down

0 comments on commit 0bbbb4d

Please sign in to comment.