Skip to content

Fix crash on template in foreign content - #18

Closed
AIEPhoenix wants to merge 1 commit into
syntax-tree:mainfrom
ai-markdown:fix/foreign-content-template
Closed

AIEPhoenix wants to merge 1 commit into
syntax-tree:mainfrom
ai-markdown:fix/foreign-content-template

Conversation

@AIEPhoenix

@AIEPhoenix AIEPhoenix commented Aug 24, 2026 •

Copy link
Copy Markdown

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

Closes #17, closes #15 — the same crash was reported earlier by @theRizwan, which I only found after filing; #16 fixes it too.

element() switched to the template-contents branch on the tag name alone, but parse5 — per the HTML spec — attaches template contents only to an HTML-namespace template. Inside foreign content (<svg>, <math>) a <template> is an ordinary foreign element whose children live in childNodes and whose content is undefined, so

fromParse5(parseFragment('<svg><template>x</template></svg>'))

threw TypeError: Cannot read properties of undefined (reading 'nodeName') — reachable through hast-util-raw, hast-util-from-html, and rehype-parse (whole-document mode included); an unclosed <svg><template> was enough.

The guard now also requires node.namespaceURI === webNamespaces.html (already imported), the same namespace check hast-util-to-html applies when serializing template contents — credit to #16 for taking this route first. The foreign template then takes the ordinary element path, and hastscript normalizes its children onto content anyway, so the converted shape matches the HTML form (asserted in the tests, both namespaces).

Five tests cover the svg/math/unclosed crash forms plus the shape of both the foreign and the HTML template. If you prefer to land #16, feel free to close this — the extra tests here can be grafted onto it.

@github-actions github-actions Bot added the 👋 phase/new Post is being triaged automatically label Aug 24, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions Bot added 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Aug 24, 2026
A template inside foreign content has no template contents: parse5 sets
content per the HTML spec on an HTML-namespace template alone, so inside
svg or math a template is an ordinary foreign element whose children live
in childNodes and whose content is undefined. The content switch read it
unconditionally, so

    fromParse5(parseFragment('<svg><template>x</template></svg>'))

threw TypeError: Cannot read properties of undefined (reading 'nodeName')
— reachable from hast-util-raw, hast-util-from-html, and rehype-parse
(whole-document mode included), an unclosed <svg><template> being enough
to trigger it.

The guard now also requires the html namespace, mirroring the check
hast-util-to-html uses when serializing template contents. The foreign
template takes the ordinary element path; hastscript then normalizes its
children onto content anyway, so the converted shape matches the HTML
form.
@AIEPhoenix
AIEPhoenix force-pushed the fix/foreign-content-template branch from 2492c79 to 32beb45 Compare August 24, 2026 11:53
@AIEPhoenix

Copy link
Copy Markdown
Author

Closing in favor of #16, which fixes the same crash the same way and was opened first. If extra regression coverage is useful, the five tests here (svg/math/unclosed crash forms plus the converted shape in both namespaces) are free to graft onto #16.

@AIEPhoenix AIEPhoenix closed this Aug 24, 2026
@github-actions

Copy link
Copy Markdown

Hi! This was closed. Team: If this was merged, please describe when this is likely to be released. Otherwise, please add one of the no/* labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

TypeError on template in the SVG or MathML namespace <svg><template> crashes: Cannot read properties of undefined (reading 'nodeName')

1 participant