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: 7 additions & 6 deletions src/core/link-to-dfn.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,15 @@ function isCode(dfn) {
if (dfn.closest("code,pre")) {
return true;
}
// Note that childNodes.length === 1 excludes
// definitions that have either other text, or other
// whitespace, inside the <dfn>.
if (dfn.childNodes.length !== 1) {
const children = [...dfn.childNodes].filter(
n => !(n.nodeType === Node.TEXT_NODE && (n.textContent ?? "").trim() === "")
);
if (children.length !== 1) {
Comment on lines +259 to +262
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.

Added regression test in e8bead4. The test uses <dfn>\n <code>myTerm</code>\n</dfn> which creates leading/trailing whitespace text nodes around the <code> element inside the <dfn>. Asserts that the corresponding <a> link gets wrapped in <code>.

return false;
}
const [first] = /** @type {NodeListOf<HTMLElement>} */ (dfn.childNodes);
return first.localName === "code";
const child = children[0];
if (child.nodeType !== Node.ELEMENT_NODE) return false;
return /** @type {Element} */ (child).localName === "code";
}

/**
Expand Down
17 changes: 17 additions & 0 deletions tests/spec/core/link-to-dfn-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,23 @@ describe("Core — Link to definitions", () => {
expect(codeElem.querySelector("code")).toBeNull();
});

it("wraps links in code when dfn>code has surrounding whitespace text nodes", async () => {
const bodyText = `
<section>
<h2>Test Section</h2>
<p><dfn>
<code>myTerm</code>
</dfn></p>
<p id="link-target"><a>myTerm</a></p>
</section>`;
const ops = makeStandardOps(null, bodyText);
const doc = await makeRSDoc(ops);
const anchor = doc.querySelector("#link-target a");
expect(anchor).toBeTruthy();
expect(anchor.querySelector("code")).toBeTruthy();
expect(anchor.textContent).toBe("myTerm");
});

it("does not corrupt data-cite values when shortName is missing", async () => {
const body = `
<section>
Expand Down
Loading