Skip to content
Draft
3 changes: 2 additions & 1 deletion src/core/link-to-dfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ function collectDfns(title) {
if ("idl" in dfn.dataset || dfnType !== "dfn") {
result.get(dfnFor)?.set("idl", dfn);
}
addId(dfn, "dfn", title);
const idText = dfnFor ? `${dfnFor}-${title}` : title;
addId(dfn, "dfn", idText);
Comment on lines +173 to +174
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/core/location-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// the window to the correct point in the document when processing is done.

export const name = "core/location-hash";
const DFN_ID_PREFIX = "dfn-";

export function run() {
if (!window.location.hash) {
Expand Down Expand Up @@ -39,6 +40,16 @@ export function run() {
const updatedElement = document.getElementById(id);
if (updatedElement) {
newHash = id;
} else if (id.startsWith(DFN_ID_PREFIX)) {
const termWithLeadingHyphen = `-${id.slice(DFN_ID_PREFIX.length)}`;
const matchingElements = [
...document.querySelectorAll(
`[id^='${DFN_ID_PREFIX}'][id$='${CSS.escape(termWithLeadingHyphen)}']`
),
];
if (matchingElements.length === 1) {
newHash = matchingElements[0].id;
}
}
}
window.location.hash = `#${newHash}`;
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/core/link-to-dfn-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("Core — Link to definitions", () => {
const doc = await makeRSDoc(ops);
const a = doc.getElementById("testAnchor");
expect(a).toBeTruthy();
expect(a.hash).toBe("#dfn-test");
expect(a.hash).toBe("#dfn-window-test");
const decodedHash = decodeURIComponent(a.hash);
expect(doc.getElementById(decodedHash.slice(1))).toBeTruthy();
Comment on lines 18 to 22
});
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/core/location-hash-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
it("recovers legacy encoded hashes for slots", async () => {
const testURL = `${simpleURL}#dfn-%5B%5Bescapedslot%5D%5D`;
const doc = await makeRSDoc(ops, testURL);
expect(doc.location.hash).toBe("#dfn-escapedslot");
expect(doc.location.hash).toBe("#dfn-test-escapedslot");

Check failure on line 25 in tests/spec/core/location-hash-spec.js

View workflow job for this annotation

GitHub Actions / Karma Unit Tests (ChromeHeadless)

Core — Location Hash legacy fragment format recovers legacy encoded hashes for slots Expected '#dfn-[[escapedslot]]' to be '#dfn-test-escapedslot'.
}, 20000);
});
});
2 changes: 1 addition & 1 deletion tests/spec/core/xref-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ describe("Core — xref", () => {

// as base == [[type]], it is treated as a local internal slot
const link1 = doc.querySelector("#link1 a");
expect(link1.getAttribute("href")).toBe("#dfn-type");
expect(link1.getAttribute("href")).toBe("#dfn-window-type");
expect(link1.firstElementChild.localName).toBe("code");

// the base "Credential" is used as "forContext" for [[type]]
Expand Down
Loading