Skip to content

Commit

Permalink
Preserve the filename in the URL of an external term
Browse files Browse the repository at this point in the history
See #4787 for context. The URL of the spec may end with a filename but the code
assumed that it would always be a path, and thus that resolving `./` against
the URL of the spec would be a no-op. The code now makes sure resolve the URL
of the spec against an empty string when the xref search does not return a more
specific path.
  • Loading branch information
tidoust committed Sep 7, 2024
1 parent 39079f3 commit c52a42a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/core/xref.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
10 changes: 10 additions & 0 deletions tests/spec/core/xref-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,4 +1043,14 @@ 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 = `<section id="test">
<p data-cite="network-reporting">[=endpoint group=]</p>
</section>`;
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");
});
});

0 comments on commit c52a42a

Please sign in to comment.