From 53e8e3d20dd812b14145ee4cdf46688d0e9ec6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Daoust?= Date: Sat, 7 Sep 2024 13:32:37 +0200 Subject: [PATCH] fix(core/xref) : preserve filename in URL (#4788) --- src/core/xref.js | 5 ++++- tests/spec/core/xref-spec.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/xref.js b/src/core/xref.js index 97bdacc208..6bc80ea7f4 100644 --- a/src/core/xref.js +++ b/src/core/xref.js @@ -400,7 +400,10 @@ function addDataCite(elem, query, result, conf) { // but sometimes we get lucky and we get an absolute URL from xref // which we can then use in other places (e.g., data-cite.js) const url = new URL(uri, "https://partial"); - const { pathname: citePath } = url; + let { pathname: citePath } = url; + // final resolution will be against the URL of the spec, which may end with + // a filename. That filename must be preserved if there's no specific path. + if (citePath === "/") citePath = ""; const citeFrag = url.hash.slice(1); const dataset = { cite, citePath, citeFrag, type }; if (forContext) dataset.linkFor = forContext[0]; diff --git a/tests/spec/core/xref-spec.js b/tests/spec/core/xref-spec.js index 313d76c85e..0c13a7338f 100644 --- a/tests/spec/core/xref-spec.js +++ b/tests/spec/core/xref-spec.js @@ -1043,4 +1043,16 @@ describe("Core — xref", () => { const test2 = doc.getElementById("test2"); expect(test2.classList).toContain("respec-offending-element"); }); + + it("preserves the filename in the URL of an external term", async () => { + const body = `
+

[=endpoint group=]

+
`; + const ops = makeStandardOps(null, body); + const doc = await makeRSDoc(ops); + const [specLink] = doc.querySelectorAll("#test a"); + expect(specLink.href).toBe( + "https://w3c.github.io/reporting/network-reporting.html#endpoint-group" + ); + }); });