fix(core/link-to-dfn): recognize code dfns with empty text nodes#5239
fix(core/link-to-dfn): recognize code dfns with empty text nodes#5239marcoscaceres wants to merge 2 commits intomainfrom
Conversation
There was a problem hiding this comment.
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>’schildNodesbefore 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.
| } | ||
| const [first] = /** @type {NodeListOf<HTMLElement>} */ (dfn.childNodes); | ||
| return first.localName === "code"; | ||
| return /** @type {HTMLElement} */ (children[0]).localName === "code"; |
There was a problem hiding this comment.
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.
| const children = [...dfn.childNodes].filter( | ||
| n => !(n.nodeType === Node.TEXT_NODE && (n.textContent ?? "").trim() === "") | ||
| ); | ||
| if (children.length !== 1) { |
There was a problem hiding this comment.
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>.
|
@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:
|
…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>.
e8bead4 to
c85674c
Compare
Closes #3346