Skip to content

fix(core/link-to-dfn): recognize code dfns with empty text nodes#5239

Draft
marcoscaceres wants to merge 2 commits intomainfrom
fix/3346-backtick-dfn
Draft

fix(core/link-to-dfn): recognize code dfns with empty text nodes#5239
marcoscaceres wants to merge 2 commits intomainfrom
fix/3346-backtick-dfn

Conversation

@marcoscaceres
Copy link
Copy Markdown
Contributor

Closes #3346

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates core/link-to-dfn’s “is this dfn code?” detection so <dfn> elements whose only meaningful child is <code> (but that also contain whitespace-only text nodes) are treated as code, enabling the requested <dfn>\thing`` authoring pattern.

Changes:

  • Filter out whitespace-only text nodes from a <dfn>’s childNodes before determining whether it contains exactly one <code> child.
  • Adjust the “single child” check to use the filtered node list.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/link-to-dfn.js Outdated
}
const [first] = /** @type {NodeListOf<HTMLElement>} */ (dfn.childNodes);
return first.localName === "code";
return /** @type {HTMLElement} */ (children[0]).localName === "code";
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.

Fixed in e8bead4. Now checks child.nodeType !== Node.ELEMENT_NODE before accessing localName, so non-element nodes (Text, Comment) are safely rejected without a forced JSDoc cast to HTMLElement. The remaining cast to Element is only reached after the nodeType guard confirms it is indeed an element.

Comment thread src/core/link-to-dfn.js
Comment on lines +259 to +262
const children = [...dfn.childNodes].filter(
n => !(n.nodeType === Node.TEXT_NODE && (n.textContent ?? "").trim() === "")
);
if (children.length !== 1) {
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>.

@marcoscaceres
Copy link
Copy Markdown
Contributor Author

@copilot Can you confirm all your feedback has been addressed? Can you fix anything that was not addressed?

Copy link
Copy Markdown
Contributor

Copilot AI commented May 4, 2026

@copilot Can you confirm all your feedback has been addressed? Can you fix anything that was not addressed?

All reviewer feedback has been addressed in e8bead4:

  1. nodeType guard (src/core/link-to-dfn.js:266): Added if (child.nodeType !== Node.ELEMENT_NODE) return false; before accessing localName, safely rejecting Text/Comment nodes without a forced JSDoc cast to HTMLElement. The Element cast on the next line is only reached after the guard confirms it's an element node.

  2. Regression test (tests/spec/core/link-to-dfn-spec.js:391–406): Added a test using <dfn>\n <code>myTerm</code>\n</dfn> (with leading/trailing whitespace text nodes around <code>), asserting the corresponding <a> link is wrapped in <code>.

…t nodes

Enables <dfn>`term`</dfn> to auto-wrap [=term=] links in <code>.
Inlines.js processes backticks before dfn.js, leaving empty text nodes
around the <code>. The isCode() check now filters these out.

Closes #3346
Address Copilot review feedback:
- Check child.nodeType === ELEMENT_NODE before accessing localName,
  preventing incorrect cast on non-element nodes (Text, Comment).
- Add regression test for <dfn> with whitespace text nodes around <code>.
@marcoscaceres marcoscaceres force-pushed the fix/3346-backtick-dfn branch from e8bead4 to c85674c Compare May 5, 2026 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow auto-coding of <dfn>thing<dfn>

3 participants