Repository navigation
Fix crash on template in foreign content - #18
Closed
AIEPhoenix wants to merge 1 commit into
Closed
AIEPhoenix wants to merge 1 commit into
AIEPhoenix wants to merge 1 commit into
Conversation
This comment has been minimized.
This comment has been minimized.
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
force-pushed
the
fix/foreign-content-template
branch
from
August 24, 2026 11:53
2492c79 to
32beb45
Compare
Author
|
Hi! This was closed. Team: If this was merged, please describe when this is likely to be released. Otherwise, please add one of the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initial checklist
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 inchildNodesand whosecontentisundefined, sothrew
TypeError: Cannot read properties of undefined (reading 'nodeName')— reachable throughhast-util-raw,hast-util-from-html, andrehype-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 checkhast-util-to-htmlapplies 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 ontocontentanyway, 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.